前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >js之Ant Design Vue 表格内编辑

js之Ant Design Vue 表格内编辑

原创
作者头像
IT工作者
发布2022-05-16 18:15:27
2.8K0
发布2022-05-16 18:15:27
举报
文章被收录于专栏:程序技术知识程序技术知识

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

代码语言:javascript
复制
scopedSlots: {
   customRender: ''
}

 实现完整代码:

代码语言:javascript
复制
<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: '2022-05-16',
          endTime: '2022-05-16',
          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>

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档