Table
表格

展示行列数据。

何时使用

  • 当有大量结构化的数据需要展现时;
  • 当需要对数据进行排序、搜索、分页、自定义操作等复杂行为时。

如何使用

指定表格的数据源 dataSource 为一个数组。

const dataSource = [
{
key: '1',
name: '胡彦斌',
age: 32,
address: '西湖区湖底公园1号',
},
{
key: '2',
name: '胡彦祖',
age: 42,
address: '西湖区湖底公园1号',
},
];
const columns = [
{
title: '姓名',
dataIndex: 'name',
key: 'name',
},
{
title: '年龄',
dataIndex: 'age',
key: 'age',
},
{
title: '住址',
dataIndex: 'address',
key: 'address',
},
];
<Table dataSource={dataSource} columns={columns} />;

相关推荐

代码演示

NameAgeAddressTagsAction
John Brown32New York No. 1 Lake ParkNICEDEVELOPER
Jim Green42London No. 1 Lake ParkLOSER
Joe Black32Sydney No. 1 Lake ParkCOOLTEACHER

简单的表格,最后一列是各种操作。

expand codeexpand code
NameAgeAddressTagsAction
First NameLast Name
JohnBrown32New York No. 1 Lake Parknicedeveloper
JimGreen42London No. 1 Lake Parkloser
JoeBlack32Sydney No. 1 Lake Parkcoolteacher

使用 JSX 风格的 API(2.5.0 以后引入)

这个只是一个描述 columns 的语法糖,所以你不能用其他组件去包裹 ColumnColumnGroup

expand codeexpand code
NameAgeAddress
John Brown32New York No. 1 Lake Park
Jim Green42London No. 1 Lake Park
Joe Black32Sydney No. 1 Lake Park
Disabled User99Sydney No. 1 Lake Park

第一列是联动的选择框。可以通过 rowSelection.type 属性指定选择类型,默认为 checkbox

默认点击 checkbox 触发选择行为,需要点击行触发可以参考例子:https://codesandbox.io/s/000vqw38rl

expand codeexpand code
NameAgeAddress
Edward King 032London, Park Lane no. 0
Edward King 132London, Park Lane no. 1
Edward King 232London, Park Lane no. 2
Edward King 332London, Park Lane no. 3
Edward King 432London, Park Lane no. 4
Edward King 532London, Park Lane no. 5
Edward King 632London, Park Lane no. 6
Edward King 732London, Park Lane no. 7
Edward King 832London, Park Lane no. 8
Edward King 932London, Park Lane no. 9

选择后进行操作,完成后清空选择,通过 rowSelection.selectedRowKeys 来控制选中项。

expand codeexpand code
NameAgeAddress
Edward King 032London, Park Lane no. 0
Edward King 132London, Park Lane no. 1
Edward King 232London, Park Lane no. 2
Edward King 332London, Park Lane no. 3
Edward King 432London, Park Lane no. 4
Edward King 532London, Park Lane no. 5
Edward King 632London, Park Lane no. 6
Edward King 732London, Park Lane no. 7
Edward King 832London, Park Lane no. 8
Edward King 932London, Park Lane no. 9

通过 rowSelection.selections 自定义选择项,默认不显示下拉选项,设为 true 时显示默认选择项。

expand codeexpand code
Name
Age
Address
Jim Green42London No. 1 Lake Park
John Brown32New York No. 1 Lake Park
Joe Black32Sydney No. 1 Lake Park
Jim Red32London No. 2 Lake Park

对某一列数据进行筛选,使用列的 filters 属性来指定需要筛选菜单的列,onFilter 用于筛选当前数据,filterMultiple 用于指定多选和单选。

对某一列数据进行排序,通过指定列的 sorter 函数即可启动排序按钮。sorter: function(rowA, rowB) { ... }, rowA、rowB 为比较的两个行数据。

sortDirections: ['ascend' | 'descend']改变每列可用的排序方式,切换排序时按数组内容依次切换,设置在 table props 上时对所有列生效。你可以通过设置 ['ascend', 'descend', 'ascend'] 禁止排序恢复到默认状态。

使用 defaultSortOrder 属性,设置列的默认排序顺序。

expand codeexpand code
Name
Age
Address
John Brown32New York No. 1 Lake Park
Jim Green42London No. 1 Lake Park
Joe Black32Sydney No. 1 Lake Park
Jim Red32London No. 2 Lake Park

可以使用 filterMode 来修改筛选菜单的 UI,可选值有 menu(默认)和 tree

expand codeexpand code
Name
Chinese Score
Math Score
English Score
John Brown986070
Jim Green986689
Joe Black989070
Jim Red889989

column.sorter 支持 multiple 字段以配置多列排序优先级。通过 sorter.compare 配置排序逻辑,你可以通过不设置该函数只启动多列排序的交互形式。

expand codeexpand code
Name
Age
Address
John Brown32New York No. 1 Lake Park
Jim Green42London No. 1 Lake Park
Joe Black32Sydney No. 1 Lake Park
Jim Red32London No. 2 Lake Park

使用受控属性对筛选和排序状态进行控制。

  1. columns 中定义了 filteredValue 和 sortOrder 属性即视为受控模式。
  2. 只支持同时对一列进行排序,请保证只有一列的 sortOrder 属性是生效的。
  3. 务必指定 column.key
expand codeexpand code
Name
Age
Address
John Brown32New York No. 1 Lake Park
Joe Black42London No. 1 Lake Park
Jim Green32Sydney No. 1 Lake Park
Jim Red32London No. 2 Lake Park

通过 filterDropdown 自定义的列筛选功能,并实现一个搜索列的示例。

给函数 clearFilters 添加 boolean 类型参数 closeDropdown,是否关闭筛选菜单,默认为 true。添加 boolean 类型参数 confirm,清除筛选时是否提交已选项,默认 true

expand codeexpand code
Name
Gender
Email
暂无数据

这个例子通过简单的 ajax 读取方式,演示了如何从服务端读取并展现数据,具有筛选、排序等功能以及页面 loading 效果。开发者可以自行接入其他数据处理方式。

另外,本例也展示了筛选排序功能如何交给服务端实现,列不需要指定具体的 onFiltersorter 函数,而是在把筛选和排序的参数发到服务端来处理。

当使用 rowSelection 时,请设置 rowSelection.preserveSelectedRowKeys 属性以保留 key

注意,此示例使用 模拟接口,展示数据可能不准确,请打开网络面板查看请求。

🛎️ 想要 3 分钟实现?试试 ProTable

expand codeexpand code
NameAgeAddress
John Brown32New York No. 1 Lake Park
Jim Green42London No. 1 Lake Park
Joe Black32Sydney No. 1 Lake Park
NameAgeAddress
John Brown32New York No. 1 Lake Park
Jim Green42London No. 1 Lake Park
Joe Black32Sydney No. 1 Lake Park

两种紧凑型的列表,小型列表只用于对话框内。

expand codeexpand code
Header
NameCash AssetsAddress
John Brown¥300,000.00New York No. 1 Lake Park
Jim Green¥1,256,000.00London No. 1 Lake Park
Joe Black¥120,000.00Sydney No. 1 Lake Park

添加表格边框线,页头和页脚。

expand codeexpand code
NameAgeAddressAction
John Brown32New York No. 1 Lake ParkDelete
Jim Green42London No. 1 Lake ParkDelete
Not Expandable29Jiangsu No. 1 Lake ParkDelete
Joe Black32Sydney No. 1 Lake ParkDelete

当表格内容较多不能一次性完全展示时。

expand codeexpand code
NameAge
Address
John Brown32New York No. 1 Lake Park
Jim Green42London No. 1 Lake Park
Not Expandable29Jiangsu No. 1 Lake Park
Joe Black32Sydney No. 1 Lake Park

你可以通过 Table.EXPAND_COLUMNTable.SELECT_COLUMN 来控制选择和展开列的顺序。

expand codeexpand code
RowHeadNameAgeHome phoneAddress
1John Brown320571-2209890918889898989New York No. 1 Lake Park
2Jim Green
3Joe Black320575-2209890918900010002Sydney No. 1 Lake Park
4Jim Red180575-2209890918900010002London No. 2 Lake Park
5Jake White1818900010002Dublin No. 2 Lake Park

表头只支持列合并,使用 column 里的 colSpan 进行设置。

表格支持行/列合并,使用 render 里的单元格属性 colSpan 或者 rowSpan 设值为 0 时,设置的表格不会渲染。

expand codeexpand code
CheckStrictly:
NameAgeAddress
John Brown sr.60New York No. 1 Lake Park
Joe Black32Sydney No. 1 Lake Park

表格支持树形数据的展示,当数据中有 children 字段时会自动展示为树形表格,如果不需要或配置为其他字段可以用 childrenColumnName 进行配置。

可以通过设置 indentSize 以控制每一层的缩进宽度。

expand codeexpand code
NameAgeAddress
Edward King 032London, Park Lane no. 0
Edward King 132London, Park Lane no. 1
Edward King 232London, Park Lane no. 2
Edward King 332London, Park Lane no. 3
Edward King 432London, Park Lane no. 4
Edward King 532London, Park Lane no. 5
Edward King 632London, Park Lane no. 6
Edward King 732London, Park Lane no. 7
Edward King 832London, Park Lane no. 8
Edward King 932London, Park Lane no. 9
Edward King 1032London, Park Lane no. 10
Edward King 1132London, Park Lane no. 11
Edward King 1232London, Park Lane no. 12
Edward King 1332London, Park Lane no. 13
Edward King 1432London, Park Lane no. 14
Edward King 1532London, Park Lane no. 15
Edward King 1632London, Park Lane no. 16
Edward King 1732London, Park Lane no. 17
Edward King 1832London, Park Lane no. 18
Edward King 1932London, Park Lane no. 19
Edward King 2032London, Park Lane no. 20
Edward King 2132London, Park Lane no. 21
Edward King 2232London, Park Lane no. 22
Edward King 2332London, Park Lane no. 23
Edward King 2432London, Park Lane no. 24
Edward King 2532London, Park Lane no. 25
Edward King 2632London, Park Lane no. 26
Edward King 2732London, Park Lane no. 27
Edward King 2832London, Park Lane no. 28
Edward King 2932London, Park Lane no. 29
Edward King 3032London, Park Lane no. 30
Edward King 3132London, Park Lane no. 31
Edward King 3232London, Park Lane no. 32
Edward King 3332London, Park Lane no. 33
Edward King 3432London, Park Lane no. 34
Edward King 3532London, Park Lane no. 35
Edward King 3632London, Park Lane no. 36
Edward King 3732London, Park Lane no. 37
Edward King 3832London, Park Lane no. 38
Edward King 3932London, Park Lane no. 39
Edward King 4032London, Park Lane no. 40
Edward King 4132London, Park Lane no. 41
Edward King 4232London, Park Lane no. 42
Edward King 4332London, Park Lane no. 43
Edward King 4432London, Park Lane no. 44
Edward King 4532London, Park Lane no. 45
Edward King 4632London, Park Lane no. 46
Edward King 4732London, Park Lane no. 47
Edward King 4832London, Park Lane no. 48
Edward King 4932London, Park Lane no. 49
  • 1
  • 2

方便一页内展示大量数据。

需要指定 column 的 width 属性,否则列头和内容可能不对齐。如果指定 width 不生效或出现白色垂直空隙,请尝试建议留一列不设宽度以适应弹性布局,或者检查是否有超长连续字段破坏布局

expand codeexpand code
Full Name
Age
Column 1Column 2Column 3Column 4Column 5Column 6Column 7Column 8Action
John Brown32New York ParkNew York ParkNew York ParkNew York ParkNew York ParkNew York ParkNew York ParkNew York Parkaction
Jim Green40London ParkLondon ParkLondon ParkLondon ParkLondon ParkLondon ParkLondon ParkLondon Parkaction

对于列数很多的数据,可以固定前后的列,横向滚动查看其它数据,需要和 scroll.x 配合使用。

若列头与内容不对齐或出现列重复,请指定固定列的宽度 width。如果指定 width 不生效或出现白色垂直空隙,请尝试建议留一列不设宽度以适应弹性布局,或者检查是否有超长连续字段破坏布局

建议指定 scroll.x 为大于表格宽度的固定值或百分比。注意,且非固定列宽度之和不要超过 scroll.x

注意:v4 版本固定列通过 sticky 实现,IE 11 会降级成横向滚动。

expand codeexpand code
Full NameAgeColumn 1Column 2Column 3Column 4Column 5Column 6Column 7Column 8Action
Edward 032London Park no. 0London Park no. 0London Park no. 0London Park no. 0London Park no. 0London Park no. 0London Park no. 0London Park no. 0action
Edward 132London Park no. 1London Park no. 1London Park no. 1London Park no. 1London Park no. 1London Park no. 1London Park no. 1London Park no. 1action
Edward 232London Park no. 2London Park no. 2London Park no. 2London Park no. 2London Park no. 2London Park no. 2London Park no. 2London Park no. 2action
Edward 332London Park no. 3London Park no. 3London Park no. 3London Park no. 3London Park no. 3London Park no. 3London Park no. 3London Park no. 3action
Edward 432London Park no. 4London Park no. 4London Park no. 4London Park no. 4London Park no. 4London Park no. 4London Park no. 4London Park no. 4action
Edward 532London Park no. 5London Park no. 5London Park no. 5London Park no. 5London Park no. 5London Park no. 5London Park no. 5London Park no. 5action
Edward 632London Park no. 6London Park no. 6London Park no. 6London Park no. 6London Park no. 6London Park no. 6London Park no. 6London Park no. 6action
Edward 732London Park no. 7London Park no. 7London Park no. 7London Park no. 7London Park no. 7London Park no. 7London Park no. 7London Park no. 7action
Edward 832London Park no. 8London Park no. 8London Park no. 8London Park no. 8London Park no. 8London Park no. 8London Park no. 8London Park no. 8action
Edward 932London Park no. 9London Park no. 9London Park no. 9London Park no. 9London Park no. 9London Park no. 9London Park no. 9London Park no. 9action

适合同时展示有大量数据和数据列。

若列头与内容不对齐或出现列重复,请指定固定列的宽度 width。如果指定 width 不生效或出现白色垂直空隙,请尝试建议留一列不设宽度以适应弹性布局,或者检查是否有超长连续字段破坏布局

建议指定 scroll.x 为大于表格宽度的固定值或百分比。注意,且非固定列宽度之和不要超过 scroll.x

expand codeexpand code
Name
OtherCompanyGender
Age
AddressCompany AddressCompany Name
StreetBlock
BuildingDoor No.
John Brown1Lake ParkC2035Lake Street 42SoftLake CoM
John Brown2Lake ParkC2035Lake Street 42SoftLake CoM
John Brown3Lake ParkC2035Lake Street 42SoftLake CoM
John Brown4Lake ParkC2035Lake Street 42SoftLake CoM
John Brown5Lake ParkC2035Lake Street 42SoftLake CoM
John Brown6Lake ParkC2035Lake Street 42SoftLake CoM
John Brown7Lake ParkC2035Lake Street 42SoftLake CoM
John Brown8Lake ParkC2035Lake Street 42SoftLake CoM
John Brown9Lake ParkC2035Lake Street 42SoftLake CoM
John Brown10Lake ParkC2035Lake Street 42SoftLake CoM

columns[n] 可以内嵌 children,以渲染分组表头。

expand codeexpand code
nameageaddressoperation
Edward King 0
32London, Park Lane no. 0Delete
Edward King 1
32London, Park Lane no. 1Delete

带单元格编辑功能的表格。当配合 shouldCellUpdate 使用时请注意闭包问题

expand codeexpand code
nameageaddressoperation
Edward 032London Park no. 0Edit
Edward 132London Park no. 1Edit
Edward 232London Park no. 2Edit
Edward 332London Park no. 3Edit
Edward 432London Park no. 4Edit
Edward 532London Park no. 5Edit
Edward 632London Park no. 6Edit
Edward 732London Park no. 7Edit
Edward 832London Park no. 8Edit
Edward 932London Park no. 9Edit

带行编辑功能的表格。

🛎️ 想要 3 分钟实现?试试 ProTable 的可编辑表格

expand codeexpand code
NamePlatformVersionUpgradedCreatorDateAction
ScreeniOS10.3.4.5654500Jack2014-12-24 23:12:00Publish
DateNameStatusUpgrade StatusAction
2014-12-24 23:12:00This is production nameFinishedUpgraded: 56
2014-12-24 23:12:00This is production nameFinishedUpgraded: 56
2014-12-24 23:12:00This is production nameFinishedUpgraded: 56
ScreeniOS10.3.4.5654500Jack2014-12-24 23:12:00Publish
ScreeniOS10.3.4.5654500Jack2014-12-24 23:12:00Publish
NamePlatformVersionUpgradedCreatorDateAction
ScreeniOS10.3.4.5654500Jack2014-12-24 23:12:00Publish
DateNameStatusUpgrade StatusAction
2014-12-24 23:12:00This is production nameFinishedUpgraded: 56
2014-12-24 23:12:00This is production nameFinishedUpgraded: 56
2014-12-24 23:12:00This is production nameFinishedUpgraded: 56
ScreeniOS10.3.4.5654500Jack2014-12-24 23:12:00Publish
ScreeniOS10.3.4.5654500Jack2014-12-24 23:12:00Publish
NamePlatformVersionUpgradedCreatorDateAction
ScreeniOS10.3.4.5654500Jack2014-12-24 23:12:00Publish
DateNameStatusUpgrade StatusAction
2014-12-24 23:12:00This is production nameFinishedUpgraded: 56
2014-12-24 23:12:00This is production nameFinishedUpgraded: 56
2014-12-24 23:12:00This is production nameFinishedUpgraded: 56
ScreeniOS10.3.4.5654500Jack2014-12-24 23:12:00Publish
ScreeniOS10.3.4.5654500Jack2014-12-24 23:12:00Publish

展示每行数据更详细的信息。

expand codeexpand code
NameAgeAddress
John Brown32Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text
Jim Green42London No. 1 Lake Park
Joe Black32Sidney No. 1 Lake Park

使用自定义元素,我们可以集成 dnd-kit 来实现拖拽排序。

expand codeexpand code
NameAgeAddress
John Brown32Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text Long text
Jim Green42London No. 1 Lake Park
Joe Black32Sidney No. 1 Lake Park

使用 dnd-kit 来实现一个拖拽操作列。

expand codeexpand code
NameAgeAddressLong Column Long Column Long ColumnLong Column Long ColumnLong Column
John Brown32New York No. 1 Lake Park, New York No. 1 Lake ParkNew York No. 1 Lake Park, New York No. 1 Lake ParkNew York No. 1 Lake Park, New York No. 1 Lake ParkNew York No. 1 Lake Park, New York No. 1 Lake Park
Jim Green42London No. 2 Lake Park, London No. 2 Lake ParkLondon No. 2 Lake Park, London No. 2 Lake ParkLondon No. 2 Lake Park, London No. 2 Lake ParkLondon No. 2 Lake Park, London No. 2 Lake Park
Joe Black32Sydney No. 1 Lake Park, Sydney No. 1 Lake ParkSydney No. 1 Lake Park, Sydney No. 1 Lake ParkSydney No. 1 Lake Park, Sydney No. 1 Lake ParkSydney No. 1 Lake Park, Sydney No. 1 Lake Park

设置 column.ellipsis 可以让单元格内容根据宽度自动省略。

列头缩略暂不支持和排序筛选一起使用。

expand codeexpand code
NameAgeAddressLong Column Long Column Long ColumnLong Column Long ColumnLong Column
John Brown32New York No. 1 Lake Park, New York No. 1 Lake ParkNew York No. 1 Lake Park, New York No. 1 Lake ParkNew York No. 1 Lake Park, New York No. 1 Lake ParkNew York No. 1 Lake Park, New York No. 1 Lake Park
Jim Green42London No. 2 Lake Park, London No. 2 Lake ParkLondon No. 2 Lake Park, London No. 2 Lake ParkLondon No. 2 Lake Park, London No. 2 Lake ParkLondon No. 2 Lake Park, London No. 2 Lake Park
Joe Black32Sydney No. 1 Lake Park, Sydney No. 1 Lake ParkSydney No. 1 Lake Park, Sydney No. 1 Lake ParkSydney No. 1 Lake Park, Sydney No. 1 Lake ParkSydney No. 1 Lake Park, Sydney No. 1 Lake Park

设置 column.ellipsis.showTitle 关闭单元格内容自动省略后默认的 title 提示, 使用 Tooltip 替代。

expand codeexpand code
NameBorrowRepayment
John Brown1033
Jim Green1000
Joe Black1010
Jim Red7545
Total19588
Balance107

NameDescription
LightEverything that has a beginning, has an end.
BambooEverything that has a beginning, has an end.
LittleEverything that has a beginning, has an end.
LightEverything that has a beginning, has an end.
BambooEverything that has a beginning, has an end.
LittleEverything that has a beginning, has an end.
LightEverything that has a beginning, has an end.
BambooEverything that has a beginning, has an end.
LittleEverything that has a beginning, has an end.
LightEverything that has a beginning, has an end.
BambooEverything that has a beginning, has an end.
LittleEverything that has a beginning, has an end.
LightEverything that has a beginning, has an end.
BambooEverything that has a beginning, has an end.
LittleEverything that has a beginning, has an end.
LightEverything that has a beginning, has an end.
BambooEverything that has a beginning, has an end.
LittleEverything that has a beginning, has an end.
LightEverything that has a beginning, has an end.
BambooEverything that has a beginning, has an end.
SummaryThis is a summary content

通过 summary 设置总结栏。使用 Table.Summary.Cell 同步 Column 的固定状态。你可以通过配置 Table.Summaryfixed 属性使其固定(4.16.0 支持)。

expand codeexpand code
ABCDEF
0
0
1
1
2
2
3
3
4
4
5
5
6
6

通过 react-window 引入虚拟滚动方案,实现 100000 条数据的高性能表格。

expand codeexpand code
Name (all screens)
John Brown

响应式配置列的展示。

expand codeexpand code
NameAgeAddressTagsAction
John Brown32New York No. 1 Lake ParkNICEDEVELOPER
Jim Green42London No. 1 Lake ParkLOSER
Joe Black32Sydney No. 1 Lake ParkCOOLTEACHER

表格的分页设置。

expand codeexpand code
Full NameAgeColumn 1Column 2Column 3Column 4Column 5Column 6Column 7Column 8Action
Edward 032London Park no. 0London Park no. 0London Park no. 0London Park no. 0London Park no. 0London Park no. 0London Park no. 0London Park no. 0action
Edward 132London Park no. 1London Park no. 1London Park no. 1London Park no. 1London Park no. 1London Park no. 1London Park no. 1London Park no. 1action
Edward 232London Park no. 2London Park no. 2London Park no. 2London Park no. 2London Park no. 2London Park no. 2London Park no. 2London Park no. 2action
Edward 332London Park no. 3London Park no. 3London Park no. 3London Park no. 3London Park no. 3London Park no. 3London Park no. 3London Park no. 3action
Edward 432London Park no. 4London Park no. 4London Park no. 4London Park no. 4London Park no. 4London Park no. 4London Park no. 4London Park no. 4action
Edward 532London Park no. 5London Park no. 5London Park no. 5London Park no. 5London Park no. 5London Park no. 5London Park no. 5London Park no. 5action
Edward 632London Park no. 6London Park no. 6London Park no. 6London Park no. 6London Park no. 6London Park no. 6London Park no. 6London Park no. 6action
Edward 732London Park no. 7London Park no. 7London Park no. 7London Park no. 7London Park no. 7London Park no. 7London Park no. 7London Park no. 7action
Edward 832London Park no. 8London Park no. 8London Park no. 8London Park no. 8London Park no. 8London Park no. 8London Park no. 8London Park no. 8action
Edward 932London Park no. 9London Park no. 9London Park no. 9London Park no. 9London Park no. 9London Park no. 9London Park no. 9London Park no. 9action
Scroll ContextFix Right

对于长表格,需要滚动才能查看表头和滚动条,那么现在可以设置跟随页面固定表头和滚动条。

expand codeexpand code
Name
Age
Address
Action
John Brown12New York No. 1 Lake Park
John Brown22New York No. 2 Lake Park
John Brown32New York No. 3 Lake Park
John Brown42New York No. 4 Lake Park
John Brown52New York No. 5 Lake Park
John Brown62New York No. 6 Lake Park
John Brown72New York No. 7 Lake Park
John Brown82New York No. 8 Lake Park
John Brown92New York No. 9 Lake Park
John Brown102New York No. 10 Lake Park

选择不同配置组合查看效果。

expand codeexpand code

API

通用属性参考:通用属性

Table

参数说明类型默认值版本
bordered是否展示外边框和列边框booleanfalse
columns表格列的配置描述,具体项见下表ColumnsType[]-
components覆盖默认的 table 元素TableComponents-
dataSource数据数组object[]-
expandable配置展开属性expandable-
footer表格尾部function(currentPageData)-
getPopupContainer设置表格内各类浮层的渲染节点,如筛选菜单(triggerNode) => HTMLElement() => TableHtmlElement
loading页面是否加载中boolean | Spin Propsfalse
locale默认文案设置,目前包括排序、过滤、空数据文案object默认值
pagination分页器,参考配置项pagination 文档,设为 false 时不展示和进行分页object | false-
rowClassName表格行的类名function(record, index): string-
rowKey表格行 key 的取值,可以是字符串或一个函数string | function(record): stringkey
rowSelection表格行是否可选择,配置项object-
scroll表格是否可滚动,也可以指定滚动区域的宽、高,配置项object-
showHeader是否显示表头booleantrue
showSorterTooltip表头是否显示下一次排序的 tooltip 提示。当参数类型为对象时,将被设置为 Tooltip 的属性boolean | Tooltip propstrue
size表格大小large | middle | smalllarge
sortDirections支持的排序方式,取值为 ascend descendArray[ascend, descend]
sticky设置粘性头部和滚动条boolean | {offsetHeader?: number, offsetScroll?: number, getContainer?: () => HTMLElement}-4.6.0 (getContainer: 4.7.0)
summary总结栏(currentData) => ReactNode-
tableLayout表格元素的 table-layout 属性,设为 fixed 表示内容不会影响列的布局- | auto | fixed
固定表头/列或使用了 column.ellipsis 时,默认值为 fixed
title表格标题function(currentPageData)-
onChange分页、排序、筛选变化时触发function(pagination, filters, sorter, extra: { currentDataSource: [], action: paginate | sort | filter })-
onHeaderRow设置头部行属性function(columns, index)-
onRow设置行属性function(record, index)-

onRow 用法

适用于 onRow onHeaderRow onCell onHeaderCell

<Table
onRow={(record) => {
return {
onClick: (event) => {}, // 点击行
onDoubleClick: (event) => {},
onContextMenu: (event) => {},
onMouseEnter: (event) => {}, // 鼠标移入行
onMouseLeave: (event) => {},
};
}}
onHeaderRow={(columns, index) => {
return {
onClick: () => {}, // 点击表头行
};
}}
/>

Column

列描述数据对象,是 columns 中的一项,Column 使用相同的 API。

参数说明类型默认值版本
align设置列的对齐方式left | right | centerleft
className列样式类名string-
colSpan表头列合并,设置为 0 时,不渲染number-
dataIndex列数据在数据项中对应的路径,支持通过数组查询嵌套路径string | string[]-
defaultFilteredValue默认筛选值string[]-
filterResetToDefaultFilteredValue点击重置按钮的时候,是否恢复默认筛选值booleanfalse
defaultSortOrder默认排序顺序ascend | descend-
ellipsis超过宽度将自动省略,暂不支持和排序筛选一起使用。
设置为 true{ showTitle?: boolean } 时,表格布局将变成 tableLayout="fixed"
boolean | { showTitle?: boolean }falseshowTitle: 4.3.0
filterDropdown可以自定义筛选菜单,此函数只负责渲染图层,需要自行编写各种交互ReactNode | (props: FilterDropdownProps) => ReactNode-
filterDropdownOpen用于控制自定义筛选菜单是否可见boolean-
filtered标识数据是否经过过滤,筛选图标会高亮booleanfalse
filteredValue筛选的受控属性,外界可用此控制列的筛选状态,值为已筛选的 value 数组string[]-
filterIcon自定义 filter 图标。ReactNode | (filtered: boolean) => ReactNodefalse
filterMultiple是否多选booleantrue
filterMode指定筛选菜单的用户界面'menu' | 'tree''menu'4.17.0
filterSearch筛选菜单项是否可搜索boolean | function(input, record):booleanfalseboolean:4.17.0 function:4.19.0
filters表头的筛选菜单项object[]-
fixed(IE 下无效)列是否固定,可选 true (等效于 left) left rightboolean | stringfalse
keyReact 需要的 key,如果已经设置了唯一的 dataIndex,可以忽略这个属性string-
render生成复杂数据的渲染函数,参数分别为当前行的值,当前行数据,行索引function(text, record, index) {}-
responsive响应式 breakpoint 配置列表。未设置则始终可见。Breakpoint[]-4.2.0
rowScope设置列范围row | rowgroup-5.1.0
shouldCellUpdate自定义单元格渲染时机(record, prevRecord) => boolean-4.3.0
showSorterTooltip表头显示下一次排序的 tooltip 提示, 覆盖 table 中 showSorterTooltipboolean | Tooltip propstrue
sortDirections支持的排序方式,覆盖 TablesortDirections, 取值为 ascend descendArray[ascend, descend]
sorter排序函数,本地排序使用一个函数(参考 Array.sort 的 compareFunction),需要服务端排序可设为 truefunction | boolean | { compare: function, multiple: number }-
sortOrder排序的受控属性,外界可用此控制列的排序,可设置为 ascend descend nullascend | descend | null-
sortIcon自定义 sort 图标(props: { sortOrder }) => ReactNode-5.6.0
title列头显示文字(函数用法 3.10.0 后支持)ReactNode | ({ sortOrder, sortColumn, filters }) => ReactNode-
width列宽度(指定了也不生效?string | number-
onCell设置单元格属性function(record, rowIndex)-
onFilter本地模式下,确定筛选的运行函数function-
onFilterDropdownOpenChange自定义筛选菜单可见变化时调用function(visible) {}-
onHeaderCell设置头部单元格属性function(column)-

ColumnGroup

参数说明类型默认值
title列头显示文字ReactNode-

pagination

分页的配置项。

参数说明类型默认值
position指定分页显示的位置, 取值为topLeft | topCenter | topRight |bottomLeft | bottomCenter | bottomRightArray[bottomRight]

更多配置项,请查看 Pagination

expandable

展开功能的配置。

参数说明类型默认值版本
childrenColumnName指定树形结构的列名stringchildren
columnTitle自定义展开列表头ReactNode-4.23.0
columnWidth自定义展开列宽度string | number-
defaultExpandAllRows初始时,是否展开所有行booleanfalse
defaultExpandedRowKeys默认展开的行string[]-
expandedRowClassName展开行的 classNamefunction(record, index, indent): string-
expandedRowKeys展开的行,控制属性string[]-
expandedRowRender额外的展开行function(record, index, indent, expanded): ReactNode-
expandIcon自定义展开图标,参考示例function(props): ReactNode-
expandRowByClick通过点击行来展开子行booleanfalse
fixed控制展开图标是否固定,可选 true left rightboolean | stringfalse4.16.0
indentSize展示树形数据时,每层缩进的宽度,以 px 为单位number15
rowExpandable设置是否允许行展开(record) => boolean-
showExpandColumn设置是否展示行展开列booleantrue4.18.0
onExpand点击展开图标时触发function(record, event)-
onExpandedRowsChange展开的行变化时触发function(expandedRows)-

rowSelection

选择功能的配置。

参数说明类型默认值版本
checkStrictlycheckable 状态下节点选择完全受控(父子数据选中状态不再关联)booleantrue4.4.0
columnTitle自定义列表选择框标题ReactNode-
columnWidth自定义列表选择框宽度string | number32px
fixed把选择框列固定在左边boolean-
getCheckboxProps选择框的默认属性配置function(record)-
hideSelectAll隐藏全选勾选框与自定义选择项booleanfalse4.3.0
preserveSelectedRowKeys当数据被删除时仍然保留选项的 keyboolean-4.4.0
renderCell渲染勾选框,用法与 Column 的 render 相同function(checked, record, index, originNode) {}-4.1.0
selectedRowKeys指定选中项的 key 数组,需要和 onChange 进行配合string[] | number[][]
defaultSelectedRowKeys默认选中项的 key 数组string[] | number[][]
selections自定义选择项 配置项, 设为 true 时使用默认选择项object[] | booleantrue
type多选/单选checkbox | radiocheckbox
onCell设置单元格属性,用法与 Column 的 onCell 相同function(record, rowIndex)-5.5.0
onChange选中项发生变化时的回调function(selectedRowKeys, selectedRows, info: { type })-info.type: 4.21.0
onSelect用户手动选择/取消选择某行的回调function(record, selected, selectedRows, nativeEvent)-
onSelectAll用户手动选择/取消选择所有行的回调function(selected, selectedRows, changeRows)-
onSelectInvert用户手动选择反选的回调function(selectedRowKeys)-
onSelectNone用户清空选择的回调function()-
onSelectMultiple用户使用键盘 shift 选择多行的回调function(selected, selectedRows, changeRows)-

scroll

参数说明类型默认值
scrollToFirstRowOnChange当分页、排序、筛选变化后是否滚动到表格顶部boolean-
x设置横向滚动,也可用于指定滚动区域的宽,可以设置为像素值,百分比,true 和 'max-content'string | number | true-
y设置纵向滚动,也可用于指定滚动区域的高,可以设置为像素值string | number-

selection

参数说明类型默认值
keyReact 需要的 key,建议设置string-
text选择项显示的文字ReactNode-
onSelect选择项点击回调function(changeableRowKeys)-

在 TypeScript 中使用

import { Table } from 'antd';
import type { ColumnsType } from 'antd/es/table';
import React from 'react';
interface User {
key: number;
name: string;
}
const columns: ColumnsType<User> = [
{
key: 'name',
title: 'Name',
dataIndex: 'name',
},
];
const data: User[] = [
{
key: 0,
name: 'Jack',
},
];
const Demo: React.FC = () => (
<>
<Table<User> columns={columns} dataSource={data} />
{/* 使用 JSX 风格的 API */}
<Table<User> dataSource={data}>
<Table.Column<User> key="name" title="Name" dataIndex="name" />
</Table>
</>
);
export default Demo;

TypeScript 里使用 Table 的 CodeSandbox 实例

Design Token

全局 Token

注意

按照 React 的规范,所有的数组组件必须绑定 key。在 Table 中,dataSourcecolumns 里的数据值都需要指定 key 值。对于 dataSource 默认将每列数据的 key 属性作为唯一的标识。

控制台警告

如果 dataSource[i].key 没有提供,你应该使用 rowKey 来指定 dataSource 的主键,如下所示。若没有指定,控制台会出现以上的提示,表格组件也会出现各类奇怪的错误。

// 比如你的数据主键是 uid
return <Table rowKey="uid" />;
// 或
return <Table rowKey={(record) => record.uid} />;

FAQ

如何在没有数据或只有一页数据时隐藏分页栏

你可以设置 paginationhideOnSinglePage 属性为 true

表格过滤时会回到第一页?

前端过滤时通常条目总数会减少,从而导致总页数小于筛选前的当前页数,为了防止当前页面没有数据,我们默认会返回第一页。

如果你在使用远程分页,很可能需要保持当前页面,你可以参照这个 受控例子 控制当前页面不变。

表格分页为何会出现 size 切换器?

4.1.0 起,Pagination 在 total 大于 50 条时会默认显示 size 切换器以提升用户交互体验。如果你不需要该功能,可以通过设置 showSizeChangerfalse 来关闭。

为什么 更新 state 会导致全表渲染?

由于 columns 支持 render 方法,因而 Table 无法知道哪些单元会受到影响。你可以通过 column.shouldCellUpdate 来控制单元格的渲染。

固定列穿透到最上层该怎么办?

固定列通过 z-index 属性将其悬浮于非固定列之上,这使得有时候你会发现在 Table 上放置遮罩层时固定列会被透过的情况。为遮罩层设置更高的 z-index 覆盖住固定列即可。

如何自定义渲染可选列的勾选框(比如增加 Tooltip)?

4.1.0 起,可以通过 rowSelectionrenderCell 属性控制,可以参考此处 Demo 实现展示 Tooltip 需求或其他自定义的需求。

  • Statistic统计数值Tabs标签页