Ant Design Vue 表格内编辑(附完整源码及效果图)

87 篇文章 4 订阅

效果图:

实现关键代码就是表单的 columns 属性对象下标的 scopedSlots:

scopedSlots: {
   customRender: ''
}

 实现完整代码:

<template>
  <div>
    <div class="table-wrapper">
      <!--每个列的宽度必须比列名总长度大才能使表格所有列对齐,留一个列不设置宽度-->
      <a-table :rowSelection="{selectedRowKeys: selectedRowKeys}" :columns="columns" :dataSource="tableData" bordered
        size="middle" :loading="tableLoading">

        <template v-for="col in customRenderList" v-slot:[col]="text, record, index">

          <div :key="col">
            <!-- includes 用来判断当前的输入类型 -->
            <a-input :read-only="readonlyArr.includes(col)" placeholder="请输入"
              v-if="record.editable && inputScopedSlots.includes(col)" :value="text"
              @change="e => handleChange(e.target.value, record.key, col)" />

            <a-date-picker placeholder="请选择时间" v-else-if="record.editable && dateScopedSlots.includes(col)"
              :value="text" @change="onChangeDate($event, record.key, col)" />

            <a-select placeholder="请选择" style="display: block;"
              v-else-if="record.editable && selectScopedSlots.includes(col)" :value="text"
              @change="onChangeSelect($event, record.key, col)">
              <a-select-option value="1小时">1小时</a-select-option>
              <a-select-option value="2小时">2小时</a-select-option>
            </a-select>

            <span v-else>{{text}}</span>
          </div>

        </template>
      </a-table>
    </div>
  </div>
</template>

<script>
  import Moment from 'moment'

  export default {
    name: "demo",
    data() {
      return {
        columns: [{
            title: '开始时间',
            align: "center",
            dataIndex: 'beginTime',
            width: 120,
            scopedSlots: {
              customRender: 'beginTime'
            }
          },
          {
            title: '结束时间',
            align: "center",
            dataIndex: 'endTime',
            width: 120,
            scopedSlots: {
              customRender: 'endTime'
            }
          },
          {
            title: '工时数',
            align: "center",
            dataIndex: 'workingHours',
            width: 120,
            scopedSlots: {
              customRender: 'workingHours'
            }
          },
          {
            title: '工作内容',
            align: "center",
            dataIndex: 'jobContent',
            width: 120,
            scopedSlots: {
              customRender: 'jobContent'
            }
          },
          {
            title: '产出内容上传',
            align: "center",
            dataIndex: 'produceUrl',
            width: 120,
            scopedSlots: {
              customRender: 'produceUrl'
            }
          }
        ],
        // 表格数据
        tableData: [{
          key: '',
          beginTime: '2019-12-17',
          endTime: '2019-12-17',
          workingHours: '1小时',
          jobContent: '123',
          produceUrl: '12421',
          editable: true
        }],

        // 每一列的插槽名 - 表格行内编辑用
        customRenderList: ['beginTime', 'endTime', 'workingHours', 'jobContent', 'produceUrl'],
        // 用来匹配插槽中显示input框
        inputScopedSlots: ['jobContent', 'produceUrl'],
        // 用来匹配插槽中显示date框
        dateScopedSlots: ['beginTime','endTime'],
        // 用来匹配插槽中显示select框
        selectScopedSlots: ['workingHours'],
        // 对于某些自动赋值的input框设为 只读
        readonlyArr: [''],

        // 表格loading
        tableLoading: false,
        // 表格选中key
        selectedRowKeys: []
      }
    },
    methods: {
      // input 输入change
      handleChange(value, rowKey, colName) {
        const newData = [...this.tableData];
        const target = newData.filter(item => rowKey === item.key)[0];
        if (target) {
          target[colName] = value;
          this.tableData = newData;
        }
      },
      // 日期框 change
      onChangeDate($event, rowKey, colName) {
        const newData = [...this.tableData];
        const target = newData.filter(item => rowKey === item.key)[0];
        if (target) {
          target[colName] = this.$dateformat($event, 'isoDate');
          this.tableData = newData;
        }
      },
      // select 框 change
      onChangeSelect($event, rowKey, colName) {
        const newData = [...this.tableData];
        const target = newData.filter(item => rowKey === item.key)[0];
        if (target) {
          target[colName] = $event;
          // 根据select框的值自动带出某个input的值 - 因为第三列列名为c
          $event === '1小时' ? target.c = '根据1小时带出的值' : target.c = '根据2小时带出的值'
          this.tableData = newData;
        }
      }
    }
  }
</script>

  • 5
    点赞
  • 28
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
对于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表格编辑功能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a_靖

对你有帮助吗?打赏鼓励一下?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值