Antv a-table 表格行/列合并,在合并后的td中使用插槽slot

news2024/9/22 7:32:26

【需求】

这次的表格需要实现行列合并,并且要在合并后的 td 中使用子组件或弹出弹窗,难点在于:

1. 根据提供的data,自行判断是否合并项的 getRowspan方法 

2. customCell 、scopedSlots 冲突导致的子组件无法展示

(在最下方的参考资料中提供了关于这两个难点的解决方法,感谢两位作者。)

【template】

<template>
  <div style="margin: 20px;">
    <a-table :columns="columns" :data-source="data" bordered>
      <template slot="head3" slot-scope="text"><editable-cell :text="text" /></template>
      <template slot="func4" slot-scope="text, record">
        <a-button v-if="record.func4.length == 0" type="link" @click="handleChoose(record.key)">
          <span>请选择item</span>
        </a-button>
        <a-tooltip v-else placement="topLeft">
          <template slot="title">
            <span style="margin-right: 8px;" v-for="item in record.func4" :key="item">{{ item }}</span>
          </template>
          <a-button type="link" @click="handleChoose(record.key)">
            <span>已选择{{ record.func4.length }}项</span>
          </a-button>
        </a-tooltip>
      </template>
      <template slot="head5" slot-scope="text, record">
        <div v-if="record.slot5.isEdit">
          <editable-cell :text="record.slot5.value" />
        </div>
        <div v-else>
          {{ record.slot5.value }}
        </div>
      </template>
      <template slot="slot6" slot-scope="text">
        <editable-cell :text="text" />
      </template>
      <template slot="action" slot-scope="text,record">
        <div>
          <a-button type="link">link {{ record.key }}</a-button>
        </div>
      </template>
    </a-table>
  </div>
</template>

【script】

<script>
import EditableCell from '@/components/EditableCell';

const data = [
  {
    key: '1',
    name1: 'item1.1',
    name2: 'text1',
    slot3: 'slot1',
    func4: [],
    slot5: {
      isEdit: false,
      value: 'text'
    },
    slot6: 'slot',
    head7: 'text',
  },
  {
    key: '2',
    name1: 'item1.1',
    name2: 'text1',
    slot3: 'slot1',
    func4: [],
    slot5: {
      isEdit: false,
      value: 'text'
    },
    slot6: 'slot',
    head7: 'text',
  },
  {
    key: '3',
    name1: 'item2.1',
    name2: 'text2',
    slot3: 'slot2',
    func4: ['item1', 'item2', 'item3'],
    slot5: {
      isEdit: false,
      value: 'text'
    },
    slot6: 'slot',
    head7: 'text',
  },
  {
    key: '4',
    name1: 'item2.1',
    name2: 'text2',
    slot3: 'slot2',
    func4: ['item1', 'item2', 'item3'],
    slot5: {
      isEdit: true,
      value: 'slot'
    },
    slot6: 'slot',
    head7: 'text',
  },
];

export default {
  components: { EditableCell },
  data() {
    return {
      data,
      columns: [
        {
          title: 'head1',
          dataIndex: 'name1',
          width: 160,
          customCell: (record, rowIndex, column) => {
            return {
              style: { display: this.rowSpanArr[rowIndex] === 0 ? 'none' : undefined },
              attrs: {
                rowSpan: this.rowSpanArr[rowIndex],
              }
            }
          },
        },
        {
          title: 'head2',
          dataIndex: 'name2',
          width: 120,
          customCell: (record, rowIndex, column) => {
            return {
              style: { display: this.rowSpanArr[rowIndex] === 0 ? 'none' : undefined },
              attrs: {
                rowSpan: this.rowSpanArr[rowIndex],
              }
            }
          },
        },
        {
          title: 'head3-slot',
          width: 200,
          dataIndex: 'slot3',
          scopedSlots: { customRender: 'head3' },
          customCell: (record, rowIndex, column) => {
            return {
              style: { display: this.rowSpanArr[rowIndex] === 0 ? 'none' : undefined },
              attrs: {
                rowSpan: this.rowSpanArr[rowIndex],
              }
            }
          },
        },
        {
          title: 'head4-func',
          dataIndex: 'func4',
          scopedSlots: { customRender: 'func4' },
          customCell: (record, rowIndex, column) => {
            return {
              style: { display: this.rowSpanArr[rowIndex] === 0 ? 'none' : undefined },
              attrs: {
                rowSpan: this.rowSpanArr[rowIndex],
              }
            }
          },
        },
        {
          title: 'head5-slot-text',
          dataIndex: 'dataCenter',
          scopedSlots: { customRender: 'head5' },
        },
        {
          title: 'head6',
          dataIndex: 'slot6',
          scopedSlots: { customRender: 'slot6' },
        },
        {
          title: 'head7',
          dataIndex: 'head7',
        },
        {
          title: 'action',
          dataIndex: 'action',
          width: 100,
          scopedSlots: { customRender: 'action' }
        },
      ],
      rowSpanArr: [],
    };
  },
  computed: {},
  mounted() {
    this.rowSpanArr = this.getRowspan(this.data, 'name1') // 关键句,选一个会重复并且多项会一致的属性如 name1,作为判断是否合并的依据
  },
  watch: {

  },
  methods: {
    handleChoose(id) {
      console.log('请选择项,id:', id);      
    },
    getRowspan(dataScroce, filed) {
      let spanArr = [];
      let position = 0;

      dataScroce.forEach((item, index) => {
        if (index === 0) {
          spanArr.push(1);
          position = 0;
        } else {
          //需要合并的地方判断
          if (dataScroce[index][filed] === dataScroce[index - 1][filed]) {
            spanArr[position] += 1;
            spanArr.push(0);
          } else {
            spanArr.push(1);
            position = index;
          }
        }
      });
      return spanArr
    }
  }
};
</script>

【注】

import EditableCell from '@/components/EditableCell';

这个子组件是用官网的可编辑单元格这部分改的,链接:表格 Table - Ant Design Vue (antdv.com)

【参考资料】

Ant Design Vue实现a-table表格单元格合并(列/行合并)_a-table合并单元格-CSDN博客

antd的a-table表格中合并且使用插槽(使用customRender合并出现问题解决方案)_a-table customrender-CSDN博客

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Cesium 实战 - 自定义纹理材质 - 流动线(精灵线)

Cesium 实战 - 自定义纹理材质 - 流动线(精灵线) 核心代码完整代码在线示例Cesium 给实体对象(Entity)提供了很多实用的样式,基本满足普通项目需求; 但是作为 WebGL 引擎,肯定不够丰富,尤其是动态效果样式。 对于实体对象(Entity),可以通过自定义材质,实现各种动…

【YOLOv8系列】YOLOv8的GUI界面设计;在电脑本地实现YOLOv8的可视化交互界面设计(对摄像头的实时画面进行分类)

背景: 最近在研究YOLOv8的应用,并且已经在自己的笔记本环境中跑通了YOLOv8的检测和分类算法,训练、验证、预测等功能均已实现;也通过自己的数据集训练出了自己的模型(权重);且之前也做了一个webUI界面,对YOLOv8检测和分类的结果进行展示;但是如果在本地的GUI界面调用摄…

Python pip 更换镜像源

文章目录 1 概述1.1 默认镜像&#xff0c;速度慢&#xff0c;易报错1.2 常用国内镜像源 2 更改镜像源2.1 临时更改2.2 永久更改2.2.1 查看配置源及配置文件2.2.2 编辑 pip.ini2.2.3 配置后的效果 1 概述 1.1 默认镜像&#xff0c;速度慢&#xff0c;易报错 默认镜像&#xff…

导出硬盘所有文件名到txt文本文件——C#学习笔记

下面的示例演示如何使用递归遍历目录树。递归方法很简洁&#xff0c;但如果目录树很大且嵌套很深&#xff0c;则有可能会引起堆栈溢出异常。 对于所处理的特定异常以及在每个文件和文件夹上执行的特定操作&#xff0c;都只是作为示例提供。您应该修改此代码来满足自己特定的需…

分类学习器(Classification Learner App)MATLAB

在MATLAB中&#xff0c;分类学习器用于构建和评估分类模型。MATLAB提供了一些工具和功能&#xff0c;帮助你进行分类任务&#xff0c;例如分类学习器应用程序、统计和机器学习工具箱中的函数等。 导入数据 我们在打开应用程序之前的第一步将是导入我们将在工作区使用的数据。…

新品上市丨科学级新款制冷相机sM4040A/sM4040B

sM4040B科学级显微制冷相机 特性 sM4040B搭载了 GSENSE4040BSI 3.2 英寸图像传感器&#xff0c;针对传感器固有的热噪声&#xff0c;专门设计了高效制冷模块&#xff0c;使得相机传感器的工作温度比环境温度低达 35-40 度。针对制冷相机常见的低温结雾现象设计了防结雾机制&a…

Serverless 应用引擎 SAE 助力袋拉拉研发提效 70%

作者&#xff1a;百潼 医院环保 IOT 设备的引领者&#xff1a;机汽猫 机汽猫是⼀家致⼒于通过投放⾃助取袋设备&#xff0c;为医院场景提供新型环保袋交付⽅式的科技公司。它成⽴于 2019 年&#xff0c;旗下品牌袋拉拉&#xff08;DaiLala&#xff09;通过投放⾃助取袋机&…

《Cloud Native Data Center Networking》(云原生数据中心网络设计)读书笔记 -- 10数据中心中的BGP

本章解答以下问题&#xff1a; ASN&#xff0c;团体&#xff08;community&#xff09;&#xff0c;属性&#xff08;attribute&#xff09;&#xff0c;最佳路径这些BGP术语是什么疑似&#xff1f;在数据中心中应该使用eBGP还是iBGP?在数据中心使用BGP时&#xff0c;应采用什…

序列化和反序列化之Serializable与Parcelable的异同

目录 序列化和反序列化Serializable 和 Parcelable 的区别Serializable特点Parcelable特点Serializable、Parcelable 使用场景区别总结 在 Android 开发中&#xff0c;序列化和反序列化是将对象转换为字节流以及从字节流还原对象的过程。Java 提供了 Serializable 接口&#xf…

jmeter中响应时间、TPS、服务器资源图表

一、响应时间图表 jmeter中的聚合报告已经足够显示响应时间&#xff0c;但是不会显示很详细&#xff0c;下面使用监听器中的插件查看&#xff0c; 添加后&#xff0c;可以不用更改任何配置&#xff0c;直接使用默认即可统计响应时间 还是抓取百度1分钟查看数据&#xff0c;也是…

Meta:大语言模型可以通过自我批判取得大幅提升!

夕小瑶科技说 原创 作者 | 谢年年 论文的审稿模式想必大家都不会陌生&#xff0c;一篇论文除了分配多个评审&#xff0c;最后还将由PC综合评估各位审稿人的reviews撰写meta-review。 最近&#xff0c;来自Meta的研究团队将这一模式引进到大模型的对齐训练中。模型同时扮演 执…

一. 从Hive开始

1. 怎么理解Hive Hive不能理解成一个传统意义上的数据库&#xff0c;应该理解成一个解决方案。 是Hadoop在hdfs和mapreduce之后才出现的一个结构化数据处理的解决方案。 Hdfs解决了大数据的存储问题&#xff0c;mapreduce解决了数据的计算问题。 一切似乎很美好。 但是使用成本…

人机环境系统智能与Petri网

人机环境系统工程是一门新兴的交叉学科&#xff0c;它以人、机、环境为系统&#xff0c;研究系统整体的优化。而 Petri 网是一种用于描述和分析系统动态行为的图形化建模工具。 在人机环境系统中&#xff0c;智能体现在人、机、环境三个要素之间的相互作用和协同工作。人的智能…

【微信小程序】搭建项目步骤 + 引入Tdesign UI

目录 创建1个空文件夹&#xff0c;选择下图基础模板 开启/支持sass 创建公共style文件并引入 引入Tdesign UI: 1. 初始化&#xff1a; 2. 安装后&#xff0c;开发工具进行构建&#xff1a; 3. 修改 app.json 4. 使用 5. 自定义主题色 创建1个空文件夹&#xff0c;选择下…

map和set的使用和底层实现

嗨喽大家好&#xff0c;时隔许久阿鑫又给大家带来了新的博客&#xff0c;c进阶——map和set的使用和底层实现&#xff0c;下面让我们开始今天的学习吧&#xff01; map和set的使用和底层实现 1.set和multiset的使用 2.map和multimap的使用 3.底层结构 1.set和multiset的使…

基于FCM模糊聚类算法的图像分割matlab仿真

目录 1.算法运行效果图预览 2.算法运行软件版本 3.部分核心程序 4.算法理论概述 4.1 FCM算法原理 4.2 图像分割中的应用 5.算法完整程序工程 1.算法运行效果图预览 (完整程序运行后无水印) 2.算法运行软件版本 matlab2022a 3.部分核心程序 &#xff08;完整版代码包…

单列表集合顶层接口Collection

List&#xff1a;添加元素是有序&#xff0c;可重复&#xff0c;有索引 Set&#xff1a;添加元素是无序&#xff0c;不重复&#xff0c;无索引 Collection是单列集合的祖宗接口&#xff0c;它的功能是全部单列集合都可以继承使用。 1.添加元素 细节1:如果我们要往List系列集…

ArcGIS出图格网小数位数设置

1、比如要去掉格网后面的小数点&#xff0c;如何设置呢&#xff1f; 2、如下图设置。

《软件工程导论》(第6版)第12章 面向对象实现 复习笔记

第12章 面向对象实现 一、面向对象实现概述 1&#xff0e;主要任务 &#xff08;1&#xff09;把面向对象设计结果翻译成用某种程序语言书写的面向对象程序。 &#xff08;2&#xff09;测试并调试面向对象的程序。 2&#xff0e;面向对象程序质量的影响因素 &#xff0…

Redis Pub/Sub模式:分布式系统中的解耦利器

序言 Redis的发布订阅&#xff08;Pub/Sub&#xff09;是一种消息通信模式&#xff0c;允许发布者&#xff08;Publisher&#xff09;发送消息到频道&#xff08;Channel&#xff09;&#xff0c;而订阅者&#xff08;Subscriber&#xff09;可以订阅一个或多个频道来接收消息…