【发布时间】:2020-11-27 18:43:59
【问题描述】:
我正在使用 react 和 ant design 3.26 构建我的网站,并使用 antd_table 显示一些数据。现在我需要对表格进行排序,同时将一个特殊行永久保留在表格顶部,并且不受分拣机(既不下降也不上升)。我有一些简单的方法来制作这个效果吗?
我不想用onChange()做一些复杂的功能来管理数据源和状态,只想使用antd-table提供的默认sorter props。
这是一个简单的演示:
import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import "./index.css";
import { Table} from "antd";
const data = [
{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park',
shouldForeverTop: true,
},
{
key: '2',
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park',
shouldForeverTop: false,
},
{
key: '3',
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park',
shouldForeverTop: false,
},
{
key: '4',
name: 'Jim Red',
age: 32,
address: 'London No. 2 Lake Park',
shouldForeverTop: false,
},
];
class App extends React.Component {
render() {
const columns = [
{
title: 'Name',
dataIndex: 'name',
sorter: (a, b) => a.name.length - b.name.length,
},
{
title: 'Age',
dataIndex: 'age',
sorter: (a, b) => a.age - b.age,
},
{
title: 'Address',
dataIndex: 'address',
sorter: (a, b) => a.address.length - b.address.length,
},
];
return (
<Table columns={columns} dataSource={data}/>
);
}
}
ReactDOM.render(<App />, document.getElementById("container"));
【问题讨论】: