index.md 4.3 KB
Newer Older
A
afc163 已提交
1 2 3
# Table

- category: Components
A
afc163 已提交
4
- chinese: 表格
A
afc163 已提交
5
- cols: 1
A
afc163 已提交
6
- type: 展示
A
afc163 已提交
7 8 9

---

A
afc163 已提交
10
展示行列数据。
A
afc163 已提交
11

A
afc163 已提交
12
## 何时使用
A
afc163 已提交
13

A
afc163 已提交
14 15
- 当有大量结构化的数据需要展现时;
- 当需要对数据进行排序、搜索、分页、自定义操作等复杂行为时。
A
afc163 已提交
16

J
jljsj 已提交
17
## 如何使用
A
afc163 已提交
18

A
afc163 已提交
19
指定表格的数据源 `dataSource` 为一个数组。
20 21

```jsx
A
afc163 已提交
22
const dataSource = [{
A
afc163 已提交
23
  key: '1',
24 25 26 27
  name: '胡彦斌',
  age: 32,
  address: '西湖区湖底公园1号'
}, {
A
afc163 已提交
28
  key: '2',
29 30 31 32 33
  name: '胡彦祖',
  age: 42,
  address: '西湖区湖底公园1号'
}];

A
afc163 已提交
34 35 36 37 38 39 40 41 42 43
const columns = [{
  title: '姓名',
  dataIndex: 'name'
}, {
  title: '年龄',
  dataIndex: 'age'
}, {
  title: '住址',
  dataIndex: 'address'
}];
44

A
afc163 已提交
45
<Table dataSource={dataSource} columns={columns} />
46 47
```

A
afc163 已提交
48 49
> 注:`dataSource` 在 `0.11.0` 版本后不再支持远程模式。

J
jljsj 已提交
50 51
## API

A
afc163 已提交
52 53
### Table

54 55 56
| 参数          | 说明                     | 类型            |  可选值             | 默认值  |
|---------------|--------------------------|-----------------|---------------------|---------|
| rowSelection  | 列表项是否可选择         | Object          |                     | false   |
A
afc163 已提交
57
| pagination    | 分页器                   | Object   | 配置项参考 [pagination](/components/pagination),设为 false 时不显示分页 |         |
58
| size          | 正常或迷你类型           | String          | `default` or `small`| default |
A
afc163 已提交
59
| dataSource    | 数据数组                 | Array           |                     |         |
A
afc163 已提交
60
| columns       | 表格列的配置描述,具体项见下表 | Array |                     |    无    |
A
afc163 已提交
61
| rowKey        | 表格列 key 的取值 | Function(recode, index):string |                     |    record.key    |
Z
zhujun24 已提交
62
| expandIconAsCell  | 设置展开 Icon 是否单独一列 | Boolean |                     |    true    |
A
afc163 已提交
63
| onChange      | 分页、排序、筛选变化时触发 | Function(pagination, filters, sorter) |                     |       |
A
afc163 已提交
64
| loading       | 页面是否加载中 | Boolean |                     | false      |
B
Benjy Cui 已提交
65
| locale        | 设置排序、过滤按钮的文字或 `title` | Object         | | [默认值](https://github.com/ant-design/ant-design/issues/575#issuecomment-159169511) |
A
afc163 已提交
66 67 68 69 70 71 72 73

### Column

列描述数据对象,是 columns 中的一项。

| 参数       | 说明                       | 类型            |  可选值             | 默认值  |
|------------|----------------------------|-----------------|---------------------|---------|
| title      | 列头显示文字               | String or React.Element |             |         |
A
afc163 已提交
74
| dataIndex  | 列数据在 data 中对应的 key | String          |                     |         |
75
| colSpan    | 表头列合并,设置为 0 时,不渲染 | Number      |                     |         |
A
afc163 已提交
76
| key        | React 需要的 key           | String          |                     |         |
77
| render     | 生成复杂数据的渲染函数,参数分别为当前列的值,当前列数据,列索引,@return里面可以设置表格[行/列合并](#demo-colspan-rowspan) | Function(text, record, index) {} |            |         |
A
afc163 已提交
78
| filters    | 表头的筛选菜单项           | Array           |                     |         |
A
afc163 已提交
79
| onFilter   | 本地模式下,确定筛选的运行函数 | Function    |                     |         |
80
| filterMultiple | 是否多选 | Boolean    |                                        | true    |
A
afc163 已提交
81
| sorter     | 排序函数,本地排序使用一个函数,需要服务端排序可设为 true | Function or Boolean |  | 无 |
82
| width      | 列宽度 | String or Number |                                        | 无      |
A
afc163 已提交
83
| className  | 列的 className             | String          |                     | 无      |
A
afc163 已提交
84

A
afc163 已提交
85 86
## 注意

A
afc163 已提交
87 88 89 90 91
按照 React 的[规范](http://facebook.github.io/react/docs/multiple-components.html#dynamic-children),所有的组件数组必须绑定 key。在 Table 中,默认将每列数据的 `key` 属性作为唯一的标识。

如果你的数据没有这个属性,务必使用 `rowKey` 来指定数据列的主键。若没有指定,控制台会出现以下的提示,表格组件也会出现各类奇怪的错误。

![](https://os.alipayobjects.com/rmsportal/luLdLvhPOiRpyss.png)
A
afc163 已提交
92 93 94 95 96 97 98 99

```jsx
const rowKey = function(record) {
  return record.uid;  // 比如你的数据主键是 uid
};

return <Table rowKey={rowKey} />;
```