*初级小白*~ 2022-07-01 18:11 采纳率: 97.9%
浏览 175
已结题

antdesignUI组件可编辑表格

使用antdesign可编辑表格,把input输入框改为日期选择器,点击编辑,报错,怎么解决


<template>
  <a-table :columns="columns" :data-source="dataSource" bordered>
    <template #bodyCell="{ column, text, record }">
      <template v-if="['name', 'age', 'address'].includes(column.dataIndex)">
        <div>
           <a-date-picker v-model:value="editableData[record.key][column.dataIndex]" v-if="editableData[record.key]" />
          <template v-else>
            {{ text }}
          </template>
        </div>
      </template>
      <template v-else-if="column.dataIndex === 'operation'">
        <div class="editable-row-operations">
          <span v-if="editableData[record.key]">
            <a-typography-link @click="save(record.key)">Save</a-typography-link>
            <a-popconfirm title="Sure to cancel?" @confirm="cancel(record.key)">
              <a>Cancel</a>
            </a-popconfirm>
          </span>
          <span v-else>
            <a @click="edit(record.key)">Edit</a>
          </span>
        </div>
      </template>
    </template>
  </a-table>
</template>
<script>
import { cloneDeep } from 'lodash-es';
import { defineComponent, reactive, ref } from 'vue';
const columns = [{
  title: 'name',
  dataIndex: 'name',
  width: '25%',
}, {
  title: 'age',
  dataIndex: 'age',
  width: '15%',
}, {
  title: 'address',
  dataIndex: 'address',
  width: '40%',
}, {
  title: 'operation',
  dataIndex: 'operation',
}];
const data = [];

for (let i = 0; i < 100; i++) {
  data.push({
    key: i.toString(),
    name: `Edrward ${i}`,
    age: 32,
    address: `London Park no. ${i}`,
  });
}

export default defineComponent({
  setup() {
    const dataSource = ref(data);
    const editableData = reactive({});

    const edit = key => {
      editableData[key] = cloneDeep(dataSource.value.filter(item => key === item.key)[0]);
      console.log( editableData[key],' editableData[key]')
    };

    const save = key => {
      console.log( editableData[key],' save   editableData[key]')
      Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
      console.log(Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]))
      delete editableData[key];
    };

    const cancel = key => {
      delete editableData[key];
    };

    return {
      dataSource,
      columns,
      editingKey: '',
      editableData,
      edit,
      save,
      cancel,
    };
  },

});
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-07-01 21:42
    关注

    datepicker需要用dayjs数据,改成下面这样,注意引入dayjs。然后判断是date日期列再使用a-data-picker编辑器,其他普通字符用a-input

    <template>
      <a-table :columns="columns" :data-source="dataSource" bordered>
        <template #bodyCell="{ column, text, record }">
          <template v-if="['name', 'date', 'address'].includes(column.dataIndex)">
            <div>
              <a-date-picker v-model:value="editableData[record.key][column.dataIndex]" v-if="editableData[record.key]&&column.dataIndex=='date'" />
              <a-input v-model:value="editableData[record.key][column.dataIndex]"  v-else-if="editableData[record.key]"  />
              <template v-else>
                {{ text }}
              </template>
            </div>
          </template>
          <template v-else-if="column.dataIndex === 'operation'">
            <div class="editable-row-operations">
              <span v-if="editableData[record.key]">
                <a-typography-link @click="save(record.key)">Save</a-typography-link>
                <a-popconfirm title="Sure to cancel?" @confirm="cancel(record.key)">
                  <a>Cancel</a>
                </a-popconfirm>
              </span>
              <span v-else>
                <a @click="edit(record.key)">Edit</a>
              </span>
            </div>
          </template>
        </template>
      </a-table>
    </template>
    <script>
    import { cloneDeep } from 'lodash-es';
    import { defineComponent, reactive, ref } from 'vue';
    import dayjs from 'dayjs';
    const columns = [{
      title: 'name',
      dataIndex: 'name',
      width: '15%',
    }, {
      title: 'date',
      dataIndex: 'date',
      width: '30%',
    }, {
      title: 'address',
      dataIndex: 'address',
      width: '35%',
    }, {
      title: 'operation',
      dataIndex: 'operation',
    }];
    const data = [];
     
    for (let i = 1; i < 10; i++) {
      data.push({
        key: i.toString(),
        name: `Edrward ${i}`,
        date: '2022-7-'+i,
        address: `London Park no. ${i}`,
      });
    }
     
    export default defineComponent({
      setup() {
        const dataSource = ref(data);
        const editableData = reactive({});
     
        const edit = key => {
          let edata=cloneDeep(dataSource.value.filter(item => key === item.key)[0]);
          edata.date=dayjs(edata.date,'YYYY-M-D');//转为moment格式
          
          editableData[key] = edata;
          console.log(editableData, editableData[key],` editableData[${key}]`)
          
        };
     
        const save = key => {
          editableData[key].date=editableData[key].date.format('YYYY-MM-DD')
          Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]);
          console.log(Object.assign(dataSource.value.filter(item => key === item.key)[0], editableData[key]))
          delete editableData[key];
        };
     
        const cancel = key => {
          delete editableData[key];
        };
     
        return {
          dataSource,
          columns,
          editingKey: '',
          editableData,
          edit,
          save,
          cancel
        };
      },
     
    });
    </script>
    <style scoped>
    .editable-row-operations a {
      margin-right: 8px;
    }
    </style>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月12日
  • 已采纳回答 7月4日
  • 创建了问题 7月1日

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。