vue3使用antd组件库a-table表格组件实现合并单元格。

需求
在这里插入图片描述
发货单编码、实际发货数量、发货日期、操作人员完全相同的情况下合并单元格展示;

<template>
  <a-table :columns="columns" :data-source="dataSource" :custom-row="customRow" />
</template>

<script>
import { ref } from 'vue'
import { Table } from 'ant-design-vue'

export default {
  components: {
    'a-table': Table
  },
  setup() {
    const columns = ref([
      // 表头配置
      {
        title: '发货单编码',
        dataIndex: 'deliveryCode',
        key: 'deliveryCode',
        customCell: ({ rowSpan }) => {
          return {
            rowSpan: rowSpan
          }
        }
      },
      {
        title: '实际发货数量',
        dataIndex: 'quantity',
        key: 'quantity',
        customCell: ({ rowSpan }) => {
          return {
            rowSpan: rowSpan
          }
        }
      },
      {
        title: '发货日期',
        dataIndex: 'deliveryDate',
        key: 'deliveryDate',
        customCell: ({ rowSpan }) => {
          return {
            rowSpan: rowSpan
          }
        }
      },
      {
        title: '操作人员',
        dataIndex: 'operator',
        key: 'operator',
        customCell: ({ rowSpan }) => {
          return {
            rowSpan: rowSpan
          }
        }
      },
      {
        title: '产品名称',
        dataIndex: 'productName',
        key: 'productName'
      },
      {
        title: '产品类型',
        dataIndex: 'productType',
        key: 'productType'
      },
      {
        title: '生产令牌',
        dataIndex: 'productionToken',
        key: 'productionToken'
      }
    ])

    const dataSource = ref([
      // 表格数据
      // 数据需要按照发货单编码排序
      {
        deliveryCode: 'A001',
        quantity: '10',
        deliveryDate: '2023-06-01',
        operator: 'John',
        productName: 'Product A',
        productType: 'Type 1',
        productionToken: 'Token 1'
      },
      {
        deliveryCode: 'A001',
        quantity: '10',
        deliveryDate: '2023-06-01',
        operator: 'John',
        productName: 'Product B',
        productType: 'Type 2',
        productionToken: 'Token 2'
      },
      {
        deliveryCode: 'A002',
        quantity: '15',
        deliveryDate: '2023-06-02',
        operator: 'Jane',
        productName: 'Product C',
        productType: 'Type 1',
        productionToken: 'Token 3'
      },
      {
        deliveryCode: 'A002',
        quantity: '25',
        deliveryDate: '2023-06-02',
        operator: 'Jane',
        productName: 'Product D',
        productType: 'Type 2',
        productionToken: 'Token 4'
      }
    ])
    function groupData(data) {
      let currentDeliveryCode = '';
      let currentQuantity = '';
      let currentDeliveryDate = '';
      let currentOperator = '';
      return data.map((item, index) => {
        if (
          currentDeliveryCode !== item.deliveryCode ||
          currentQuantity !== item.quantity ||
          currentDeliveryDate !== item.deliveryDate ||
          currentOperator !== item.operator
        ) {
          currentDeliveryCode = item.deliveryCode;
          currentQuantity = item.quantity;
          currentDeliveryDate = item.deliveryDate;
          currentOperator = item.operator;
          let rowSpan = 0;
          for (let i = 0; i < data.length; i++) {
            if (i >= index) {
              const currentItem = data[i];
              if (
                currentDeliveryCode === currentItem.deliveryCode &&
                currentQuantity === currentItem.quantity &&
                currentDeliveryDate === currentItem.deliveryDate &&
                currentOperator === currentItem.operator
              ) {
                rowSpan += 1;
              } else {
                break;
              }
            }
          }
          item.rowSpan = rowSpan;
        } else {
          item.rowSpan = 0;
        }

        return item;
      });
    }
    groupData(dataSource.value)
    return {
      columns,
      dataSource,
      groupData,
    }
  }
}
</script>

效果
在这里插入图片描述
这种需求一般不会出现,一般是根据一个字段进行合并,那就更简单了如下:

    function groupData(data) {
    //声明一个新字段
      let currentMilepostName = ''
      return data.map((item, index) => {
        if (currentMilepostName !== item.milepostName) {
          currentMilepostName = item.milepostName
          let rowSpan = 0
          for (let i = 0; i < data.length; i++) {
            if (i >= index) {
              if (currentMilepostName === data[i].milepostName) {
                rowSpan += 1
              } else {
                break
              }
            }
          }
          item.rowSpan = rowSpan
        } else {
          item.rowSpan = 0
        }

        return item
      })
    }
columns: [
        {
          title: '父级姓名',
          dataIndex: 'Name',
          ellipsis: true,
          width: '250px',
          fixed: 'left',
          align: 'left',
          customCell: ({ rowSpan }) => {
            return {
              rowSpan: rowSpan
            }
          }
        },  
        ]
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 6
    评论
在Ant Design Vue 3中,您可以使用 `customRender` 属性来自定义表格单元格的渲染方式,以实现合并单元格的效果。 以下是一个示例代码,展示了如何合并表格中的两列: ```vue <template> <a-table :columns="columns" :data-source="data"> <template #name="{ text, record }"> <span v-if="record.key === '1'">{{ text }}</span> <span v-else></span> </template> </a-table> </template> <script> export default { data() { return { columns: [ { title: "姓名", dataIndex: "name", key: "name", customRender: ({ text, record }) => { if (record.key === "1") { return { children: <span>{text}</span>, attrs: { rowSpan: 2, }, }; } return { children: <span></span>, attrs: { rowSpan: 0, }, }; }, }, { title: "年龄", dataIndex: "age", key: "age", customRender: ({ text, record }) => { if (record.key === "1") { return { children: <span>{text}</span>, attrs: { rowSpan: 2, }, }; } return { children: <span></span>, attrs: { rowSpan: 0, }, }; }, }, { title: "地址", dataIndex: "address", key: "address", }, ], data: [ { key: "1", name: "John Brown", age: 32, address: "New York No. 1 Lake Park", }, { key: "2", name: "", age: 0, address: "New York No. 1 Lake Park", }, { key: "3", name: "Jim Green", age: 42, address: "London No. 1 Lake Park", }, { key: "4", name: "Joe Black", age: 32, address: "Sidney No. 1 Lake Park", }, ], }; }, }; </script> ``` 在这个示例中,我们使用 `customRender` 属性来自定义单元格的渲染方式。在第一列和第二列中,我们检查当前行是否是第一行(key为1),如果是,则将 `rowSpan` 属性设置为2,以合并单元格。在其他行中,我们将 `rowSpan` 属性设置为0,以使单元格不可见。 请注意,我们使用Vue 的 JSX 语法来创建表格中的单元格。如果您不熟悉 JSX,可以使用常规的模板语法来编写相同的代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

屎山制造者2022

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

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

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

打赏作者

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

抵扣说明:

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

余额充值