antd Vue可编辑table表格,输入框,下拉框,可添加删除

在这里插入图片描述

<template>
  <a-table :columns="columns" :data-source="data" bordered>
    <template
      v-for="col in ['name', 'time', 'workOrNo']"
      :slot="col"
      slot-scope="text, record, index"
    >
      <div :key="col">
        <a-input
          placeholder="请输入名称"
          v-if="col == 'name'"
          style="margin: -5px 0"
          v-model="record.name"
          :value="text"
        />
        <a-date-picker
          placeholder="请选择时间"
          v-model="record.time"
          :format="YYYY - MM - DD"
          v-if="col == 'time'"
        />
        <a-select
          placeholder="请选择"
          v-if="col == 'workOrNo'"
          v-model="record.workOrNo"
          style="width: 120px"
        >
          <a-select-option value="Yes"> Yes </a-select-option>
          <a-select-option value="No"> No </a-select-option>
        </a-select>
        <!-- <template v-else>
          {{ text }}
        </template> -->
      </div>
    </template>
    <template slot="operation" slot-scope="text, record, index">
      <div class="editable-row-operations">
        <span>
          <a-button
            type="primary"
            shape="circle"
            icon="plus"
            @click="() => addItem(record.key)"
          />
          <a-button
            type="primary"
            shape="circle"
            icon="minus"
            @click="() => deleteItem(record.key)"
          />
        </span>
      </div>
    </template>
  </a-table>
</template>
<script>
const columns = [
  {
    title: 'name',
    dataIndex: 'name',
    width: '25%',
    scopedSlots: { customRender: 'name' },
  },
  {
    title: 'time',
    dataIndex: 'time',
    width: '15%',
    scopedSlots: { customRender: 'time' },
  },
  {
    title: 'workOrNo',
    dataIndex: 'workOrNo',
    width: '40%',
    scopedSlots: { customRender: 'workOrNo' },
  },
  {
    title: 'operation',
    dataIndex: 'operation',
    scopedSlots: { customRender: 'operation' },
  },
]

const data = []
data.push({
  key: data.length,
  name: '',
  time: '',
  workOrNo: 'No',
})
export default {
  data () {
    this.cacheData = data.map(item => ({ ...item }))
    return {
      key: 0,
      data,
      columns,
      editingKey: ''
    }
  },
  methods: {
    addItem (key) {
      console.log(key)
      this.data.splice(key + 1, 0, {
        key: '',
        name: '',
        time: '',
        workOrNo: 'No',
      })
      this.data.forEach((item, index) => {
        item.key = index
      })
      alert('所有的数据直接收集到data里面了,查看vue工具可以看收集的数据,给后台传参数的时候把这个数据处理一下,转换为后台需要的格式')
    },
    deleteItem (key) {
      console.log(key)
      if (this.data.length === 1) {
        return
      }
      this.data.splice(key, 1)
      this.data.forEach((item, index) => {
        item.key = index
      })
    }
  },
};
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>


  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值