vue实现数组上移下移置顶置底
swapItems: function(arr, index1, index2, direction) {
if(direction == 'up'){
//置顶
arr.unshift(arr[index1]);
arr.splice(index1+1, 1);
return arr;
}
if(direction == 'down'){
//置底
arr.push(arr[index1]);
arr.splice(index1,1);
return arr;
}
arr[index1] = arr.splice(index2, 1, arr[index1])[0];
return arr;
},
,
up(k) {
if(k == 0) {
layer.msg('已经是最顶部了');
return;
}
this.datas = this.swapItems(this.datas, k, k - 1);
var datas = this.datas;
datas.forEach(function(e, k) {
datas[k].sort_order = k;
})
},
down(k) {
var index = this.datas.length - 1;
if(k == index) {
layer.msg('已经是最底部了');
return;
}
this.datas = this.swapItems(this.datas, k, k + 1);
var datas = this.datas;
datas.forEach(function(e, k) {
datas[k].sort_order = k;
})
},