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

46 lines
1.1 KiB
Markdown
Raw Normal View History

2015-07-13 16:22:02 +08:00
# 排序
2015-07-09 20:29:26 +08:00
- order: 5
2015-07-13 16:22:02 +08:00
对某一列数据进行排序,通过指定列的 `sorter` 函数即可启动排序按钮。支持前台排序和后台排序,后台排序会发送请求。
`sorter: function(order) { ... }` order 为排序方向:升序 `ascend`、降序 `descend` 或空字符串。
2015-07-09 20:29:26 +08:00
---
````jsx
2015-07-13 10:29:53 +08:00
var Table = antd.Table;
var columns = [{
2015-07-13 16:22:02 +08:00
title: '姓名(后端排序)',
dataIndex: 'name',
sorter: function(order) {
this.fetch(this.props.dataSource + (order && '?sort=name&order=' + order));
}
2015-07-13 10:29:53 +08:00
}, {
2015-07-13 16:22:02 +08:00
title: '年龄(前端排序)',
2015-07-13 10:29:53 +08:00
dataIndex: 'age',
2015-07-13 16:22:02 +08:00
sorter: function(order) {
if (!order) {
return;
}
this.state.data = this.state.data.sort(function(a, b) {
return (order === 'ascend') ?
(a.age - b.age) : (b.age - a.age);
});
this.setState({
data: this.state.data
});
2015-07-13 10:29:53 +08:00
}
}, {
title: '地址',
dataIndex: 'address'
}];
2015-07-13 16:22:02 +08:00
function resolve(result) {
return result.data;
}
React.render(<Table columns={columns} dataSource="/components/table/demo/data.json" resolve={resolve} />
2015-07-13 10:29:53 +08:00
, document.getElementById('components-table-demo-sort'));
2015-07-09 20:29:26 +08:00
````