AntD table行内增改

Html:

<a-table
               <!--
                 * 表格栏:
                 * columns: 表格列的配置描述
                 * rowKey: 表格行 key 的取值
                 * dataSource: 数据数组
                 * pagination: 分页器
                 * loading: 是否加载中
               -->
                :columns="columns"
                :dataSource="dataSource"
                rowKey="key"
                bordered
                :loading="memberLoading" //加载
                :pagination="false">
                <template v-for="col in ['caption', 'name', 'desc']" :slot="col" slot-scope="text, record">
                  <div :key="col">
                    <a-input
                      v-if="record.editable"
                      style="margin: -5px 0"
                      :value="text"
                      @change="e => handleChange(e.target.value, record, col)"
                    />
                    <template v-else>{{ text }}</template>
                  </div>
                </template>
                <template slot="handle" slot-scope="text, record, index">
                  <span v-if="record.editable">
                    <span v-if="record.isNew">
                      <a-button style="margin-right: 4px;" size="small" @click="handleSave(record)">添加</a-button>|
                      <a-button size="small" @click="handleAddCancle(record)">取消</a-button>|
                    </span>
                    <span v-else>
                      <a-button style="margin-right: 4px;" size="small" @click="handleSave(record)">保存</a-button>|
                      <a-button size="small" @click="handleEditCancle(record)">返回</a-button>|
                    </span>
                  </span>
                  <span v-else>
                    <a-button style="margin-right: 4px;" size="small" @click="handleEdit(record)">编辑</a-button>|
                    <a-popconfirm title="确定删除该属性吗" @confirm="handleDelete(record, index)">
                      <a-button size="small">删除</a-button>
                    </a-popconfirm>
                  </span>
                </template>
</a-table>

methods:

// table中input编辑时
handleChange (value, record, column) {
      // console.log(record)
        console.log('元数据属性编辑')
        const newData = [...this.dataSource]
        const target = newData.filter(item => record.id === item.id)[0]
        if (target) {
          target[column] = value
          this.dataSource = newData
        }
      }
 },
// 按编辑按钮时
handleEdit (record) {
        const newData = [...this.dataSource]
        const target = newData.filter(item => record.id === item.id)[0]
        if (target) {
          target.editable = true
          delete target.isNew
          this.dataSource= newData
        }
},
handleDelete (record, index) {
      // 模拟网络请求、卡顿 800ms
      this.memberLoading = true
      new Promise((resolve) => {
        setTimeout(() => {
          resolve({ loop: false })
        }, 800)
      }).then(() => {
          // 删除元数据属性
          this.axios.delete('/reports/metaproperty/' + record.id).then((res) => {
            console.log(res)
            this.memberLoading = false
          })
      })
},
// table添加行
add () {
      // 数组头插值
      this.dataSource.unshift({
        id: '123456'
        caption: '',
        name: '',
        desc: '',
        propertyType: this.attrSelectType,
        // 下面属性控制表格出现input框
        editable: true,
        isNew: true
      })
},
// 添加取消
handleAddCancle (record) {
        const newData = [...this.dataSource]
        const target = newData.filter(item => item.id === record.id)[0]
        if (target) {
          Object.assign(target, this.cacheData.filter(item => record.id === item.id)[0])
          this.dataSource = newData.slice(1)
          delete target.editable
        }
},
// 编辑取消
handleEditCancle (record) {
        const newData = [...this.dataSource]
        const target = newData.filter(item => item.id === record.id)[0]
        if (target) {
          Object.assign(target, this.cacheData.filter(item => record.id === item.id)[0])
          delete target.editable
          this.dataSource= newData
        }
},

注意: 需要先在mounted初始化(取消的时候需要回复原始状态时使用)

this.cacheData = this.dataSource.map(item => ({ ...item }))
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 这是16年5月份编辑的一份比较杂乱适合自己观看的学习记录文档,今天18年5月份再次想写文章,发现简书还为我保存起的...
    Jenaral阅读 3,172评论 2 9
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,681评论 1 32
  • 一:什么是闭包?闭包的用处? (1)闭包就是能够读取其他函数内部变量的函数。在本质上,闭包就 是将函数内部和函数外...
    xuguibin阅读 10,067评论 1 52
  • 感恩下班了去燕子家——真是可以啊——有吃的玩的——边聊天边玩手机——很惬意—— 感恩妈妈开视频来——有您真好! 感...
    灿宝儿阅读 114评论 0 0
  • 倒计时283天 啊啊啊啊啊,忘记更了 今天几乎一天课,所以没太多自主时间学习,中午饭后看了王江涛的一个句子,翻译了...
    吉吉去学习阅读 169评论 0 1

友情链接更多精彩内容