2015-07-14 15:35:17 +08:00
|
|
|
|
# 筛选和排序
|
|
|
|
|
|
2015-10-09 17:14:09 +08:00
|
|
|
|
- order: 6
|
2015-07-14 15:35:17 +08:00
|
|
|
|
|
2015-11-03 22:32:36 +08:00
|
|
|
|
对某一列数据进行筛选,使用列的 `filter` 属性来指定需要筛选菜单的列,`onFilter` 用于筛选当前数据,`filterMultiple` 用于指定多选和单选。
|
2015-07-14 15:35:17 +08:00
|
|
|
|
|
|
|
|
|
对某一列数据进行排序,通过指定列的 `sorter` 函数即可启动排序按钮。`sorter: function(a, b) { ... }`, a、b 为比较的两个列数据。
|
|
|
|
|
|
|
|
|
|
---
|
|
|
|
|
|
|
|
|
|
````jsx
|
2015-10-28 20:55:49 +08:00
|
|
|
|
import { Table } from 'antd';
|
|
|
|
|
|
|
|
|
|
const columns = [{
|
2015-07-14 15:35:17 +08:00
|
|
|
|
title: '姓名',
|
|
|
|
|
dataIndex: 'name',
|
|
|
|
|
filters: [{
|
|
|
|
|
text: '姓李的',
|
2016-01-10 21:10:10 +08:00
|
|
|
|
value: '李',
|
2015-07-14 15:35:17 +08:00
|
|
|
|
}, {
|
|
|
|
|
text: '姓胡的',
|
2016-01-10 21:10:10 +08:00
|
|
|
|
value: '胡',
|
|
|
|
|
}, {
|
|
|
|
|
text: '子菜单',
|
|
|
|
|
value: '子菜单',
|
|
|
|
|
children: [{
|
|
|
|
|
text: '姓陈的',
|
|
|
|
|
value: '陈',
|
|
|
|
|
}, {
|
|
|
|
|
text: '姓王的',
|
|
|
|
|
value: '王',
|
2016-03-28 16:00:58 +08:00
|
|
|
|
}],
|
2015-07-14 15:35:17 +08:00
|
|
|
|
}],
|
|
|
|
|
// 指定确定筛选的条件函数
|
|
|
|
|
// 这里是名字中第一个字是 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
|
|
|
|
}, {
|
|
|
|
|
title: '年龄',
|
|
|
|
|
dataIndex: 'age',
|
2016-03-28 16:00:58 +08:00
|
|
|
|
sorter: (a, b) => a.age - b.age,
|
2015-07-14 15:35:17 +08:00
|
|
|
|
}, {
|
|
|
|
|
title: '地址',
|
|
|
|
|
dataIndex: 'address',
|
2015-11-03 22:32:36 +08:00
|
|
|
|
filters: [{
|
|
|
|
|
text: '南湖',
|
2016-03-28 16:00:58 +08:00
|
|
|
|
value: '南湖',
|
2015-11-03 22:32:36 +08:00
|
|
|
|
}, {
|
|
|
|
|
text: '西湖',
|
2016-03-28 16:00:58 +08:00
|
|
|
|
value: '西湖',
|
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',
|
2015-07-16 18:26:17 +08:00
|
|
|
|
name: '胡斌',
|
2015-07-14 15:35:17 +08:00
|
|
|
|
age: 32,
|
2016-03-28 16:00:58 +08:00
|
|
|
|
address: '南湖区湖底公园1号',
|
2015-07-14 15:35:17 +08:00
|
|
|
|
}, {
|
2015-08-28 15:22:09 +08:00
|
|
|
|
key: '2',
|
2015-07-14 15:35:17 +08:00
|
|
|
|
name: '胡彦祖',
|
|
|
|
|
age: 42,
|
2016-03-28 16:00:58 +08:00
|
|
|
|
address: '西湖区湖底公园12号',
|
2015-07-14 15:35:17 +08:00
|
|
|
|
}, {
|
2015-08-28 15:22:09 +08:00
|
|
|
|
key: '3',
|
2015-07-14 15:35:17 +08:00
|
|
|
|
name: '李大嘴',
|
|
|
|
|
age: 32,
|
2016-03-28 16:00:58 +08:00
|
|
|
|
address: '南湖区湖底公园123号',
|
2015-07-16 18:26:17 +08:00
|
|
|
|
}, {
|
2015-08-28 15:22:09 +08:00
|
|
|
|
key: '4',
|
2015-07-16 18:26:17 +08:00
|
|
|
|
name: '李秀莲大嘴哥',
|
|
|
|
|
age: 32,
|
2016-03-28 16:00:58 +08:00
|
|
|
|
address: '西湖区湖底公园123号',
|
2015-07-14 15:35:17 +08:00
|
|
|
|
}];
|
|
|
|
|
|
2015-10-19 11:29:36 +08:00
|
|
|
|
function onChange(pagination, filters, sorter) {
|
|
|
|
|
// 点击分页、筛选、排序时触发
|
|
|
|
|
console.log('各类参数是', pagination, filters, sorter);
|
|
|
|
|
}
|
|
|
|
|
|
2016-03-28 16:00:58 +08:00
|
|
|
|
ReactDOM.render(<Table columns={columns} dataSource={data} onChange={onChange} />, mountNode);
|
2015-07-14 15:35:17 +08:00
|
|
|
|
````
|