vue项目中利用splice方法实现table行的上移下移

vue项目中利用splice方法实现table行的上移下移

实现效果
在这里插入图片描述

splice 方法

通过删除或替换现有元素或者原地添加新的元素来修改数组,并以数组形式返回被修改的内容。此方法会改变原数组。

array.splice(start, deleteCount, item1, item2,…)

start: 修改的开始位置
deleteCount:要移除或要替换的元素个数。(如果为0,则添加元素)
item1, item2, … :要添加进数组的元素,从start 位置开始。如果不指定,则 splice() 将只删除数组元素。

const months = ['Jan', 'March', 'April', 'June'];
months.splice(1, 0, 'Feb');
// inserts at index 1
console.log(months);
// output: Array ["Jan", "Feb", "March", "April", "June"]

months.splice(4, 1, 'May');
// replaces 1 element at index 4
console.log(months);
// output: Array ["Jan", "Feb", "March", "April", "May"]

months.splice(1, 3);
console.log(months);
// output: Array ["Jan",  "May"]

实现代码

template部分

<template #action="{ record, index }">
      <a-button type="link" class="action" @click="moveUp(record, index)" :disabled="index === 0">上移</a-button>
      <a-button type="link" class="action" @click="moveDown(record, index)" :disabled="index === tableData.length - 1">
        下移
      </a-button>
</template>

第一条和最后一条禁用上移下移

js部分

const moveUp = (record, index) => {
  if (index > 0) {
    let upDate = tableData[index - 1];
    tableData.splice(index - 1, 1);
    tableData.splice(index, 0, upDate);
  } else {
    message.error('已经是第一条,不可上移', 1);
  }
};
const moveDown = (record, index) => {
  if (index + 1 === tableData.value.length) {
    message.error('已经是最后一条,不可下移', 1);
  } else {
    let downDate = tableData.value[index + 1];
    tableData.splice(index + 1, 1);
    tableData.splice(index, 0, downDate);
  }
};
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值