如何在反应虚拟化的 table 中排序
how to sort in table of react virtualized
这是project link使用react虚拟化table,
我需要进行排序 column-wise,但在更新列表数组时出现错误,
如何为 table 编写 sort() 函数以在用户单击列 Header 时更新 table ASC、DESC
这是文档,https://github.com/bvaughn/react-virtualized/blob/ae38b6f58478026e6f19d828cad85a05fefd4260/source/Table/Table.example.js#L31-L32
import React from 'react';
import { Column, Table, SortDirection, SortIndicator } from 'react-virtualized';
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
import _ from 'underscore';
import 'react-virtualized/styles.css';
import { fakeJson } from './Data/fakeJson';
const datalist = fakeJson;
const list = datalist;
class TableComponent2 extends React.Component {
constructor(){
super();
this.state = {
sortBy: 'username',
sortDirection: SortDirection.DESC,
sortedList: list
}
}
sort({ sortBy, sortDirection }) {
console.log(list)
const tempList = _.sortBy(list, item => item[sortBy]);
console.log(tempList);
const sortedList = tempList.update(
list =>
sortDirection === SortDirection.DESC ? list.reverse() : list
);
this.setState({ sortBy, sortDirection, sortedList });
}
render() {
return (
<AutoSizer disableHeight>
{({ width }) => (
<Table
headerHeight={20}
height={740}
rowCount={datalist.length}
rowGetter={({ index }) => this.state.sortedList[index]}
rowHeight={60}
width={width}
sort={this.sort}
sortBy={this.state.sortBy}
sortDirection={this.state.sortDirection}
>
<Column
dataKey='id'
width={200}
flexGrow={1}
label='ID'
/>
<Column
dataKey='name'
width={200}
flexGrow={1}
label='NAME'
/>
<Column
dataKey='username'
width={200}
flexGrow={1}
label='USERNAME'
/>
</Table>
)}
</AutoSizer>
);
}
}
export default TableComponent2;
基本上您是在数组上使用 update
方法,而 update
不是 Array
的方法。
const array = [1, 2, 3, 4, 5];
console.log(array.update); // undefined.
array.update(); //error.
你只需要reverse
你的sorteredList
基于你的SortDirection
,这就是你需要设置的状态。
sort({ sortBy, sortDirection }) {
console.log(list)
const tempList = _.sortBy(list, item => item[sortBy]);
console.log(tempList);
const sortedList = sortDirection === SortDirection.DESC ? list.reverse() : list
this.setState({ sortBy, sortDirection, sortedList });
}
这是project link使用react虚拟化table, 我需要进行排序 column-wise,但在更新列表数组时出现错误, 如何为 table 编写 sort() 函数以在用户单击列 Header 时更新 table ASC、DESC 这是文档,https://github.com/bvaughn/react-virtualized/blob/ae38b6f58478026e6f19d828cad85a05fefd4260/source/Table/Table.example.js#L31-L32
import React from 'react';
import { Column, Table, SortDirection, SortIndicator } from 'react-virtualized';
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
import _ from 'underscore';
import 'react-virtualized/styles.css';
import { fakeJson } from './Data/fakeJson';
const datalist = fakeJson;
const list = datalist;
class TableComponent2 extends React.Component {
constructor(){
super();
this.state = {
sortBy: 'username',
sortDirection: SortDirection.DESC,
sortedList: list
}
}
sort({ sortBy, sortDirection }) {
console.log(list)
const tempList = _.sortBy(list, item => item[sortBy]);
console.log(tempList);
const sortedList = tempList.update(
list =>
sortDirection === SortDirection.DESC ? list.reverse() : list
);
this.setState({ sortBy, sortDirection, sortedList });
}
render() {
return (
<AutoSizer disableHeight>
{({ width }) => (
<Table
headerHeight={20}
height={740}
rowCount={datalist.length}
rowGetter={({ index }) => this.state.sortedList[index]}
rowHeight={60}
width={width}
sort={this.sort}
sortBy={this.state.sortBy}
sortDirection={this.state.sortDirection}
>
<Column
dataKey='id'
width={200}
flexGrow={1}
label='ID'
/>
<Column
dataKey='name'
width={200}
flexGrow={1}
label='NAME'
/>
<Column
dataKey='username'
width={200}
flexGrow={1}
label='USERNAME'
/>
</Table>
)}
</AutoSizer>
);
}
}
export default TableComponent2;
基本上您是在数组上使用 update
方法,而 update
不是 Array
的方法。
const array = [1, 2, 3, 4, 5];
console.log(array.update); // undefined.
array.update(); //error.
你只需要reverse
你的sorteredList
基于你的SortDirection
,这就是你需要设置的状态。
sort({ sortBy, sortDirection }) {
console.log(list)
const tempList = _.sortBy(list, item => item[sortBy]);
console.log(tempList);
const sortedList = sortDirection === SortDirection.DESC ? list.reverse() : list
this.setState({ sortBy, sortDirection, sortedList });
}