ant design vue表格行内编辑时,点击取消返回之前的数据

编辑的时候先把数据存起来

// 编辑
edit(key) {
      const newData = [...this.data]
      const target = newData.filter(item => key === item.key)[0]
      this.editingKey = key
      if (target) {
      // 把target 存入editingLine中
        this.editingLine = { ...target }
        target.editable = true
        this.data = newData
      }
    },

点击取消editingLin赋给this.data[i]

 // 取消
    cancel(key) {
      for (let i = 0; i < this.data.length; i++) {
        if (key === this.data[i].key) {
            this.data[i] = this.editingLine
            this.editingKey = ''
            break
          }
      }
      const newData = [...this.data]
      const target = newData.filter(item => key === item.key)[0]
      this.editingKey = ''
      if (target) {
        delete target.editable
        this.data = newData
      }
    },
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于Ant Design Vue中的表格编辑,你可以使用Table组件的editable属性来实现。首先,设置editable为true,然后在columns中定义需要编辑的列,并指定相应的编辑器组件。 以下是一个示例代码,展示如何在Ant Design Vue中实现表格编辑: ```vue <template> <a-table :columns="columns" :data-source="data" :row-key="record => record.id"> <template slot="name" slot-scope="{ record, index }"> <a-input v-model="record.name" :disabled="!record.editable" /> </template> <template slot="email" slot-scope="{ record, index }"> <a-input v-model="record.email" :disabled="!record.editable" /> </template> <template slot="operation" slot-scope="{ record, index }"> <a-button @click="handleEdit(record)">编辑</a-button> <a-button @click="handleSave(record)">保存</a-button> <a-button @click="handleCancel(record)">取消</a-button> </template> </a-table> </template> <script> export default { data() { return { data: [ { id: 1, name: 'John', email: 'john@example.com', editable: false }, { id: 2, name: 'Jane', email: 'jane@example.com', editable: false }, ], columns: [ { title: '姓名', dataIndex: 'name' }, { title: '邮箱', dataIndex: 'email' }, { title: '操作', dataIndex: 'operation' }, ], }; }, methods: { handleEdit(record) { record.editable = true; }, handleSave(record) { record.editable = false; // 在这里可以进行保存操作,例如向后端提交数据 }, handleCancel(record) { record.editable = false; // 在这里可以进行取消操作,例如恢复原始数据 }, }, }; </script> ``` 上述代码中,我们使用了Ant Design Vue的Table组件,通过设置editable属性为true来启用行编辑功能。每个需要编辑的列都使用了具名插槽,并使用a-input组件作为编辑器。 在表格中,我们还添加了三个操作按钮:编辑、保存和取消点击编辑按钮会将对应行的editable属性设置为true,使该行变为可编辑状态。点击保存按钮会将对应行的editable属性设置为false,并可以在handleSave方法中进行保存数据的操作。点击取消按钮会将对应行的editable属性设置为false,并可以在handleCancel方法中进行取消编辑的操作。 希望以上示例能帮助到你实现Ant Design Vue表格编辑功能。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值