如何在已虚拟化的反应表中进行排序

时间:2019-01-31 13:35:20

标签: reactjs react-virtualized

这是{@ 3}使用反应虚拟化表, 我需要按列进行排序,但是在更新列表数组时出现错误, 当用户单击列标题ASC和DESC时,如何为表编写sort()函数以更新表 这是文档project link

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;

1 个答案:

答案 0 :(得分:0)

基本上,您是在数组上使用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 });
}