React&antd表格合并列

合并表格中连续的相同的列

// antd表格合并列
/*
key: 键值
row:当前行
index:当前行索引
listData:表格全部数据
reactDom:要渲染的jsx
*/
export const rowSpanTable = (key, row, index, listData, reactDom) => {
  let rowSpan = 1
  let arrIndex = 0
  listData.forEach((item, Dindex) => {
    if (item.id === row.id) {
      arrIndex = Dindex
    }
  })
  if (index === 0) {
    listData.forEach((item, Dindex) => {
      if (Dindex > arrIndex && item[key] === row[key]) {
        rowSpan += 1
      }
    })
  } else if (listData[arrIndex][key] === listData[arrIndex - 1][key]) {
    rowSpan = 0
  } else {
    listData.forEach((item, Dindex) => {
      if (Dindex > arrIndex && item[key] === row[key]) {
        rowSpan += 1
      }
    })
  }
  return {
    children: reactDom,
    props: {
      rowSpan
    }
  }
}

使用时

// 引入
import { rowSpanTable } from '../../utils/utils'

// 表格中每一列 render 函数中直接使用
<Table
	dataSource={this.props.labelList.tagList}
	rowKey={row => row.id}
	pagination={{
		position: 'bottom',
		pageSize: this.state.listData.pageSize,
		current: this.state.listData.pageNo,
		total: this.props.labelList.totalCount,
		onChange: this.pageChange
	}}
	>
	<Column
		title="标签位置"
		align="center"
		dataIndex="sort"
		render={(sort, row, index) => rowSpanTable('sort', row, index, this.props.labelList.tagList, (
			<span>{sort}</span>
		))}
	/>
</Table>

因为之前一直写vue所以习惯使用jsx方式使用表格,和普通方式一样,都在render函数中直接写方法
页面效果

这里是引用

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值