2016-03-31 09:40:55 +08:00
---
2016-11-22 10:11:12 +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
---
2015-07-14 15:35:17 +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
2018-01-26 10:39:22 +08:00
使用 `defaultSortOrder` 属性,设置列的默认排序顺序。
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.
2015-07-14 15:35:17 +08:00
2018-04-29 22:08:28 +08:00
Use `sorter` to make a column sortable. `sorter` can be a function of the type `function(a, b) { ... }` for sorting data locally.
2015-07-14 15:35:17 +08:00
2017-11-09 19:30:24 +08:00
Uses `defaultSortOrder` to make a column sorted by default.
2018-04-29 22:08:28 +08:00
If a `sortOrder` or `defaultSortOrder` is specified with the value `ascend` or `descend` , you can access this value from within the function passed to the `sorter` as explained above. Such a function can take the form: `function(a, b, sortOrder) { ... }` .
2017-02-13 10:55:53 +08:00
````jsx
2015-10-28 20:55:49 +08:00
import { Table } from 'antd';
const columns = [{
2016-10-02 08:39:23 +08:00
title: 'Name',
2015-07-14 15:35:17 +08:00
dataIndex: 'name',
filters: [{
2016-10-02 08:39:23 +08:00
text: 'Joe',
value: 'Joe',
2015-07-14 15:35:17 +08:00
}, {
2016-10-02 08:39:23 +08:00
text: 'Jim',
value: 'Jim',
2016-01-10 21:10:10 +08:00
}, {
2016-10-02 08:39:23 +08:00
text: 'Submenu',
value: 'Submenu',
2016-01-10 21:10:10 +08:00
children: [{
2016-10-02 08:39:23 +08:00
text: 'Green',
value: 'Green',
2016-01-10 21:10:10 +08:00
}, {
2016-10-02 08:39:23 +08:00
text: 'Black',
value: 'Black',
2016-03-28 16:00:58 +08:00
}],
2015-07-14 15:35:17 +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,
2015-07-14 15:35:17 +08:00
}, {
2016-10-02 08:39:23 +08:00
title: 'Age',
2015-07-14 15:35:17 +08:00
dataIndex: 'age',
2017-11-09 19:30:24 +08:00
defaultSortOrder: 'descend',
2016-03-28 16:00:58 +08:00
sorter: (a, b) => a.age - b.age,
2015-07-14 15:35:17 +08:00
}, {
2016-10-02 08:39:23 +08:00
title: 'Address',
2015-07-14 15:35:17 +08:00
dataIndex: 'address',
2015-11-03 22:32:36 +08:00
filters: [{
2016-10-02 08:39:23 +08:00
text: 'London',
value: 'London',
2015-11-03 22:32:36 +08:00
}, {
2016-10-02 08:39:23 +08:00
text: 'New York',
value: 'New York',
2015-11-03 22:32:36 +08:00
}],
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,
2015-07-14 15:35:17 +08:00
}];
2015-10-28 20:55:49 +08:00
const data = [{
2015-08-28 15:22:09 +08:00
key: '1',
2016-10-02 08:39:23 +08:00
name: 'John Brown',
2015-07-14 15:35:17 +08:00
age: 32,
2016-10-02 08:39:23 +08:00
address: 'New York No. 1 Lake Park',
2015-07-14 15:35:17 +08:00
}, {
2015-08-28 15:22:09 +08:00
key: '2',
2016-10-02 08:39:23 +08:00
name: 'Jim Green',
2015-07-14 15:35:17 +08:00
age: 42,
2016-10-02 08:39:23 +08:00
address: 'London No. 1 Lake Park',
2015-07-14 15:35:17 +08:00
}, {
2015-08-28 15:22:09 +08:00
key: '3',
2016-10-02 08:39:23 +08:00
name: 'Joe Black',
2015-07-14 15:35:17 +08:00
age: 32,
2016-10-02 08:39:23 +08:00
address: 'Sidney No. 1 Lake Park',
2015-07-16 18:26:17 +08:00
}, {
2015-08-28 15:22:09 +08:00
key: '4',
2016-10-02 08:39:23 +08:00
name: 'Jim Red',
2015-07-16 18:26:17 +08:00
age: 32,
2016-10-02 08:39:23 +08:00
address: 'London No. 2 Lake Park',
2015-07-14 15:35:17 +08:00
}];
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(
2018-06-27 15:55:04 +08:00
< Table columns = {columns} dataSource = {data} onChange = {onChange} / > ,
mountNode);
2015-07-14 15:35:17 +08:00
````