ant-design/components/table/demo/head.md

99 lines
2.3 KiB
Markdown
Raw Normal View History

2016-03-31 09:40:55 +08:00
---
order: 7
2016-08-15 07:54:01 +08:00
title:
en-US: Filter and sorter
zh-CN: 筛选和排序
2016-03-31 09:40:55 +08:00
---
2016-08-15 08:07:03 +08:00
## zh-CN
2016-08-15 07:54:01 +08:00
2016-08-15 08:07:03 +08:00
对某一列数据进行筛选,使用列的 `filters` 属性来指定需要筛选菜单的列,`onFilter` 用于筛选当前数据,`filterMultiple` 用于指定多选和单选。
2016-08-15 07:54:01 +08:00
2016-08-15 08:07:03 +08:00
对某一列数据进行排序,通过指定列的 `sorter` 函数即可启动排序按钮。`sorter: function(a, b) { ... }` a、b 为比较的两个列数据。
2016-08-15 07:54:01 +08:00
2016-08-15 08:07:03 +08:00
## en-US
2016-08-15 07:54:01 +08:00
2016-08-15 08:07:03 +08:00
Use `filters` to generate filter menu in columns, `onFilter` to determine filtered result, and `filterMultiple` to indicate whether it's multiple or single selection.
2016-08-15 08:07:03 +08:00
Use `sorter` to make a column sortable. `sorter` can be a function `function(a, b) { ... }` for sorting data locally.
Uses `defaultSortOrder` to make a column sorted by default.
2017-02-13 10:55:53 +08:00
````jsx
import { Table } from 'antd';
const columns = [{
title: 'Name',
dataIndex: 'name',
filters: [{
text: 'Joe',
value: 'Joe',
}, {
text: 'Jim',
value: 'Jim',
}, {
text: 'Submenu',
value: 'Submenu',
children: [{
text: 'Green',
value: 'Green',
}, {
text: 'Black',
value: 'Black',
2016-03-28 16:00:58 +08:00
}],
}],
2016-08-15 07:54:01 +08:00
// specify the condition of filtering result
// here is that finding the name started with `value`
2016-03-28 16:00:58 +08:00
onFilter: (value, record) => record.name.indexOf(value) === 0,
sorter: (a, b) => a.name.length - b.name.length,
}, {
title: 'Age',
dataIndex: 'age',
defaultSortOrder: 'descend',
2016-03-28 16:00:58 +08:00
sorter: (a, b) => a.age - b.age,
}, {
title: 'Address',
dataIndex: 'address',
filters: [{
text: 'London',
value: 'London',
}, {
text: 'New York',
value: 'New York',
}],
filterMultiple: false,
2016-03-28 16:00:58 +08:00
onFilter: (value, record) => record.address.indexOf(value) === 0,
sorter: (a, b) => a.address.length - b.address.length,
}];
const data = [{
2015-08-28 15:22:09 +08:00
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
}, {
2015-08-28 15:22:09 +08:00
key: '2',
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park',
}, {
2015-08-28 15:22:09 +08:00
key: '3',
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
2015-07-16 18:26:17 +08:00
}, {
2015-08-28 15:22:09 +08:00
key: '4',
name: 'Jim Red',
2015-07-16 18:26:17 +08:00
age: 32,
address: 'London No. 2 Lake Park',
}];
2015-10-19 11:29:36 +08:00
function onChange(pagination, filters, sorter) {
2016-08-15 07:54:01 +08:00
console.log('params', pagination, filters, sorter);
2015-10-19 11:29:36 +08:00
}
2017-12-10 16:20:31 +08:00
ReactDOM.render(
<Table columns={columns} dataSource={data} onChange={onChange} />
, mountNode);
````