2016-03-31 09:40:55 +08:00
---
2018-12-19 12:07:32 +08:00
order: 6
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
2018-10-18 09:24:17 +08:00
对某一列数据进行排序,通过指定列的 `sorter` 函数即可启动排序按钮。`sorter: function(rowA, rowB) { ... }`, rowA、rowB 为比较的两个行数据。
2016-08-15 07:54:01 +08:00
2021-06-07 11:05:17 +08:00
`sortDirections: ['ascend' | 'descend']` 改变每列可用的排序方式,切换排序时按数组内容依次切换,设置在 table props 上时对所有列生效。你可以通过设置 `['ascend', 'descend', 'ascend']` 禁止排序恢复到默认状态。
2018-12-22 18:16:02 +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
2019-12-06 14:22:54 +08:00
Uses `defaultFilteredValue` to make a column filtered by default.
2019-11-16 18:58:23 +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
2020-06-19 10:18:45 +08:00
`sortDirections: ['ascend' | 'descend']` defines available sort methods for each columns, effective for all columns when set on table props. You can set as `['ascend', 'descend', 'ascend']` to prevent sorter back to default status.
2018-12-22 18:16:02 +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) { ... }` .
2019-05-07 14:57:32 +08:00
```jsx
2015-10-28 20:55:49 +08:00
import { Table } from 'antd';
2019-05-07 14:57:32 +08:00
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',
},
],
},
],
// specify the condition of filtering result
// here is that finding the name started with `value`
onFilter: (value, record) => record.name.indexOf(value) === 0,
sorter: (a, b) => a.name.length - b.name.length,
sortDirections: ['descend'],
},
{
title: 'Age',
dataIndex: 'age',
defaultSortOrder: 'descend',
sorter: (a, b) => a.age - b.age,
},
{
title: 'Address',
dataIndex: 'address',
filters: [
{
text: 'London',
value: 'London',
},
{
text: 'New York',
value: 'New York',
},
],
onFilter: (value, record) => record.address.indexOf(value) === 0,
},
];
const data = [
{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
},
{
key: '2',
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park',
},
{
key: '3',
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
},
{
key: '4',
name: 'Jim Red',
age: 32,
address: 'London No. 2 Lake Park',
},
];
2015-07-14 15:35:17 +08:00
2019-10-15 10:00:16 +08:00
function onChange(pagination, filters, sorter, extra) {
console.log('params', pagination, filters, sorter, extra);
2015-10-19 11:29:36 +08:00
}
2019-05-07 14:57:32 +08:00
ReactDOM.render(< Table columns = {columns} dataSource = {data} onChange = {onChange} / > , mountNode);
```