{{item}}
上移
下移
置顶
// 上移
moveUp(index){
let nums = this.nums;
let temp = nums[index]
nums[index] = nums[index - 1]
nums[index - 1] = temp;
this.nums = [...nums];
},
// 下移
moveDown(index){
let nums = this.nums;
let temp = nums[index]
nums[index] = nums[index + 1]
nums[index + 1] = temp;
this.nums = [...nums];
},
// 置顶
moveTop(index){
let nums = [...this.nums]
let temp = nums[index]
let other = nums.filter((item,idx) => index != idx)
this.nums = [temp,...other]
},
// 上移
moveUp(index){
let nums = this.nums;
nums.splice(index, 1, ...nums.splice(index - 1, 1, nums[index]))
this.nums = [...nums];
},
// 下移
moveDown(index){
let nums = this.nums;
nums.splice(index, 1, ...nums.splice(index + 1, 1, nums[index]))
this.nums = [...nums];
},
// 置顶
moveTop(index){
let nums = [...this.nums]
this.nums = [...nums.splice(index,1),...nums]
},
此方法和方法二的区别再去,方法二第二次替换值使用 splice,而方法三是直接将第一次的 splice 的返回值赋值给当前位置。
// 上移
moveUp(index){
let nums = this.nums;
nums[index] = nums.splice(index - 1, 1, nums[index])[0]
this.nums = [...nums];
},
// 下移
moveDown(index){
let nums = this.nums;
nums[index] = nums.splice(index + 1, 1, nums[index])[0]
this.nums = [...nums];
},
// 置顶
moveTop(index){
let nums = [...this.nums]
this.nums = [...nums.splice(index,1),...nums]
},
// 上移
moveUp(index){
let nums = this.nums;
[nums[index], nums[index - 1]] = [nums[index - 1], nums[index]]
this.nums = [...nums];
},
// 下移
moveDown(index){
let nums = this.nums;
[nums[index], nums[index + 1]] = [nums[index + 1], nums[index]]
this.nums = [...nums];
},
// 置顶
moveTop(index){
let nums = [...this.nums]
this.nums = [...nums.splice(index,1),...nums]
}
move(type, nums, index, row={}){
if(!Array.isArray(nums)){
return nums
}
switch(type){
case 'up':
[nums[index], nums[index - 1]] = [nums[index - 1], nums[index]]
break;
case 'down':
[nums[index], nums[index + 1]] = [nums[index + 1], nums[index]]
break;
case 'top':
nums = [...nums.splice(index,1), ...nums]
break;
case 'bottom':
let val = nums.splice(index,1)[0]
nums = [...nums, val]
break;
}
return [...nums]
},
// 上移[up]、下移[down]、置顶[top]
moveRow(type, index, item = {}){
this.nums = this.move(type, this.nums, index, item)
},