Table 表格

如果项目中使用的是 1.x 版本的基础组件(@alifd/next),请在左侧导航顶部切换组件版本。

安装方法

  1. 在命令行中执行以下命令npm install @icedesign/base@latest -S

Guide

Table负责将数据呈现为高度可定制和具备可访问性的HTML表格,其核心功能为将结构化的数据使用表格的方式展现,然后可以使用各种参数来向表格中加入一些特性,比如排序,过滤,滚动,锁列等。

基本使用

基本的Table包含行和列,使用Table.Column来定义列的信息,使用传入的dataSource属性数据来创建行。

下面的代码将会创建一行两列的数据表。

  1. const dataSource = [{id: 1, time: '2016'}];
  2. ReactDOM.render(<Table dataSource={dataSource}>
  3. <Table.Column title="Id" dataIndex="id"/>
  4. <Table.Column title="Time" dataIndex="time"/>
  5. </Table>, mountNode)

列配置

Table.Column提供了非常多的配置属性用于自定义列,最常见的就是使用cell自定义单元格的渲染逻辑. 其他的配置选项可以参考下面的Table.Column的API

下面的代码会让cell根据值渲染不同的视图

  1. const dataSource = [{id: 1, time: '2016'}];
  2. const renderTime = value => {
  3. if (value == '2016') {
  4. return '今年';
  5. }
  6. return value;
  7. }
  8. ReactDOM.render(<Table dataSource={dataSource}>
  9. <Table.Column title="Id" dataIndex="id"/>
  10. <Table.Column title="Time" dataIndex="time" cell={renderTime}/>
  11. </Table>, mountNode)

多表头

使用Table.ColumnGroup包裹Table.Column来创建有多个表头的表格

  1. const dataSource = [{id: 1, time: '2016'}];
  2. ReactDOM.render(<Table dataSource={dataSource}>
  3. <Table.ColumnGroup>
  4. <Table.Column title="Id" dataIndex="id"/>
  5. <Table.Column title="Time" dataIndex="time"/>
  6. </Table.ColumnGroup>
  7. <Table.ColumnGroup>
  8. <Table.Column title="Id" dataIndex="id"/>
  9. </Table.ColumnGroup>
  10. </Table>, mountNode)

已知问题

GroupList不支持在Hover状态和选中状态下显示背景色

无法完全支持在锁列的情况下合并单元格

无法支持在锁列的区域和非锁列的区域内合并单元格

性能问题

由于React的机制问题,在做与Table无关的更新的时候,可能会导致diff计算花费大量的时间,

在你确认只有props和state才能影响Table渲染的情况下,可以设置optimizationtrue来开启, 原理就是通过

shouldComponentUpdate的生命周期来对比props和state的变更,开启了该选项后可能导致下面的副作用。

  1. class App extends React.Component{
  2. state = {
  3. extra: 'abc'
  4. }
  5. cellRender = (value) => {
  6. return value + this.state.extra;
  7. }
  8. render(){
  9. return <Table dataSource={[{id: 1}]}>
  10. <Table.Column cell={this.cellRender} dataIndex="id"/>
  11. </Table>
  12. }
  13. componentDidMount(){
  14. setTimeout(() => {
  15. this.setState({
  16. extra: 'bcd'
  17. })
  18. },1000)
  19. }
  20. }

上面的代码在componentDidMount之后的setState虽然更新了extra, 但是并不会触发Table的重新渲染。

解决方式如下:

  • 将cellRender访问的state通过props的方式传入。
  1. class App extends React.Component{
  2. state = {
  3. extra: 'abc'
  4. }
  5. cellRender = (value, index, record, context) => {
  6. return value + context.props.extra;
  7. }
  8. render(){
  9. return <Table dataSource={[{id: 1}]} extra={this.state.extra}>
  10. <Table.Column cell={this.cellRender} dataIndex="id"/>
  11. </Table>
  12. }
  13. componentDidMount(){
  14. setTimeout(() => {
  15. this.setState({
  16. extra: 'bcd'
  17. })
  18. },1000)
  19. }
  20. }
  • 通过设置optimizationfalse来关闭Table的shouldComponentUpdate配置。
  1. class App extends React.Component{
  2. state = {
  3. extra: 'abc'
  4. }
  5. cellRender = (value, index, record, context) => {
  6. return value + this.state.extra;
  7. }
  8. render(){
  9. return <Table dataSource={[{id: 1}]} optimization={false}>
  10. <Table.Column cell={this.cellRender} dataIndex="id"/>
  11. </Table>
  12. }
  13. componentDidMount(){
  14. setTimeout(() => {
  15. this.setState({
  16. extra: 'bcd'
  17. })
  18. },1000)
  19. }
  20. }

API

表格

参数说明类型默认值
prefix样式类名的品牌前缀String'next-'
className自定义类名String-
style自定义内联样式Object-
dataSource表格展示的数据源Array[]
rowSelection是否启用选择模式属性:getProps: {Function} Function(record)=>Object 获取selection的默认属性onChange: {Function} Function(selectedRowKeys:Array, records:Array) 选择改变的时候触发的事件,注意: 其中records只会包含当前dataSource的数据,很可能会小于selectedRowKeys的长度。onSelect: {Function} Function(selected:Boolean, record:Object, records:Array) 用户手动选择/取消选择某行的回调onSelectAll: {Function} Function(selected:Boolean, records:Array) 用户手动选择/取消选择所有行的回调selectedRowKeys: {Array} 设置了此属性,将rowSelection变为受控状态,接收值为该行数据的primaryKey的值mode: {String} 选择selection的模式, 可选值为single, multiple,默认为multipleObjectnull
onRowClick点击表格每一行触发的事件签名:Function(record: Object, index: Number, e: Event) => void参数:record: {Object} 该行所对应的数据index: {Number} 该行所对应的序列e: {Event} DOM事件对象Function() => {}
onRowMouseEnter悬浮在表格每一行的时候触发的事件签名:Function(record: Object, index: Number, e: Event) => void参数:record: {Object} 该行所对应的数据index: {Number} 该行所对应的序列e: {Event} DOM事件对象Function() => {}
onRowMouseLeave离开表格每一行的时候触发的事件签名:Function(record: Object, index: Number, e: Event) => void参数:record: {Object} 该行所对应的数据index: {Number} 该行所对应的序列e: {Event} DOM事件对象Function() => {}
onSort点击列排序触发的事件签名:Function(dataIndex: String, order: String) => void参数:dataIndex: {String} 指定的排序的字段order: {String} 排序对应的顺序, 有descasc两种Function() => {}
onFilter点击过滤确认按钮触发的事件签名:Function(filterParams: Object) => void参数:filterParams: {Object} 过滤的字段信息Function() => {}
getRowClassName设置每一行的样式名称签名:Function(record: Object, index: Number) => String参数:record: {Object} 该行所对应的数据index: {Number} 该行所对应的序列返回值:{String} 需要设置的样式名称Function() => {}
getRowProps设置每一行的属性,如果返回值和其他针对行操作的属性冲突则无效。签名:Function(record: Object, index: Number) => Object参数:record: {Object} 该行所对应的数据index: {Number} 该行所对应的序列返回值:{Object} 需要设置的行属性Function() => {}
getCellProps设置单元格的属性,通过该属性可以进行合并单元格签名:Function(rowIndex: Number, colIndex: Number, record: Object) => Object参数:rowIndex: {Number} 该列所对应的序列colIndex: {Number} 该行所对应的序列record: {Object} 该行对应的记录返回值:{Object} 返回td元素的所支持的属性对象Function() => {}
fixedHeader表头是否固定,该属性配合maxBodyHeight使用,当内容区域的高度超过maxBodyHeight的时候,在内容区域会出现滚动条Booleanfalse
maxBodyHeight最大内容区域的高度,在fixedHeadertrue的时候,超过这个高度会出现滚动条Number200
hasBorder表格是否具有边框Booleantrue
hasHeader表格是否具有头部Booleantrue
isZebra表格是否是斑马线Booleanfalse
isLoading表格是否在加载中Booleanfalse
primaryKeydataSource当中数据的主键,如果给定的数据源中的属性不包含该主键,会造成选择状态全部选中String'id'
filterParams当前过滤的的keys,使用此属性可以控制表格的头部的过滤选项中哪个菜单被选中,格式为 {dataIndex: {selectedKeys:[]}}示例:假设要控制dataIndex为id的列的过滤菜单中key为one的菜单项选中<Table filterParams={{id: {selectedKeys: ['one']}}}/>Object-
sort当前排序的字段,使用此属性可以控制表格的字段的排序,格式为{dataIndex: 'asc'}Object-
expandedRowRender额外渲染行的渲染函数签名:Function(record: Object, index: Number) => Element参数:record: {Object} 该行所对应的数据index: {Number} 该行所对应的序列返回值:{Element} nullFunction-
expandedRowIndent额外渲染行的缩进Array1, 0
expandedRowKeys默认情况下展开的额外渲染行, 传入此属性为受控状态Array-
hasExpandedRowCtrl是否显示点击展开额外渲染行的+号按钮Booleantrue
getExpandedColProps设置额外渲染行的属性签名:Function() => voidFunction() => {}
onExpandedChange在额外渲染行展开或者收齐的时候触发的事件签名:Function(expandedRowKeys: Array, currentRowKey: String, expanded: Boolean, currentRecord: Object) => void参数:expandedRowKeys: {Array} 展开的渲染行的keycurrentRowKey: {String} 当前点击的渲染行的keyexpanded: {Boolean} 当前点击是展开还是收起currentRecord: {Object} 当前点击额外渲染行的记录Function() => {}
onExpandedRowClick点击额外渲染行触发的事件签名:Function(record: Object, index: Number, e: Event) => void参数:record: {Object} 该行所对应的数据index: {Number} 该行所对应的序列e: {Event} DOM事件对象Function() => {}
indentSize在tree模式下的缩进尺寸, 仅在isTree为true时候有效Number12
openRowKeys默认情况下展开的树形表格,传入了此属性代表tree的展开为受控操作Array-
onRowOpen点击tree展开或者关闭的时候触发的事件签名:Function(openRowKeys: Array, currentRowKey: String, opened: Boolean, currentRecord: Object) => void参数:openRowKeys: {Array} tree模式下展开的keycurrentRowKey: {String} 当前点击行的keyopened: {Boolean} 当前点击是展开还是收起currentRecord: {Object} 当前点击行的记录Function() => {}
isTree开启Table的tree模式, 接收的数据格式中包含children则渲染成tree tableBooleanfalse
optimization是否开启性能优化,开启了性能优化后,会自动加入shouldComponentUpdateBooleanfalse
locale自定义国际化文案对象属性:empty: {String} 没有数据时的提示文案ok: {String} 过滤器中确认按钮文案reset: {String} 过滤器中重置按钮文案Object-
language自定义国际化语言可选值:'en-us', 'zh-cn', 'zh-tw'Enum-

Table.ColumnGroup

参数说明类型默认值
title表头显示的内容ReactElement/ReactNode/Function'column-group'

Table.Column

参数说明类型默认值
dataIndex指定列对应的字段,支持a.b形式的快速取值String-
cell行渲染的逻辑Function(value, index, record) => ElementReactElement/ReactNode/Function(value) => value
title表头显示的内容ReactElement/ReactNode/Function'column'
sortable是否支持排序Boolean-
width在锁列的情况下需要配置的宽度ReactNode-
align单元格的对齐方式可选值:'left', 'center', 'right'Enum-
filters生成标题过滤的菜单, 格式为[{label:'xxx', value:'xxx'}]Array<Object>-
filterMode过滤的模式是单选还是多选可选值:'single', 'multiple'Enum'multiple'
lock是否支持锁列,可选值为left,right, trueBoolean/String-

Table.GroupHeader

参数说明类型默认值
cell行渲染的逻辑ReactElement/ReactNode/Function() => ''
hasSelection是否在GroupHeader上面渲染selectionBooleanfalse

代码示例

简单

简单的表格渲染

Table 表格 - 图1

查看源码在线预览

  1. import { Table } from "@icedesign/base";
  2. const onRowClick = function(record, index, e) {
  3. console.log(record, index, e);
  4. },
  5. getData = () => {
  6. let result = [];
  7. for (let i = 0; i < 5; i++) {
  8. result.push({
  9. title: {
  10. name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`
  11. },
  12. id: 100306660940 + i,
  13. time: 2000 + i
  14. });
  15. }
  16. return result;
  17. },
  18. render = (value, index, record) => {
  19. return <a>Remove({record.id})</a>;
  20. };
  21. ReactDOM.render(
  22. <Table dataSource={getData()} onRowClick={onRowClick}>
  23. <Table.Column title="Id" dataIndex="id" />
  24. <Table.Column title="Title" dataIndex="title.name" />
  25. <Table.Column title="Time" dataIndex="time" />
  26. <Table.Column cell={render} width="40%" />
  27. </Table>,
  28. mountNode
  29. );

选择可控

演示全选和单选受控的功能

Table 表格 - 图2

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. const getData = () => {
  3. let result = [];
  4. for (let i = 0; i < 5; i++) {
  5. result.push({
  6. title: {
  7. name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`
  8. },
  9. id: 100306660940 + i,
  10. time: 2000 + i
  11. });
  12. }
  13. return result;
  14. },
  15. render = (value, index, record) => {
  16. return <a>Remove({record.id})</a>;
  17. };
  18. class App extends React.Component {
  19. constructor(props) {
  20. super(props);
  21. this.state = {
  22. rowSelection: {
  23. onChange: this.onChange.bind(this),
  24. onSelect: function(selected, record, records) {
  25. console.log("onSelect", selected, record, records);
  26. },
  27. onSelectAll: function(selected, records) {
  28. console.log("onSelectAll", selected, records);
  29. },
  30. selectedRowKeys: [],
  31. getProps: record => {
  32. return {
  33. disabled: record.id === 100306660941
  34. };
  35. }
  36. },
  37. dataSource: getData(0, 5)
  38. };
  39. }
  40. render() {
  41. return (
  42. <div>
  43. <p>
  44. <Button onClick={this.clear.bind(this)}>Clear Selection</Button>&nbsp;
  45. <Button onClick={this.changeMode.bind(this)}>
  46. Switch single mode
  47. </Button>&nbsp;
  48. <Button onClick={this.toggleLoading.bind(this)}>
  49. Toggle loading
  50. </Button>&nbsp;
  51. <Button onClick={this.modifyDataSource.bind(this)}>
  52. Modify dataSource
  53. </Button>
  54. </p>
  55. <Table
  56. dataSource={this.state.dataSource}
  57. isLoading={this.state.isLoading}
  58. rowSelection={this.state.rowSelection}
  59. >
  60. <Table.Column title="Id" dataIndex="id" />
  61. <Table.Column title="Title" dataIndex="title.name" />
  62. <Table.Column title="Time" dataIndex="time" />
  63. <Table.Column cell={render} width={200} />
  64. </Table>
  65. </div>
  66. );
  67. }
  68. onChange(ids, records) {
  69. let { rowSelection } = this.state;
  70. rowSelection.selectedRowKeys = ids;
  71. console.log("onChange", ids, records);
  72. this.setState({ rowSelection });
  73. }
  74. clear() {
  75. let { rowSelection } = this.state;
  76. rowSelection.selectedRowKeys = [];
  77. this.setState({ rowSelection });
  78. }
  79. toggleLoading() {
  80. this.setState({ isLoading: !this.state.isLoading });
  81. }
  82. changeMode() {
  83. let { rowSelection } = this.state;
  84. rowSelection.mode = "single";
  85. this.setState({ rowSelection });
  86. }
  87. modifyDataSource() {
  88. this.setState({
  89. dataSource: getData(9, 14)
  90. });
  91. }
  92. }
  93. ReactDOM.render(<App />, mountNode);

行列合并

通过getCellProps进行列合并。

Table 表格 - 图3

查看源码在线预览

  1. import { Table } from "@icedesign/base";
  2. const onRowClick = function(record, index, e) {
  3. console.log(record, index, e);
  4. },
  5. getData = () => {
  6. let result = [];
  7. for (let i = 0; i < 5; i++) {
  8. result.push({
  9. title: {
  10. name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`
  11. },
  12. id: 100306660940 + i,
  13. time: 2000 + i
  14. });
  15. }
  16. return result;
  17. },
  18. render = (value, index, record) => {
  19. return <a>Remove({record.id})</a>;
  20. },
  21. getCellProps = (rowIndex, colIndex) => {
  22. if (rowIndex === 2 && colIndex === 1) {
  23. return {
  24. colSpan: 2,
  25. rowSpan: 3
  26. };
  27. }
  28. if (rowIndex === 1 && colIndex === 2) {
  29. return {
  30. colSpan: 2,
  31. rowSpan: 1
  32. };
  33. }
  34. };
  35. ReactDOM.render(
  36. <Table
  37. dataSource={getData()}
  38. onRowClick={onRowClick}
  39. getCellProps={getCellProps}
  40. >
  41. <Table.Column title="Id" dataIndex="id" />
  42. <Table.Column title="Title" dataIndex="title.name" />
  43. <Table.Column title="Time" dataIndex="time" />
  44. <Table.Column cell={render} width={200} />
  45. </Table>,
  46. mountNode
  47. );

定制列

定制显示的表格列数

Table 表格 - 图4

查看源码在线预览

  1. import { Table, Button, Dialog, Checkbox } from "@icedesign/base";
  2. const { Group } = Checkbox;
  3. const getData = () => {
  4. let result = [];
  5. for (let i = 0; i < 5; i++) {
  6. result.push({
  7. title: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`,
  8. id: 100306660940 + i,
  9. time: 2000 + i
  10. });
  11. }
  12. return result;
  13. },
  14. cols = [
  15. {
  16. title: "id",
  17. dataIndex: "id"
  18. },
  19. {
  20. title: "Title",
  21. dataIndex: "title"
  22. },
  23. {
  24. title: "Time",
  25. dataIndex: "time"
  26. }
  27. ];
  28. class App extends React.Component {
  29. constructor(props) {
  30. super(props);
  31. this.state = {
  32. dataSource: getData(),
  33. cols: cols
  34. };
  35. }
  36. openDialog = () => {
  37. Dialog.alert({
  38. needWrapper: false,
  39. content: this.renderControlContent(),
  40. title: "选择需要显示的列",
  41. onOk: () => {
  42. this.setState({
  43. cols: this.changedCols
  44. });
  45. }
  46. });
  47. };
  48. renderControlContent() {
  49. const groupSource = cols.map(col => {
  50. return {
  51. label: col.title,
  52. value: col.dataIndex
  53. };
  54. }),
  55. defaultValue = this.state.cols.map(col => col.dataIndex);
  56. return (
  57. <Group
  58. dataSource={groupSource}
  59. onChange={this.onChange}
  60. defaultValue={defaultValue}
  61. />
  62. );
  63. }
  64. onChange = value => {
  65. this.changedCols = cols.filter(col => value.indexOf(col.dataIndex) > -1);
  66. };
  67. render() {
  68. return (
  69. <div>
  70. <p>
  71. <Button onClick={this.openDialog}> 选择显示的列数 </Button>
  72. </p>
  73. <Table dataSource={this.state.dataSource}>{this.renderCols()}</Table>
  74. </div>
  75. );
  76. }
  77. renderCols() {
  78. const { cols } = this.state;
  79. return cols.map(col => {
  80. return (
  81. <Table.Column
  82. title={col.title}
  83. dataIndex={col.dataIndex}
  84. key={col.dataIndex}
  85. />
  86. );
  87. });
  88. }
  89. }
  90. ReactDOM.render(<App />, mountNode);

增删改查

演示对表格的增删改查

Table 表格 - 图5

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. const onRowClick = function(record, index, e) {
  3. console.log(record, index, e);
  4. },
  5. getData = () => {
  6. let result = [];
  7. for (let i = 0; i < 5; i++) {
  8. result.push({
  9. title: {
  10. name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`
  11. },
  12. id: 100306660940 + i,
  13. time: 2000 + i
  14. });
  15. }
  16. return result;
  17. };
  18. class App extends React.Component {
  19. state = {
  20. dataSource: getData()
  21. };
  22. render() {
  23. const renderOper = (value, index, record) => {
  24. return (
  25. <a onClick={this.onRemove.bind(this, record.id)}>Remove({record.id})</a>
  26. );
  27. };
  28. return (
  29. <div>
  30. <p>
  31. <Button onClick={this.onAdd}>Add Item</Button>
  32. </p>
  33. <Table dataSource={this.state.dataSource} onRowClick={onRowClick}>
  34. <Table.Column title="Id" dataIndex="id" />
  35. <Table.Column title="Title" dataIndex="title.name" />
  36. <Table.Column title="Time" dataIndex="time" />
  37. <Table.Column cell={renderOper} width="20%" />
  38. </Table>
  39. </div>
  40. );
  41. }
  42. onAdd = () => {
  43. // 注意在没有通过shouldComponentUpdate判断的时候可以使用此写法
  44. // 否则注意数组和对象的引用关系
  45. const { dataSource } = this.state;
  46. dataSource.push({
  47. title: {
  48. name: "Quotation for 1PCS Nano controller compatible"
  49. },
  50. id: Date.now(),
  51. time: 2000
  52. });
  53. this.setState({
  54. dataSource
  55. });
  56. };
  57. onRemove = id => {
  58. const { dataSource } = this.state;
  59. let index = -1;
  60. dataSource.forEach((item, i) => {
  61. if (item.id === id) {
  62. index = i;
  63. }
  64. });
  65. if (index !== -1) {
  66. dataSource.splice(index, 1);
  67. this.setState({
  68. dataSource
  69. });
  70. }
  71. };
  72. }
  73. ReactDOM.render(<App />, mountNode);

可展开-复杂

可以通过 expandedRowRender 额外渲染行,但是会包含复杂的组件

Table 表格 - 图6

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. /*eslint-disable react/prop-types, react/no-multi-comp*/
  3. class ExpandedApp extends React.Component {
  4. constructor(props) {
  5. super(props);
  6. this.state = {
  7. dataSource: this.props.dataSource
  8. };
  9. }
  10. render() {
  11. const style = {
  12. borderTop: "1px solid #eee",
  13. textAlign: "center",
  14. background: "#f8f8f8",
  15. lineHeight: "28px"
  16. };
  17. return (
  18. <div style={{ marginTop: 10 }}>
  19. <Table
  20. dataSource={this.state.dataSource}
  21. hasHeader={false}
  22. hasBorder={false}
  23. >
  24. <Table.Column title="Title" dataIndex="title" />
  25. <Table.Column title="Time" dataIndex="time" width={200} />
  26. </Table>
  27. <p style={style} onClick={this.load.bind(this)}>
  28. Load more data.
  29. </p>
  30. </div>
  31. );
  32. }
  33. load() {
  34. let { dataSource } = this.state;
  35. dataSource = dataSource.concat(dataSource);
  36. this.setState({ dataSource });
  37. }
  38. }
  39. const getData = () => {
  40. let result = [];
  41. for (let i = 0; i < 5; i++) {
  42. result.push({
  43. title: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`,
  44. id: 100306660940 + i,
  45. time: 2000 + i,
  46. children: [
  47. {
  48. title: `Sub title for Quotation ${3 + i}`,
  49. time: 2000 + i
  50. },
  51. {
  52. title: `Sub2 title for Quotation ${3 + i}`,
  53. time: 2000 + i
  54. }
  55. ]
  56. });
  57. }
  58. return result;
  59. },
  60. render = (value, index, record) => {
  61. return <a>Remove({record.id})</a>;
  62. },
  63. expandedRowRender = record => {
  64. let children = record.children;
  65. return <ExpandedApp dataSource={children} />;
  66. };
  67. class App extends React.Component {
  68. constructor(props) {
  69. super(props);
  70. this.state = {
  71. dataSource: getData(),
  72. hasBorder: false,
  73. expandedRowKeys: []
  74. };
  75. }
  76. render() {
  77. const renderTitle = (value, index, record) => {
  78. return (
  79. <div>
  80. {value}
  81. <span onClick={this.toggleExpand.bind(this, record)}>+++++</span>
  82. </div>
  83. );
  84. };
  85. return (
  86. <span>
  87. <p>
  88. {" "}
  89. <Button onClick={this.disabledExpandedCol.bind(this)}>
  90. {" "}
  91. 设置第4行禁用{" "}
  92. </Button>{" "}
  93. &nbsp;
  94. <Button onClick={this.toggleCol.bind(this)}> 隐藏+号 </Button>
  95. </p>
  96. <Table
  97. dataSource={this.state.dataSource}
  98. isZebra={this.state.isZebra}
  99. hasBorder={this.state.hasBorder}
  100. onSort={this.onSort.bind(this)}
  101. expandedRowRender={expandedRowRender}
  102. expandedRowIndent={[1, 1]}
  103. expandedRowKeys={this.state.expandedRowKeys}
  104. getExpandedColProps={this.state.getExpandedColProps}
  105. hasExpandedRowCtrl={this.state.hasExpandedRowCtrl}
  106. onExpandedChange={this.onExpandedChange.bind(this)}
  107. >
  108. <Table.Column title="Id" dataIndex="id" sortable />
  109. <Table.Column title="Title" dataIndex="title" cell={renderTitle} />
  110. <Table.Column title="Time" dataIndex="time" width={200} />
  111. <Table.Column cell={render} width={200} />
  112. </Table>
  113. </span>
  114. );
  115. }
  116. onSort(dataIndex, order) {
  117. let dataSource = this.state.dataSource.sort(function(a, b) {
  118. let result = a[dataIndex] - b[dataIndex];
  119. return order === "asc" ? (result > 0 ? 1 : -1) : result > 0 ? -1 : 1;
  120. });
  121. this.setState({
  122. dataSource
  123. });
  124. }
  125. disabledExpandedCol() {
  126. this.setState({
  127. getExpandedColProps: (record, index) => {
  128. if (index === 3) {
  129. return {
  130. disabled: true
  131. };
  132. }
  133. }
  134. });
  135. }
  136. toggleCol() {
  137. this.setState({
  138. hasExpandedRowCtrl: false
  139. });
  140. }
  141. onExpandedChange(expandedRowKeys) {
  142. this.setState({ expandedRowKeys });
  143. }
  144. toggleExpand(record) {
  145. let key = record.id,
  146. { expandedRowKeys } = this.state,
  147. index = expandedRowKeys.indexOf(key);
  148. if (index > -1) {
  149. expandedRowKeys.splice(index, 1);
  150. } else {
  151. expandedRowKeys.push(key);
  152. }
  153. this.setState({
  154. expandedRowKeys: expandedRowKeys
  155. });
  156. }
  157. }
  158. ReactDOM.render(<App />, mountNode);

可展开

可以通过 expandedRowRender 额外渲染行

Table 表格 - 图7

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. const getData = () => {
  3. let result = [];
  4. for (let i = 0; i < 5; i++) {
  5. result.push({
  6. title: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`,
  7. id: 100306660940 + i,
  8. time: 2000 + i
  9. });
  10. }
  11. return result;
  12. },
  13. render = (value, index, record) => {
  14. return <a>Remove({record.id})</a>;
  15. };
  16. class App extends React.Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. dataSource: getData()
  21. };
  22. }
  23. render() {
  24. return (
  25. <div>
  26. <p>
  27. <Button onClick={this.toggleIndent.bind(this)}>
  28. {" "}
  29. 设置缩进为左二右一{" "}
  30. </Button>
  31. </p>
  32. <Table
  33. dataSource={this.state.dataSource}
  34. isZebra={this.state.isZebra}
  35. hasBorder={false}
  36. onSort={this.onSort.bind(this)}
  37. expandedRowRender={record => record.title}
  38. onRowClick={() => console.log("rowClick")}
  39. onExpandedRowClick={() => console.log("expandedRowClick")}
  40. expandedRowIndent={this.state.expandedRowIndent}
  41. >
  42. <Table.Column title="Id" dataIndex="id" sortable />
  43. <Table.Column title="Title" dataIndex="title" />
  44. <Table.Column title="Time" dataIndex="time" />
  45. <Table.Column cell={render} width={200} />
  46. </Table>
  47. </div>
  48. );
  49. }
  50. onSort(dataIndex, order) {
  51. let dataSource = this.state.dataSource.sort(function(a, b) {
  52. let result = a[dataIndex] - b[dataIndex];
  53. return order === "asc" ? (result > 0 ? 1 : -1) : result > 0 ? -1 : 1;
  54. });
  55. this.setState({
  56. dataSource
  57. });
  58. }
  59. toggleIndent() {
  60. this.setState({
  61. expandedRowIndent: [2, 1]
  62. });
  63. }
  64. toggleCol() {
  65. this.setState({
  66. hasExpandedRowCtrl: false
  67. });
  68. }
  69. }
  70. ReactDOM.render(<App />, mountNode);

排序与过滤

示例演示了排序和过滤的特性

Table 表格 - 图8

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. const getData = () => {
  3. let result = [];
  4. for (let i = 0; i < 5; i++) {
  5. result.push({
  6. title: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`,
  7. id: 100306660940 + i,
  8. time: 2000 + i
  9. });
  10. }
  11. return result;
  12. },
  13. render = (value, index, record) => {
  14. return <a>Remove({record.id})</a>;
  15. };
  16. class App extends React.Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. dataSource: getData(),
  21. filterMode: "multiple"
  22. };
  23. }
  24. render() {
  25. let filters = [
  26. {
  27. label: "Nano 包含3",
  28. value: 3
  29. },
  30. {
  31. label: "Nano 包含2",
  32. value: 2,
  33. children: [
  34. {
  35. label: "Nano 包含12",
  36. value: 22
  37. },
  38. {
  39. label: "Nano 包含23",
  40. value: 23
  41. }
  42. ]
  43. },
  44. {
  45. label: "其他",
  46. children: [
  47. {
  48. label: "Nano 包含4",
  49. value: 4
  50. },
  51. {
  52. label: "Nano 包含5",
  53. value: 5
  54. }
  55. ]
  56. }
  57. ];
  58. return (
  59. <div>
  60. <p>
  61. <Button onClick={this.changeMode.bind(this)}>
  62. 切换过滤为单选模式
  63. </Button>
  64. </p>
  65. <Table
  66. dataSource={this.state.dataSource}
  67. onSort={this.onSort.bind(this)}
  68. onFilter={this.onFilter.bind(this)}
  69. >
  70. <Table.Column title="Id" dataIndex="id" sortable />
  71. <Table.Column
  72. title="Title"
  73. dataIndex="title"
  74. filters={filters}
  75. filterMode={this.state.filterMode}
  76. />
  77. <Table.Column title="Time" dataIndex="time" />
  78. <Table.Column cell={render} width={200} />
  79. </Table>
  80. </div>
  81. );
  82. }
  83. onSort(dataIndex, order) {
  84. let dataSource = this.state.dataSource.sort(function(a, b) {
  85. let result = a[dataIndex] - b[dataIndex];
  86. return order === "asc" ? (result > 0 ? 1 : -1) : result > 0 ? -1 : 1;
  87. });
  88. this.setState({
  89. dataSource
  90. });
  91. }
  92. onFilter(filterParams) {
  93. let dataSource = getData();
  94. for (let key in filterParams) {
  95. let selectedKeys = filterParams[key].selectedKeys;
  96. if (selectedKeys.length) {
  97. dataSource = dataSource.filter(record => {
  98. return selectedKeys.some(value => {
  99. return record[key].indexOf(value) > -1;
  100. });
  101. });
  102. }
  103. }
  104. this.setState({ dataSource });
  105. }
  106. changeMode() {
  107. this.setState({
  108. filterMode: "single"
  109. });
  110. }
  111. }
  112. ReactDOM.render(<App />, mountNode);

固定表头

表格可以固定表头

Table 表格 - 图9

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. const onRowClick = function(record, index, e) {
  3. console.log(record, index, e);
  4. },
  5. getData = length => {
  6. let result = [];
  7. for (let i = 0; i < length; i++) {
  8. result.push({
  9. title: {
  10. name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`
  11. },
  12. id: 100306660940 + i,
  13. time: 2000 + i
  14. });
  15. }
  16. return result;
  17. },
  18. render = (value, index, record) => {
  19. return <a>Remove({record.id})</a>;
  20. };
  21. class App extends React.Component {
  22. state = {
  23. dataSource: getData(10)
  24. };
  25. render() {
  26. return (
  27. <div>
  28. <p>
  29. <Button onClick={this.reduceContent.bind(this)}>切换到2条数据</Button>
  30. </p>
  31. <Table
  32. dataSource={this.state.dataSource}
  33. onRowClick={onRowClick}
  34. fixedHeader
  35. >
  36. <Table.Column title="Id" dataIndex="id" />
  37. <Table.Column title="Title" dataIndex="title.name" />
  38. <Table.Column title="Time" dataIndex="time" />
  39. <Table.Column cell={render} width={200} />
  40. </Table>
  41. </div>
  42. );
  43. }
  44. reduceContent() {
  45. this.setState({
  46. dataSource: getData(2)
  47. });
  48. }
  49. }
  50. ReactDOM.render(<App />, mountNode);

分组列表

分组列表展现

Table 表格 - 图10

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. let dataSource = [
  3. {
  4. price: "US $2.45",
  5. status: 0,
  6. id: 1,
  7. product: [
  8. {
  9. title:
  10. "2014 New Fashion Novelty Tank Slim Women's Fashion Dresses With Lace",
  11. avatar:
  12. "https://sc01.alicdn.com/kf/HTB1ravHKXXXXXccXVXXq6xXFXXXJ/Chinese-Style-Fashion-Custom-Digital-Print-Silk.jpg_220x220.jpg"
  13. }
  14. ],
  15. children: [
  16. {
  17. price: "US $2.5",
  18. status: 1,
  19. id: 2,
  20. product: [
  21. {
  22. title:
  23. "Free shipping women Casual dresses lady dress plus size 2014",
  24. avatar:
  25. "https://sc02.alicdn.com/kf/HTB1efnNLVXXXXbtXpXXq6xXFXXXN/Light-100-acrylic-fashionabe-snood-shawl-weight.jpg_220x220.jpg"
  26. }
  27. ]
  28. },
  29. {
  30. price: "US $2.5",
  31. status: 1,
  32. id: 3,
  33. product: [
  34. {
  35. title:
  36. "Free shipping women Casual dresses lady dress plus size 2014",
  37. avatar:
  38. "https://sc02.alicdn.com/kf/HTB1efnNLVXXXXbtXpXXq6xXFXXXN/Light-100-acrylic-fashionabe-snood-shawl-weight.jpg_220x220.jpg"
  39. }
  40. ]
  41. }
  42. ]
  43. },
  44. {
  45. price: "US $2.5",
  46. status: 1,
  47. id: 4,
  48. product: [
  49. {
  50. title: "Free shipping women Casual dresses lady dress plus size 2014",
  51. avatar:
  52. "https://sc02.alicdn.com/kf/HTB1efnNLVXXXXbtXpXXq6xXFXXXN/Light-100-acrylic-fashionabe-snood-shawl-weight.jpg_220x220.jpg"
  53. }
  54. ]
  55. }
  56. ],
  57. productRender = function(product) {
  58. return (
  59. <div className="media">
  60. <img src={product[0].avatar} className="media-side" />
  61. <div className="media-content">{product[0].title}</div>
  62. </div>
  63. );
  64. },
  65. priceRender = function(price) {
  66. return <b>{price}</b>;
  67. },
  68. statusRender = function(status) {
  69. if (status) {
  70. return "Already Priced";
  71. } else {
  72. return "No Priced";
  73. }
  74. },
  75. operRender = function() {
  76. return <a href="javascript:;">View</a>;
  77. },
  78. groupHeaderRender = function(record) {
  79. return <div>{record.product[0].title}</div>;
  80. },
  81. getRowClassName = function(record) {
  82. if (record.status === 0) {
  83. return "highlight-row";
  84. }
  85. },
  86. rowSelection = {
  87. onChange: function(selectedKeys) {
  88. console.log(selectedKeys);
  89. }
  90. };
  91. class App extends React.Component {
  92. state = {
  93. hasSelection: false
  94. };
  95. render() {
  96. return (
  97. <div>
  98. <p>
  99. <Button onClick={this.toggleGroupSelection}>
  100. Toggle GroupHeader Selection
  101. </Button>
  102. </p>
  103. <Table
  104. dataSource={dataSource}
  105. getCellProps={(rowIndex, colIndex) => {
  106. if (
  107. (colIndex === 3 && rowIndex === 1) ||
  108. (colIndex === 4 && rowIndex === 1)
  109. ) {
  110. return {
  111. colSpan: 1,
  112. rowSpan: 2
  113. };
  114. }
  115. }}
  116. getRowClassName={getRowClassName}
  117. rowSelection={rowSelection}
  118. >
  119. <Table.GroupHeader
  120. cell={groupHeaderRender}
  121. hasSelection={this.state.hasSelection}
  122. />
  123. <Table.Column
  124. cell={productRender}
  125. title="Product Details"
  126. dataIndex="product"
  127. />
  128. <Table.Column
  129. cell={priceRender}
  130. title="Price"
  131. dataIndex="price"
  132. width={120}
  133. />
  134. <Table.Column
  135. cell={statusRender}
  136. title="Status"
  137. dataIndex="status"
  138. width={100}
  139. />
  140. <Table.Column cell={operRender} title="" width={100} />
  141. </Table>
  142. </div>
  143. );
  144. }
  145. toggleGroupSelection = () => {
  146. this.setState({
  147. hasSelection: !this.state.hasSelection
  148. });
  149. };
  150. }
  151. ReactDOM.render(<App />, mountNode);
  1. .media-side{
  2. width:48px;
  3. height:48px;
  4. float:left;
  5. margin-right:10px;
  6. }
  7. .media-content{
  8. overflow: hidden;
  9. vertical-align: top;
  10. }
  11. .media{
  12. overflow: hidden;
  13. }
  14. .next-table .highlight-row .next-table-group-header td{
  15. background: #E8F6FF;
  16. }
  17. .next-table .highlight-row td{
  18. border-color: #D3E9F7;
  19. }

锁列

演示表格锁列的功能

Table 表格 - 图11

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. const onRowClick = function(record, index, e) {
  3. console.log(record, index, e);
  4. },
  5. getData = () => {
  6. let result = [];
  7. for (let i = 0; i < 100; i++) {
  8. result.push({
  9. title: {
  10. name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`
  11. },
  12. id: 100306660940 + i,
  13. time: 2000 + i
  14. });
  15. }
  16. return result;
  17. },
  18. render = (value, index, record) => {
  19. return <a>Remove({record.id})</a>;
  20. };
  21. class App extends React.Component {
  22. state = {
  23. dataSource: [],
  24. cols: [
  25. <Table.Column
  26. title="Title"
  27. dataIndex="title.name"
  28. width={400}
  29. key="name1"
  30. lock
  31. />,
  32. <Table.ColumnGroup title="abc" key="name-group">
  33. <Table.Column
  34. title="Title"
  35. dataIndex="title.name"
  36. width={100}
  37. key="name2"
  38. />
  39. <Table.Column
  40. title="Title"
  41. dataIndex="title.name"
  42. width={400}
  43. key="name3"
  44. />
  45. </Table.ColumnGroup>,
  46. <Table.Column title="Time" dataIndex="time" width={500} key="time" />
  47. ],
  48. isLoading: true
  49. };
  50. render() {
  51. let filters = [
  52. {
  53. label: "Nano 包含3",
  54. value: 3
  55. },
  56. {
  57. label: "Nano 包含2",
  58. value: 2,
  59. children: [
  60. {
  61. label: "Nano 包含12",
  62. value: 22
  63. },
  64. {
  65. label: "Nano 包含23",
  66. value: 23
  67. }
  68. ]
  69. },
  70. {
  71. label: "其他",
  72. children: [
  73. {
  74. label: "Nano 包含4",
  75. value: 4
  76. },
  77. {
  78. label: "Nano 包含5",
  79. value: 5
  80. }
  81. ]
  82. }
  83. ];
  84. return (
  85. <div>
  86. <p>
  87. <Button onClick={this.reduceCol}>Reduce Cols</Button>
  88. </p>
  89. <Table
  90. dataSource={this.state.dataSource}
  91. onRowClick={onRowClick}
  92. fixedHeader
  93. isLoading={this.state.isLoading}
  94. >
  95. <Table.Column
  96. title="Id-Id-Id-Id-Id-Id-Id-Id-Id-Id-Id-Id"
  97. dataIndex="id"
  98. lock
  99. width={140}
  100. filters={filters}
  101. />
  102. {this.state.cols}
  103. <Table.Column cell={render} width={200} lock="right" />
  104. </Table>
  105. </div>
  106. );
  107. }
  108. componentDidMount() {
  109. setTimeout(() => {
  110. this.setState({
  111. dataSource: getData(),
  112. isLoading: false
  113. });
  114. }, 200);
  115. }
  116. reduceCol = () => {
  117. this.setState({
  118. cols: [
  119. <Table.Column
  120. title="Title"
  121. dataIndex="title.name"
  122. width={400}
  123. key="name1"
  124. lock
  125. />,
  126. <Table.Column title="Time" dataIndex="time" width={100} key="time" />
  127. ]
  128. });
  129. };
  130. }
  131. ReactDOM.render(<App />, mountNode);

混合模式

演示了tree模式和rowSelection模式混合

Table 表格 - 图12

查看源码在线预览

  1. import { Table } from "@icedesign/base";
  2. const data = [
  3. {
  4. key: 1,
  5. name: "a",
  6. age: 32,
  7. address: "我是a",
  8. children: [
  9. {
  10. key: 11,
  11. name: "aa",
  12. age: 33,
  13. address: "我是aa"
  14. },
  15. {
  16. key: 12,
  17. name: "ab",
  18. age: 33,
  19. address: "我是ab",
  20. children: [
  21. {
  22. key: 121,
  23. name: "aba",
  24. age: 33,
  25. address: "我是aba"
  26. }
  27. ]
  28. },
  29. {
  30. key: 13,
  31. name: "ac",
  32. age: 33,
  33. address: "我是ac",
  34. children: [
  35. {
  36. key: 131,
  37. name: "aca",
  38. age: 33,
  39. address: "我是aca",
  40. children: [
  41. {
  42. key: 1311,
  43. name: "acaa",
  44. age: 33,
  45. address: "我是acaa"
  46. },
  47. {
  48. key: 1312,
  49. name: "acab",
  50. age: 33,
  51. address: "我是acab"
  52. }
  53. ]
  54. }
  55. ]
  56. }
  57. ]
  58. },
  59. {
  60. key: 2,
  61. name: "b",
  62. age: 32,
  63. address: "我是b",
  64. children: []
  65. }
  66. ];
  67. const tableMixTree = (
  68. <Table
  69. dataSource={data}
  70. primaryKey="key"
  71. isTree
  72. rowSelection={{ onChange: () => {} }}
  73. >
  74. <Table.Column title="Key" dataIndex="key" />
  75. <Table.Column title="Name" dataIndex="name" />
  76. <Table.Column title="Age" dataIndex="age" />
  77. <Table.Column title="Address" dataIndex="address" />
  78. </Table>
  79. );
  80. const tableMixExpanded = (
  81. <Table
  82. dataSource={data}
  83. primaryKey="key"
  84. expandedRowRender={record => record.address}
  85. rowSelection={{ onChange: () => {} }}
  86. >
  87. <Table.Column title="Key" dataIndex="key" />
  88. <Table.Column title="Name" dataIndex="name" />
  89. <Table.Column title="Age" dataIndex="age" />
  90. <Table.Column title="Address" dataIndex="address" />
  91. </Table>
  92. );
  93. const tableMixSelectionTreeLock = (
  94. <div style={{ width: "500px" }}>
  95. <Table
  96. dataSource={data}
  97. primaryKey="key"
  98. rowSelection={{ onChange: () => {} }}
  99. isTree
  100. >
  101. <Table.Column title="Key" dataIndex="key" width={100} />
  102. <Table.Column title="Name" dataIndex="name" lock width={100} />
  103. <Table.Column title="Age" dataIndex="age" width={200} lock="right" />
  104. <Table.Column title="Address" dataIndex="address" width={200} />
  105. </Table>
  106. </div>
  107. );
  108. const tableMixLock = (
  109. <div style={{ width: "500px" }}>
  110. <Table
  111. dataSource={data}
  112. primaryKey="key"
  113. rowSelection={{ onChange: () => {} }}
  114. >
  115. <Table.Column title="Key" dataIndex="key" width={100} />
  116. <Table.Column title="Name" dataIndex="name" lock width={100} />
  117. <Table.Column title="Age" dataIndex="age" width={200} lock="right" />
  118. <Table.Column title="Address" dataIndex="address" width={200} />
  119. </Table>
  120. </div>
  121. );
  122. const tableMixExpandedLock = (
  123. <div style={{ width: "500px" }}>
  124. <Table
  125. dataSource={data}
  126. primaryKey="key"
  127. rowSelection={{ onChange: () => {} }}
  128. expandedRowRender={record => record.address}
  129. expandedRowIndent={[3, 0]}
  130. >
  131. <Table.Column title="Key" dataIndex="key" width={100} />
  132. <Table.Column title="Name" dataIndex="name" lock width={100} />
  133. <Table.Column title="Age" dataIndex="age" width={200} lock="right" />
  134. <Table.Column title="Address" dataIndex="address" width={200} />
  135. </Table>
  136. </div>
  137. );
  138. const tableMixTreeLock = (
  139. <div style={{ width: "500px" }}>
  140. <Table dataSource={data} primaryKey="key" isTree>
  141. <Table.Column title="Key" dataIndex="key" width={100} />
  142. <Table.Column title="Name" dataIndex="name" lock width={100} />
  143. <Table.Column title="Age" dataIndex="age" width={200} lock="right" />
  144. <Table.Column title="Address" dataIndex="address" width={200} />
  145. </Table>
  146. </div>
  147. );
  148. ReactDOM.render(
  149. <div className="mix-demo">
  150. <div className="row">
  151. <h4>tree和选择混合</h4>
  152. {tableMixTree}
  153. </div>
  154. <div className="row">
  155. <h4>额外渲染和选择混合</h4>
  156. {tableMixExpanded}
  157. </div>
  158. <div className="row">
  159. <h4>tree和锁列和选择混合</h4>
  160. {tableMixSelectionTreeLock}
  161. </div>
  162. <div className="row">
  163. <h4>额外渲染列和锁列和选择混合</h4>
  164. {tableMixExpandedLock}
  165. </div>
  166. <div className="row">
  167. <h4>锁列和选择混合</h4>
  168. {tableMixLock}
  169. </div>
  170. <div className="row">
  171. <h4>tree和锁列混合</h4>
  172. {tableMixTreeLock}
  173. </div>
  174. </div>,
  175. mountNode
  176. );
  1. .mix-demo .row {
  2. margin-top:10px;
  3. }

多表头

多个表头

Table 表格 - 图13

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. const onRowClick = function(record, index, e) {
  3. console.log(record, index, e);
  4. },
  5. getData = j => {
  6. let result = [];
  7. for (let i = 0; i < j; i++) {
  8. result.push({
  9. title: {
  10. name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`
  11. },
  12. id: 100306660940 + i,
  13. time: 2000 + i
  14. });
  15. }
  16. return result;
  17. },
  18. render = (value, index, record) => {
  19. return <a>Remove({record.id})</a>;
  20. };
  21. class App extends React.Component {
  22. state = {
  23. dataSource: getData(15)
  24. };
  25. render() {
  26. return (
  27. <div>
  28. <p>
  29. <Button onClick={this.onClick}>Reduce count</Button>
  30. </p>
  31. <Table
  32. dataSource={this.state.dataSource}
  33. onRowClick={onRowClick}
  34. fixedHeader
  35. maxBodyHeight={400}
  36. >
  37. <Table.Column title="Id1" dataIndex="id" width={140} />
  38. <Table.ColumnGroup>
  39. <Table.Column title="Id2" dataIndex="id" lock width={140} />
  40. <Table.Column title="Title" dataIndex="title.name" width={400} />
  41. <Table.Column title="Title" dataIndex="title.name" width={200} />
  42. </Table.ColumnGroup>
  43. <Table.ColumnGroup>
  44. <Table.Column title="Time" dataIndex="time" width={500} />
  45. <Table.Column cell={render} width={200} lock="right" />
  46. </Table.ColumnGroup>
  47. </Table>
  48. </div>
  49. );
  50. }
  51. onClick = () => {
  52. this.setState({
  53. dataSource: getData(4)
  54. });
  55. };
  56. }
  57. ReactDOM.render(<App />, mountNode);

选择框属性

通过getProps来控制选择框属性

Table 表格 - 图14

查看源码在线预览

  1. import { Table } from "@icedesign/base";
  2. const onChange = function(...args) {
  3. console.log(...args);
  4. },
  5. getData = () => {
  6. let result = [];
  7. for (let i = 0; i < 5; i++) {
  8. result.push({
  9. title: {
  10. name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`
  11. },
  12. id: 100306660940 + i,
  13. time: 2000 + i
  14. });
  15. }
  16. return result;
  17. },
  18. render = (value, index, record) => {
  19. return <a>Remove({record.id})</a>;
  20. },
  21. rowSelection = {
  22. onChange: onChange,
  23. getProps: record => {
  24. return {
  25. disabled: record.id === 100306660942
  26. };
  27. }
  28. };
  29. ReactDOM.render(
  30. <Table dataSource={getData()} rowSelection={rowSelection}>
  31. <Table.Column title="Id" dataIndex="id" />
  32. <Table.Column title="Title" dataIndex="title.name" />
  33. <Table.Column title="Time" dataIndex="time" />
  34. <Table.Column cell={render} width={200} />
  35. </Table>,
  36. mountNode
  37. );

可选择

表格可选择功能

Table 表格 - 图15

查看源码在线预览

  1. import { Table } from "@icedesign/base";
  2. const onChange = function(...args) {
  3. console.log(...args);
  4. },
  5. getData = () => {
  6. let result = [];
  7. for (let i = 0; i < 5; i++) {
  8. result.push({
  9. title: {
  10. name: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`
  11. },
  12. id: 100306660940 + i,
  13. time: 2000 + i
  14. });
  15. }
  16. return result;
  17. },
  18. render = (value, index, record) => {
  19. return <a>Remove({record.id})</a>;
  20. };
  21. ReactDOM.render(
  22. <Table dataSource={getData()} rowSelection={{ onChange: onChange }}>
  23. <Table.Column title="Id" dataIndex="id" />
  24. <Table.Column title="Title" dataIndex="title.name" />
  25. <Table.Column title="Time" dataIndex="time" />
  26. <Table.Column cell={render} width={200} />
  27. </Table>,
  28. mountNode
  29. );

样式

自定义表格边框

Table 表格 - 图16

查看源码在线预览

  1. import { Table, Button } from "@icedesign/base";
  2. const getData = () => {
  3. let result = [];
  4. for (let i = 0; i < 5; i++) {
  5. result.push({
  6. title: `Quotation for 1PCS Nano ${3 + i}.0 controller compatible`,
  7. id: 100306660940 + i,
  8. time: 2000 + i
  9. });
  10. }
  11. return result;
  12. },
  13. render = (value, index, record) => {
  14. return <a>Remove({record.id})</a>;
  15. };
  16. class App extends React.Component {
  17. constructor(props) {
  18. super(props);
  19. this.state = {
  20. dataSource: getData(),
  21. className: "",
  22. align: "left"
  23. };
  24. }
  25. render() {
  26. return (
  27. <span>
  28. <p>
  29. <Button onClick={this.toggleZebra.bind(this)}> Toggle zebra </Button>{" "}
  30. &nbsp;
  31. <Button onClick={this.toggleBorder.bind(this)}>
  32. {" "}
  33. Toggle border
  34. </Button>{" "}
  35. &nbsp;
  36. <Button onClick={this.makeBeauty.bind(this)}>
  37. {" "}
  38. Make second column beauty{" "}
  39. </Button>{" "}
  40. &nbsp;
  41. <Button onClick={this.makeAlign.bind(this)}>
  42. {" "}
  43. Make first column align right{" "}
  44. </Button>{" "}
  45. &nbsp;
  46. </p>
  47. <Table
  48. dataSource={this.state.dataSource}
  49. isZebra={this.state.isZebra}
  50. hasBorder={this.state.hasBorder}
  51. onSort={this.onSort.bind(this)}
  52. >
  53. <Table.Column
  54. title="Id"
  55. dataIndex="id"
  56. sortable
  57. align={this.state.align}
  58. />
  59. <Table.Column
  60. title="Title"
  61. dataIndex="title"
  62. className={this.state.className}
  63. />
  64. <Table.Column title="Time" dataIndex="time" />
  65. <Table.Column cell={render} width={200} />
  66. </Table>
  67. </span>
  68. );
  69. }
  70. onSort(dataIndex, order) {
  71. let dataSource = this.state.dataSource.sort(function(a, b) {
  72. let result = a[dataIndex] - b[dataIndex];
  73. return order === "asc" ? (result > 0 ? 1 : -1) : result > 0 ? -1 : 1;
  74. });
  75. this.setState({
  76. dataSource
  77. });
  78. }
  79. toggleZebra() {
  80. this.setState({
  81. isZebra: !this.state.isZebra
  82. });
  83. }
  84. toggleBorder() {
  85. this.setState({
  86. hasBorder: !this.state.hasBorder
  87. });
  88. }
  89. makeBeauty() {
  90. this.setState({
  91. className: "beauty"
  92. });
  93. }
  94. makeAlign() {
  95. this.setState({
  96. align: "right"
  97. });
  98. }
  99. }
  100. ReactDOM.render(<App />, mountNode);
  1. .beauty{
  2. background: #f7f7f7;
  3. }

相关区块

Table 表格 - 图17

暂无相关区块