需要实现的效果:点击上、下图标实现列表项的排序
<view v-for="(sort, i) in sortList" :key="i" >
<view>{{i+1}}.{{sort.maintenanceContent}}</view>
<view>
<!--第一项不显示up图标-->
<view v-if="i!=0" @click="sortUp(i)">
<image src="up.png"></image>
</view>
<!--最后一项不显示down图标-->
<view v-if="i!=sortList.length-1" @click="sortDown(i)">
<image src="down.png" ></image>
</view>
</view>
</view>
列表项上移
sortUp(index) {
// index = 1 // 当前下标为1
// this.sortList = ['内容1','内容2','内容3','内容4','内容5']
let oldList = [...this.sortList];
let newList = [...this.sortList];
// 将当前数组下标的为1的item删除
newList.splice(index, 1); // 当前返回 ['内容1','内容3','内容4','内容5']
// 在当前数组下标的前一个数组下标添加 当前数组下标的item
newList.splice(index-1, 0, oldList[index] );// 当前返回 ['内容2','内容1','内容3','内容4','内容5']
this.sortList = [...newList]
},
列表项下移 同理
sortDown (index) {
let oldList = [...this.sortList];
let newList = [...this.sortList];
newList.splice(index, 1);
newList.splice(index+1, 0, oldList[index] );
this.sortList = [...newList]
},