vue实现数组上移下移置顶置底

2018-06-21  本文已影响0人  简小咖

js方法

var 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;
};

在vue中使用

upTr(index) { // 上移
      if (index === 0) {
        return
      }
      swapItems(this.myAppList, index, index - 1);
},
downTr(index) { // 下移
      if (index === this.myAppList.length - 1) {
        return
      }
     swapItems(this.myAppList, index, index + 1);
    }
上一篇 下一篇

猜你喜欢

热点阅读