Table 表格
Guide#
Table负责将数据呈现为高度可定制和具备可访问性的HTML表格,其核心功能为将结构化的数据使用表格的方式展现, 然后可以使用各种参数来向表格中加入一些特性,比如排序,过滤,滚动,锁列等。
基本使用#
基本的Table包含行和列,使用Table.Column来定义列的信息,使用传入的dataSource属性数据来创建行。
下面的代码将会创建一行两列的数据表。
const dataSource = [{id: 1, time: '2016'}];
ReactDOM.render(<Table dataSource={dataSource}>
<Table.Column title="Id" dataIndex="id"/>
<Table.Column title="Time" dataIndex="time"/>
</Table>, mountNode)
列配置#
Table.Column提供了非常多的配置属性用于自定义列,最常见的就是使用cell
自定义单元格的渲染逻辑. 其他的配置选项可以参考下面的Table.Column的API
下面的代码会让cell根据值渲染不同的视图
const dataSource = [{id: 1, time: '2016'}];
const renderTime = value => {
if (value == '2016') {
return '今年';
}
return value;
}
ReactDOM.render(<Table dataSource={dataSource}>
<Table.Column title="Id" dataIndex="id"/>
<Table.Column title="Time" dataIndex="time" cell={renderTime}/>
</Table>, mountNode)
多表头#
使用Table.ColumnGroup包裹Table.Column来创建有多个表头的表格
const dataSource = [{id: 1, time: '2016'}];
ReactDOM.render(<Table dataSource={dataSource}>
<Table.ColumnGroup>
<Table.Column title="Id" dataIndex="id"/>
<Table.Column title="Time" dataIndex="time"/>
</Table.ColumnGroup>
<Table.ColumnGroup>
<Table.Column title="Id" dataIndex="id"/>
</Table.ColumnGroup>
</Table>, mountNode)
性能问题#
由于React的机制问题,在做与Table无关的更新的时候,可能会导致diff计算花费大量的时间,
在你确认只有props和state才能影响Table渲染的情况下,可以设置optimization
为true
来开启, 原理就是通过
shouldComponentUpdate
的生命周期来对比props和state的变更,开启了该选项后可能导致下面的副作用。
class App extends React.Component{
state = {
extra: 'abc'
}
cellRender = (value) => {
return value + this.state.extra;
}
render(){
return <Table dataSource={[{id: 1}]}>
<Table.Column cell={this.cellRender} dataIndex="id"/>
</Table>
}
componentDidMount(){
setTimeout(() => {
this.setState({
extra: 'bcd'
})
},1000)
}
}
上面的代码在componentDidMount
之后的setState
虽然更新了extra
, 但是并不会触发Table的重新渲染。
解决方式如下:
将cellRender访问的state通过props的方式传入。
class App extends React.Component{ state = { extra: 'abc' } cellRender = (value, index, record, context) => { return value + context.props.extra; } render(){ return <Table dataSource={[{id: 1}]} extra={this.state.extra}> <Table.Column cell={this.cellRender} dataIndex="id"/> </Table> } componentDidMount(){ setTimeout(() => { this.setState({ extra: 'bcd' }) },1000) } }
通过设置
optimization
为false
来关闭Table的shouldComponentUpdate
配置。class App extends React.Component{ state = { extra: 'abc' } cellRender = (value, index, record, context) => { return value + this.state.extra; } render(){ return <Table dataSource={[{id: 1}]} optimization={false}> <Table.Column cell={this.cellRender} dataIndex="id"/> </Table> } componentDidMount(){ setTimeout(() => { this.setState({ extra: 'bcd' }) },1000) } }
API#
Table#
Table的子元素必须是下面三个元素中的一种
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
dataSource | 表格展示的数据源 | Array | [] |
rowSelection | 是否启用Checkbox多选,详见rowSelection | Object | {} |
getRowClassName | 获取每一行的样式名 | Function(record, index):String | noop |
getCellProps | 获取该单元格的属性 | Function(rowIndex, colIndex, record):Object | noop |
fixedHeader | 是否固定表头 | Boolean | false |
maxBodyHeight | 最大内容区域的高度,在fixedHeader 为true 的时候,超过这个高度会出现滚动条 | Number | 200 |
hasBorder | 表格是否具有边框 | Boolean | true |
hasHeader | 表格是否具有头部 | Boolean | true |
isZebra | 表格是否是斑马线 | Boolean | false |
isLoading | 表格是否在加载中 | Boolean | false |
primaryKey | dataSource当中数据的主键 | String | id |
filterParams | 当前过滤的的keys,使用此属性可以控制表格的头部的过滤选项中哪个菜单被选中,格式为 {dataIndex: {selectedKeys:[]}} | Object | null |
sort | 当前排序的字段,使用此属性可以控制表格的字段的排序,格式为{dataIndex: 'asc'} | Object | null |
expandedRowRender | 额外渲染的行的函数 | Function(record, index) | noop |
expandedRowKeys | 默认展开的额外的行, 传入此属性为受控状态 | Array | null |
expandedRowIndent | 配置默认展开行的缩进 | Array[left,right] | [1,0] |
hasExpandedRowCtrl | 是否显示+号按钮 | Boolean | true |
onExpandedChange | 受控的时候触发的事件 | Function(expandedRowKeys) | noop |
onExpandedRowClick | 额外行点击触发的事件 | Function(record, index, e) | noop |
onRowClick | 每一行点击触发的事件 | Function(record, index, e) | noop |
onRowMouseEnter | 每一行鼠标悬浮的时候出发的事件 | Function(record, index, e) | noop |
onRowMouseLeave | 每一行鼠标离开的时候出发的事件 | Function(record, index, e) | noop |
onSort | 点击列排序触发的事件 | Function(dataIndex, order, sort) | noop |
onFilter | 点击过滤触发的事件 | Function(filterKeys) | noop |
isTree | 开启Table的tree模式,接收的数据格式中包含children则渲染成tree table | Boolean | false |
indentSize | 在tree模式下的缩进尺寸 | Number | 12 |
openRowKeys | 传入了此属性代表tree的展开为受控操作 | Array | null |
onRowOpen | 点击tree展开或者关闭的时候触发的事件 | Function(openRowKeys:Array) | noop |
optimization | 是否开启性能优化 | Boolean | true |
rowSelection#
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
getProps | 获取selection的默认属性 | Function(record):Object | noop |
onChange | 选择改变的时候触发的事件 | Function(selectedRowKeys:Array, records:Array) | null |
onSelect | 用户手动选择/取消选择某列的回调 | Function(selected:Boolean, record:Object, records:Array) | null |
onSelectAll | 用户手动选择/取消选择所有列的回调 | Function(selected:Boolean, records:Array) | null |
selectedRowKeys | 设置了此属性,将rowSelection变为受控状态,接收值为该行数据的primaryKey的值 | Array | null |
mode | 选择selection的模式, 可选值为single ,multiple | String | multiple |
Table.Column#
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
dataIndex | 指定单元格渲染的key | String | null |
cell | 行渲染的逻辑 | Function(value, index, record, context), ReactElement | null |
title | 表头显示的内容 | String, ReactElement | '' |
sortable | 是否支持排序 | Boolean | false |
filters | 生成标题过滤的菜单,格式为[{label:'xxx', value:'xxx'}] | Array | false |
filterMode | 过滤的模式是单选还是多选,可选值为single ,multiple | String | multiple |
lock | 是否支持锁列,可选值为left ,right , true | Boolean, String | false |
width | 在锁列的情况下需要配置的宽度 | Number | null |
Table.ColumnGroup#
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
title | 表头显示的内容 | String, ReactElement | '' |
Table.GroupHeader#
如果Table的子元素包含GroupHeader,则开启GroupList模式
属性 | 说明 | 类型 | 默认值 |
---|---|---|---|
cell | 行渲染的逻辑 | Function(value, index, record), React Element | null |