ant table 相同列合并单元格

效果图:

直接上代码(复制代码可直接使用):

<template>

  <a-table :columns="columns" :dataSource="dataSource" bordered> </a-table>

</template>

<script>

export default {

  data() {

    return {

      dataSource: [

        {

          key: '1',

          stationNo: '1-11',

          shortName: '上海',

          number: 12,

          connectTime: '2023-01-22 23:23:24',

     

        },

        {

          key: '2',

          stationNo: '1-11',

          shortName: '上海',

          number: 12,

          connectTime: '2023-01-22 23:23:27',

   

        },

        {

          key: '3',

          stationNo: '1-11',

          shortName: '上海',

          number: 12,

          connectTime: '2023-01-22 23:23:28',

     

        },

        {

          key: '4',

          stationNo: '1-12',

          shortName: '武汉',

          number: 13,

          connectTime: '2023-01-22 23:23:28',

       

        }

      ],

      columns: [

        {

          title: '期号',

          align: 'center',

          dataIndex: 'stationNo'

        },

        {

          title: '地点',

          align: 'center',

          dataIndex: 'shortName',

          customRender: (value, row, index) => {

            return this.mergeCell(value, row, index)

          }

        },

        {

          title: '数量',

          align: 'center',

          dataIndex: 'number',

          customRender: (value, row, index) => {

            return this.mergeCell(value, row, index)

          }

        },

        {

          title: '时间',

          align: 'center',

          dataIndex: 'connectTime'

        }

      ]

    }

  },

  methods: {

    // 合并单元格

    mergeCell(value, row, index) {

      // 满足要求需要合并的行中的第一行

      const temp_Index = this.dataSource.findIndex(

        record => record.shortName === row.shortName

      )

      // 需要合并的行数

      const rowSpan = this.dataSource.filter(

        record => record.shortName === row.shortName

      ).length

      if (rowSpan > 1) {

        // 要合并的行中的第一个(从这一行合并)

        if (temp_Index === index) {

          return {

            children: value,

            attrs: { rowSpan }

          }

        }

        // 其他要合并的行都设rowSpan=0

        return {

          children: value,

          attrs: { rowSpan: 0 }

        }

      }

      return value

    }

  }

}

</script>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值