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>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/635558.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

浏览器的下载行为基本原理

浏览器解析 在使用浏览器访问某些资源时&#xff0c;有些资源是直接下载有些资源是直接打开。例如前端的html&#xff0c;xml&#xff0c;css&#xff0c;图片等资源都是直接打开&#xff0c;而txt&#xff0c;excel等文件是直接下载。那么如何控制访问一个资源时是下载文件还…

stm32学习-光敏传感器控制蜂鸣器

接线 GPIO配置 初始化GPIO 1.使用RCC开启GPIO时钟 void RCC_APB2PeriphClockCmd(uint32_t RCC_APB2Periph, FunctionalState NewState); 作用&#xff1a;外设时钟控制(根据外设连接的总线选择要开启的时钟&#xff09; RCC_AHBPeriph/RCC_APB2Periph/RCC_APB1Periph&#x…

.NET Core Web Api Swagger运行异常

遇到的问题 因为新增了一个控制器方法&#xff0c;从而导致在运行Swagger的时候直接报错&#xff0c;异常如下&#xff1a; SwaggerGeneratorException: Conflicting method/path combination "POST api/UserOperationExample" for actions - WebApi.Controllers.Us…

GMSL图像采集卡,适用于无人车、自动驾驶、自主机器、数据采集等场景,支持定制

基于各种 系列二代 G MS L 图像采集卡&#xff08;以下简称 二代图像采集卡&#xff09;是一款自主研发的一款基于 F P G A 的高速图像产品&#xff0c;二代图像采集卡相比一代卡&#xff0c;由于采用PCIe G en 3 技术&#xff0c;速度和带宽都相应的有了成 倍的提高。该图像…

开源与闭源AI模型的对决:数据隐私、商业应用与社区参与

引言 在人工智能&#xff08;AI&#xff09;领域&#xff0c;模型的发展路径主要分为“开源”和“闭源”两条。这两种模型在数据隐私保护、商业应用以及社区参与与合作方面各有优劣&#xff0c;是创业公司、技术巨头和开发者们必须仔细权衡的重要选择。那么&#xff0c;面对这些…

【经验技巧】谷歌高级搜索语法

谷歌高级搜索语法是一些特殊的搜索指令&#xff0c;可以在谷歌搜索框中使用以帮助您更准确地找到您需要的信息。以下是一些常用的谷歌高级搜索语法&#xff1a; 搜索特定词组&#xff1a;用引号将词组括起来&#xff0c;例如&#xff1a;“人工智能” 排除特定词语&#xff1a…

前端 基础 综合案例 二 注册页面( 简单版)A

案例示例 &#xff1a; 案例 分析 &#xff1a; 我们将 上示网页&#xff0c;拆成两个部分进行分析&#xff1a; 很显然&#xff0c;网页 第一行&#xff0c;是标题&#xff08;青春不常在&#xff0c;抓紧谈恋爱&#xff09;&#xff0c; 我们就用 h4 去完成&#xff1b…

若依nodejs版本过高问题解决方案

由于nodejs版本过高,可能会导致vue-cli项目运行报错。 目录 方法1:每次启动项目前,输入配置命令 方法2:修改package.js

Study--Oracle-03-Oracle19C--RAC集群部署

一、硬件信息及配套软件 1、硬件设置 RAC集群虚拟机&#xff1a;CPU:2C、内存&#xff1a;9G、操作系统&#xff1a;30G、数据库安装目录&#xff1a;100G 数据存储&#xff1a;50G &#xff08;10G*5&#xff09; 共享存储&#xff1a;2G &#xff08;1G*2&#xff09; 2…

基于深度学习PET/CT放射学的预后价值:未来在晚期鼻咽癌个体化诱导化疗中的潜在作用 | 文献速递-深度学习结合影像组学

Title 题目 Prognostic Value of Deep Learning PET/CT-BasedRadiomics: Potential Role for Future IndividualInduction Chemotherapy in AdvancedNasopharyngeal Carcinoma 基于深度学习PET/CT放射学的预后价值&#xff1a;未来在晚期鼻咽癌个体化诱导化疗中的潜在作用 0…

HCIP-Datacom-ARST自选题库__MPLS简答【4道题】

1.如图所示&#xff0c;R1、R2、R3、R4处于同一个MPLS域&#xff0c;且设备之间采用LDP分配MPLS标签&#xff0c;R4为4.4.4.0/24这条FEC的EgressLSR。若想实现R1访问4.4.4.0/24时&#xff0c;R4不需要查询标签表但能够了解该数据的转发优先级&#xff0c;则R3对于该FEC的出标签…

新媒体时代,LCD电子价签赋予零售场景新活力

近年来&#xff0c;全球企业迅速掀起了数字化转型的浪潮&#xff0c;加速了新零售科技的发展与应用。在实体零售门店中&#xff0c;商品货架显示逐渐趋向智能化和多样化。然而&#xff0c;在信息传播日益碎片化和视频化的时代&#xff0c;零售门店如何更有效地吸引消费者的注意…

苹果CMS:采集参数设置

我们安装苹果CMS参考苹果cms&#xff1a;介绍及安装&#xff0c;安装好设置采集器苹果CMS&#xff1a;怎么采集&#xff0c;配置采集深度&#xff08;即爬取链接的层次&#xff09;&#xff0c;以及是否遵循robots.txt协议。采集插件通常需要用户自定义匹配规则来解析目标网页内…

如何轻松访问 Android 手机和平板电脑上的内部存储

概括 在数字设备领域&#xff0c;我们的智能手机充当虚拟金库&#xff0c;在其范围内存储个人数据、珍贵记忆和重要信息的宝库。因此&#xff0c;我们将指导您如何访问 Android 上的内部存储&#xff0c;确保您可以安全、轻松地检查内部文件系统并管理文件。同时&#xff0c;您…

深入解读HTTP状态码:分类、含义、应用场景与故障排查指南

HTTP状态码作为超文本传输协议(HTTP)响应的重要组成部分,为客户端与服务器之间的交互提供了清晰的状态反馈。本文将全面展开对HTTP状态码的深入解读,涵盖其分类、具体含义、典型应用场景以及在故障排查中的实用价值,旨在帮助开发者与运维人员更好地理解和应对各类HTTP响应…

windows11下安装VC6【VC6.0(VC++6.0】与Dev C++并且跑.c与.cpp后缀文件视频教程官方笔记【所用资料均提供安装包与下载地址】

背景&#xff1a; 我们大学第一次学C语言的时候&#xff0c;大部分老师会选择VC6这个编辑器。 但由于很多人是新手&#xff0c;第一次上大学学C语言&#xff0c; 老师要求VC6.0&#xff08;VC6.0&#xff09;写C语言跑程序 可能很多人还是第一次接触电脑&#xff0c; 需要安…

xcode配置快速打开终端命令行工具教程

以往我们使用idea编辑器或者vscode编辑器的时候&#xff0c;我们可以快速的在编辑器下面打开终端进行相关的操作&#xff0c;但是在xcode里面却没有这么方便的功能按钮&#xff0c;真的不是很习惯&#xff0c;所以这次就来给xcode配置这么一个方便的功能。 idea的Terminal 这…

torch配置时出现问题

torch配置时出现如下问题&#xff1a; 可能原因&#xff1a; 1、下载的whl文件中python版本与本机上的python版本不匹配&#xff1b; 2、上图中的文件是64位的&#xff0c;而本机python是32位的&#xff0c;也无法匹配&#xff1b; 3、cuda的版本不匹配。

科技赋能,打破视障人士的沟通壁垒

在探索如何增强盲人群体的社会参与度与幸福感的旅程中&#xff0c;盲人社交能力提升策略成为了不容忽视的一环。随着科技的不断进步&#xff0c;像“蝙蝠避障”这样的辅助软件&#xff0c;不仅在日常出行中为盲人提供了实时避障和拍照识别的便利&#xff0c;也在无形中为他们拓…