Anti Desgin Vue 实现 表格可编辑、新增、删除功能

1、效果图
新增:
在这里插入图片描述
在这里插入图片描述
删除:
在这里插入图片描述
在这里插入图片描述
修改:
在这里插入图片描述
代码:

<template>
  <div>
    <button @click="add">添加</button>
    <span style="margin-left: 8px">
      <template v-if="hasSelected">{{ `Selected ${selectedRowKeys.length} items` }}</template>
    </span>
    <a-table
      :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      :columns="columns"
      :data-source="data"
      bordered
    >
      <template v-for="col in ['name','barcode','price','num','amount','type','standard']" :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.key, col)"
          />
          <template v-else>{{ text }}</template>
        </div>
      </template>

      <template slot="operation" slot-scope="text, record">
        <div class="editable-row-operations">
          <span v-if="record.editable">
            <a @click="() => save(record.key)">保存</a>
            <a-popconfirm
              title="确定取消?"
              okText="确定"
              cancelText="取消"
              @confirm="() => cancel(record.key)"
            >
              <a>取消</a>
            </a-popconfirm>
          </span>
          <span v-else>
            <a :disabled="editingKey !== ''" @click="() => edit(record.key)">修改</a>
            <a  @click="() =>deleteItem(record.key)">删除</a>
          </span>
        </div>
      </template>
    </a-table>
  </div>
</template>
<script>
const columns = [
  {
    title: "样品名称",
    dataIndex: "name",
    // width: "25%",
    align: 'center',
    scopedSlots: { customRender: "name" }
  },


  {
    title: '规格型号',
    dataIndex: 'barcode',
    align: 'center',
    scopedSlots: { customRender: "barcode" }
  },

  {
    title: '数量',
    dataIndex: 'price',
    align: 'center',
    scopedSlots: { customRender: "price" }

  },
  {
    title: '生成批号',
    dataIndex: 'num',
    align: 'center',
    scopedSlots: { customRender: "num" }
  },
  {
    title: '生产单位',
    dataIndex: 'amount',
    align: 'center',
    scopedSlots: { customRender: "amount" }
  },

  {
    title: '检验类别',
    dataIndex: 'type',
    align: 'center',
    scopedSlots: { customRender: "type" }
  },

  {
    title: '试验标准',
    dataIndex: 'standard',
    align: 'center',
    scopedSlots: { customRender: "standard" }
  },

  {
    title: "操作",
    dataIndex: "operation",
    align: 'center',
    scopedSlots: { customRender: "operation" }
  }

];

let data = [];
// 数组创建时候的下标
var numbe = 0;
export default {
  data() {
    this.cacheData = data.map(item => ({ ...item }));
    return {
      data,
      columns,
      editingKey: "",
      selectedRowKeys: []
    };
  },
  methods: {
    add() {
      data.push({
        key: numbe.toString(),
        name: "hah",
        // 规格型号
        barcode:'E200',
        price: "1",
        num: "1",
        amount: "xxx公司",
        type: "1",
        standard: "国家标准",
      });
      numbe++;
      console.log(data);
      this.cacheData = data.map(item => ({ ...item }));
    },
    handleChange(value, key, column) {
      const newData = [...this.data];
      const target = newData.filter(item => key === item.key)[0];
      if (target) {
        target[column] = value;
        this.data = newData;
      }
    },
    edit(key) {
      const newData = [...this.data];
      const target = newData.filter(item => key === item.key)[0];
      this.editingKey = key;
      if (target) {
        target.editable = true;
        this.data = newData;
      }
    },
    deleteItem(key){
      console.log('删除前',this.data);
      console.log('cacheData',this.cacheData)
      console.log('删除',key)
      const newData = [...this.data];
      this.data = newData.filter(item=>item.key !== key)
      this.cacheData = this.cacheData.filter(item=>item.key !== key)
      data=this.data
      console.log('删除后',this.data);
      this.editingKey = "";
    },
    save(key) {
      const newData = [...this.data];
      console.log('newData',newData)
      const newCacheData = [...this.cacheData];
      console.log('newCacheData',newCacheData)
      const target = newData.filter(item => key === item.key)[0];
      console.log('target',target)
      const targetCache = newCacheData.filter(item => key === item.key)[0];
      console.log('targetCache',targetCache)

      if (target && targetCache) {
        delete target.editable;
        this.data = newData;
        Object.assign(targetCache, target);
        this.cacheData = newCacheData;
      }
      this.editingKey = "";
    },
    cancel(key) {
      const newData = [...this.data];
      const target = newData.filter(item => key === item.key)[0];
      this.editingKey = "";
      if (target) {
        Object.assign(
          target,
          this.cacheData.filter(item => key === item.key)[0]
        );
        delete target.editable;
        this.data = newData;
      }
    },

    onSelectChange(selectedRowKeys) {
      console.log("selectedRowKeys changed: ", selectedRowKeys);
      this.selectedRowKeys = selectedRowKeys;
    }
  },
  computed: {
    hasSelected() {
      return this.selectedRowKeys.length > 0;
    }
  }
};
</script>
<style scoped>
.editable-row-operations a {
  margin-right: 8px;
}
</style>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

一叶飘零晋

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值