vue 表格删除功能结合filter更新对象数组

首先: filter方法的使用可以参考: https://blog.csdn.net/weixin_41615439/article/details/108661807
使用filter操作对象数组,可以减少不必要的请求;如果不是对象数组,那filter方法是没有改变原数组的。
1、首先,表格数据我已经拿到
在这里插入图片描述
2、点击删除,逻辑代码如下:

// 删除表格某个数据
    del(id) {
      // 判断是多选还是单选
      let delType = id instanceof Array
      let length = 0
      let changeId = ''
      if(delType) {
        // 多条
        length = id.length
        // 数组转字符串
        changeId = id.join(',')
      } else {
        changeId = id
      }
      this.$confirm({
        title: delType? `您确定要删除这${length}个站点吗?`: '您确定要删除该站点吗?',
        content: '删除后不可恢复哦',
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          // 调用删除接口
          delData(changeId)
            .then(res => {
              if(res.data.code === 0) {
                // 删除成功更新数据
                if(delType) {
                  // 如果是多选,遍历id数组
                  id.forEach(item => {
                    // 通过选中的id与原对象数组的id判断,不相同则留着
                    this.tableData = this.tableData.filter(it => it.id != item)
                  })
                } else {
                  // 单选则直接把选中的id和原对象数组的id比较
                  this.tableData = this.tableData.filter(it => it.id != changeId)
                }
              }
            })
        },
        onCancel() {
          // 关闭提示
        },
      });
    },

3、运行后保错:
在这里插入图片描述
4、修改的代码:

// 删除表格某个数据
    del(id) {
      // 解决报未定义的错
      let that = this
      // 判断是多选还是单选
      let delType = id instanceof Array
      let length = 0
      let changeId = ''
      if(delType) {
        // 多条
        length = id.length
        // 数组转字符串
        changeId = id.join(',')
      } else {
        changeId = id
      }
      that.$confirm({
        title: delType? `您确定要删除这${length}个站点吗?`: '您确定要删除该站点吗?',
        content: '删除后不可恢复哦',
        okText: '确定',
        okType: 'danger',
        cancelText: '取消',
        onOk() {
          // 调用删除接口
          delData(changeId)
            .then(res => {
              if(res.data.code === 0) {
                // 删除成功更新数据
                if(delType) {
                  // 如果是多选,遍历id数组
                  id.forEach(item => {
                    // 通过选中的id与原对象数组的id判断,不相同则留着
                    that.tableData = that.tableData.filter(it => it.id != item)
                  })
                } else {
                  // 单选则直接把选中的id和原对象数组的id比较
                  that.tableData = that.tableData.filter(it => it.id != changeId)
                }
              }
            })
        },
        onCancel() {
          // 关闭提示
        },
      });
    },

主要是增加了

let  that = this

只因热爱,愿迎万难

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue提供了一个Table组件,可以实现带单元格编辑功能表格。以下是一个实现新增、删除、编辑功能的示例代码: ```html <template> <div> <a-button type="primary" @click="handleAdd">新增</a-button> <a-table :columns="columns" :dataSource="data"> <template v-for="col in columns" #[col.dataIndex]="{ text, record }"> <template v-if="editingKey === record.key && col.editable"> <a-input v-model:value="text" @pressEnter="handleSave(record.key)" /> </template> <template v-else> {{ text }} </template> </template> <template #action="{ text, record }"> <span> <a v-if="editingKey !== record.key" @click="handleEdit(record.key)">编辑</a> <a-divider type="vertical" /> <a v-if="editingKey !== record.key" @click="handleDelete(record.key)">删除</a> <a-divider type="vertical" /> <a v-if="editingKey === record.key" @click="handleSave(record.key)">保存</a> <a-divider type="vertical" /> <a v-if="editingKey === record.key" @click="handleCancel">取消</a> </span> </template> </a-table> </div> </template> <script> export default { data() { return { data: [ { key: '1', name: 'John Brown', age: 32, address: 'New York No. 1 Lake Park' }, { key: '2', name: 'Joe Black', age: 42, address: 'London No. 1 Lake Park' }, { key: '3', name: 'Jim Green', age: 32, address: 'Sidney No. 1 Lake Park' }, { key: '4', name: 'Jim Red', age: 32, address: 'London No. 2 Lake Park' } ], columns: [ { title: '姓名', dataIndex: 'name', editable: true }, { title: '年龄', dataIndex: 'age', editable: true }, { title: '住址', dataIndex: 'address', editable: true }, { title: '操作', dataIndex: 'action', slots: { customRender: 'action' } } ], editingKey: '' } }, methods: { handleAdd() { const newData = { key: Date.now().toString() } this.data = [...this.data, newData] this.editingKey = newData.key }, handleDelete(key) { this.data = this.data.filter(item => item.key !== key) }, handleEdit(key) { this.editingKey = key }, handleSave(key) { const index = this.data.findIndex(item => item.key === key) const item = this.data[index] this.data.splice(index, 1, { ...item, ...this.$refs.table.getRecordByKey(key) }) this.editingKey = '' }, handleCancel() { this.editingKey = '' } } } </script> ``` 在这个示例,Table组件使用了columns和dataSource属性来渲染表格。其,每个列都可以设置editable属性来指定是否可编辑。当某个单元格被点击时,根据editingKey和editable状态来判断是否需要显示编辑框。 新增功能通过在data数组添加新数据实现。编辑功能通过将editingKey设置为当前行的key来激活编辑框。保存功能通过更新data数组对应的数据来实现。删除功能则是通过过滤掉对应的数据实现。 以上代码仅供参考,具体实现方式可以根据实际需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值