ant design table 合并单元格

该博客详细介绍了如何使用Ant Design表格组件实现列合并功能,通过`customRender`属性结合`rowSpan`来控制单元格合并。文章强调了由于`chargingStationShortName`字段的唯一性,可以确保正确合并。同时,文中提醒在处理数据拆分时要注意分页问题,以避免错误的合并。示例代码展示了具体的实现方法,并附带了实际效果截图。

a-table

 我们重点看columns 的配置

    columns:
     [
            {
          title: '建设期号',
          align: 'center',
          dataIndex: 'stationNo',
          customRender: (value, row, index) => {
            return this.mergeCell(value, row, index)
          },
        },
        {
          title: '场站',
          align: 'center',
          dataIndex: 'chargingStationShortName',
          customRender: (value, row, index) => {
            return this.mergeCell(value, row, index)
          },
        },
        {
          title: '充电桩数量',
          align: 'center',
          dataIndex: 'pileNumber',
          customRender: (value, row, index) => {
            return this.mergeCell(value, row, index)
          },
        },
        {
          title: '抄表时间',
          align: 'center',
          dataIndex: 'connectTime',
        },
        {
          title: '表尾号',
          align: 'center',
          dataIndex: 'meterCodeFull',
          customRender: (value, row, index) => {
            // 满足要求需要合并的行中的第一行
            const temp_Index = this.newDataSource.findIndex((record) => 
               record.meterCodeFull === value)
            // 需要合并的行数
            const rowSpan = this.newDataSource.filter((record) => record.meterCodeFull 
               === value).length
            if (rowSpan > 1) {
              // 要合并的行中的第一个(从这一行合并)
              if (temp_Index === index) {
                return {
                  children: value,
                  attrs: { rowSpan: 2 },
                }
              }
              // 其他要合并的行都设rowSpan=0
              return {
                children: value,
                attrs: { rowSpan: 0 },
              }
            }
            return value
          },
        },
        {
          title: '电表倍率',
          align: 'center',
          dataIndex: 'meterRate',
          customRender: (value, row, index) => {
            // 满足要求需要合并的行中的第一行
            const temp_Index = this.newDataSource.findIndex((record) =>                             
               record.meterCodeFull === row.meterCodeFull)
            // 需要合并的行数
            const rowSpan = this.newDataSource.filter((record) => record.meterCodeFull 
              === row.meterCodeFull).length
            if (rowSpan > 1) {
              // 要合并的行中的第一个(从这一行合并)
              if (temp_Index === index) {
                return {
                  children: value,
                  attrs: { rowSpan: 2 },
                }
              }
              // 其他要合并的行都设rowSpan=0
              return {
                children: value,
                attrs: { rowSpan: 0 },
              }
            }
            return value
          },
        },
        {
          title: '电表示数',
          children: [
            {
              title: '有功(总)',
              align: 'center',
              dataIndex: 'powerTotal',
            },
            {
              title: '有功(尖)',
              align: 'center',
              dataIndex: 'powerSharp',
            },
            {
              title: '有功(峰)',
              align: 'center',
              dataIndex: 'powerPeak',
            },
            {
              title: '有功(平)',
              align: 'center',
              dataIndex: 'powerFlat',
            },
            {
              title: '有功(谷)',
              align: 'center',
              dataIndex: 'powerValley',
            },
            {
              title: '无功(总)',
              align: 'center',
              dataIndex: 'idleTotal',
            },
          ],
        },
    ]

 

 

 

 

 由于changingStationShortName 具有唯一性,不会出现与不该合并数据的值相同 所以封装一个方法,处理合并列问题。

// 合并单元格
    mergeCell(value, row, index) {
      // 满足要求需要合并的行中的第一行
      const temp_Index = this.newDataSource.findIndex(
        (record) => record.chargingStationShortName === row.chargingStationShortName
      )
      // 需要合并的行数
      const rowSpan = this.newDataSource.filter(
        (record) => record.chargingStationShortName === row.chargingStationShortName
      ).length
      if (rowSpan > 1) {
        // 要合并的行中的第一个(从这一行合并)
        if (temp_Index === index) {
          return {
            children: value,
            attrs: { rowSpan },
          }
        }
        // 其他要合并的行都设rowSpan=0
        return {
          children: value,
          attrs: { rowSpan: 0 },
        }
      }
      return value
    },

效果图如下:

 

注:如果涉及数据拆分要千万注意分页问题。

Ant Design Vue 中,可以通过 `customRender` 函数来实现表单元的合并。该方法允许在特定列中根据业务逻辑动态设置 `rowSpan` 或 `colSpan` 属性,以实现单元的跨行或跨列合并。 ### 实现跨行合并 跨行合并通过在 `customRender` 中设置 `rowSpan` 来实现。例如,可以按每三行为一组进行合并,其余行则隐藏或不渲染。以下是一个示例代码: ```vue <template> <a-table :dataSource="datas" :columns="columns" /> </template> <script> export default { data() { return { datas: [ { name: '张三', class: '一班' }, { name: '李四', class: '二班' }, { name: '王五', class: '三班' }, { name: '赵六', class: '一班' }, { name: '钱七', class: '二班' }, { name: '孙八', class: '三班' }, ], columns: [ { title: '序号', scopedSlots: { customRender: 'serialNo' }, width: '100px', }, { title: '姓名', dataIndex: 'name', width: '100px', }, { title: '班级', dataIndex: 'class', width: '100px', }, { title: '操作', width: '100px', customRender: (text, record, index) => { const obj = { children: <a-button type="primary" onClick={() => this.combine(record, index)}>成组</a-button>, attrs: {}, }; if (index % 3 === 0) { obj.attrs.rowSpan = 3; } else { obj.attrs.rowSpan = 0; } return obj; }, }, ], }; }, methods: { combine(record, index) { // 具体的业务逻辑 }, }, }; </script> ``` ### 实现跨列合并 跨列合并则需要在 `customRender` 中设置 `colSpan`。例如,可以根据特定条件合并多个列,以下是一个简单的示例: ```javascript customRender: (text, record, index) => { const obj = { children: text, attrs: {}, }; if (index === 0) { obj.attrs.colSpan = 2; // 合并两列 } return obj; } ``` ### 注意事项 - 在使用 `rowSpan` 或 `colSpan` 时,需确保数据的顺序和结构合理,避免出现表错位的情况。 - 如果合并的单元中包含操作按钮,建议将按钮的点击事件绑定到具体的业务逻辑上,以确保功能的完整性。 通过上述方法,可以灵活地实现 Ant Design Vue中的单元合并功能,从而提升表的展示效果和用户体验。
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值