react antDesign中表格組件的單元格動(dòng)態(tài)合并

    1. 首先要對(duì)后臺(tái)返回的表格數(shù)據(jù)排序,把需要合并的相同的字段排在一起。這一步一般后臺(tái)會(huì)處理好。
    1. 在表格組件中創(chuàng)建一個(gè)temp對(duì)象,用來(lái)存放已經(jīng)合并的字段。
    1. 新建一個(gè)方法mergecells,在方法遍歷數(shù)據(jù),相同的字段累計(jì)加1,遍歷之后返回重復(fù)次數(shù)。
    1. 在表格的columns數(shù)據(jù)中使用mergecells方法,詳情看下列代碼
import React, { Component } from 'react'
import { Table, Icon } from 'antd'
import pagination from 'src/components/pagination'

class TableList extends Component {
  
  render () {
    const temp = {} // 當(dāng)前重復(fù)的值,支持多列
    /**
     * 動(dòng)態(tài)合并表格方法
     * @param {*} text 表格每列對(duì)應(yīng)的值
     * @param {*} data 后臺(tái)返回的展示數(shù)據(jù)數(shù)組, 數(shù)據(jù)需要按字段排序
     * @param {*} columns 表格每列表頭字段
     */
    const mergeCells = (text, data, columns) => {
      let i = 0 
      if (text !== temp[columns]){
        temp[columns] = text
        data.forEach(((item) => {
          if (item[columns] === temp[columns]){
            i += 1
          }
        }))
      }
      return i
    }
    const renderContent = (value, row, index) => {
      const obj = {
        children: value,
        props: {},
      }
      return obj
    }
    const columns = [
      {
        title: '序號(hào)',
        dataIndex: 'serialNumber',
        render: renderContent,
      },
      {
        title: '渠道一',
        dataIndex: 'firstClassBrand',
        render: (value, row, index) => {
          const obj = {
            children: value,
            props: {},
          }
          obj.props.rowSpan = mergeCells(row.firstClassBrand, this.props.data, 'firstClassBrand')
          return obj
        },
      },
      {
        title: '渠道二',
        dataIndex: 'twoTierBrand',
        render: (value, row, index) => {
          const obj = {
            children: value,
            props: {}
          }
          obj.props.rowSpan = mergeCells(row.twoTierBrand, this.props.data, 'twoTierBrand')
          return obj
        },
      }
    ]
    return(
      <Table columns={columns} dataSource={this.props.data} pagination={pagination({ total: 100 })} bordered />
    )
  }
}

export default TableList
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容