如何通过反应虚拟化向表添加排序?

时间:2017-08-09 13:06:13

标签: javascript reactjs react-virtualized

我正在尝试使用Github上的表格排序演示向我的项目添加排序。

我的代码:

import React from 'react';
import PropTypes from 'prop-types';
import { Table, Column, SortDirection, SortIndicator } from 'react-virtualized';
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';

import 'react-virtualized/styles.css';

class NewTable extends React.Component {
  constructor(props) {
    super(props);

    this.dataList = props.list;

    this.state = {
      headerHeight: 50,
      rowHeight: 25,
      rowCount: this.dataList.length,
      height: 400,
      sortBy: 'columnone',
      sortDirection: SortDirection.ASC,
    };

    this.headerRenderer = this.headerRenderer.bind(this);
    this.sort = this.sort.bind(this);
  }

  isSortEnabled() {
    const list = this.dataList;
    const rowCount = this.state;

    return rowCount <= list.length;
  }

  sort({ sortBy, sortDirection }) {
    this.setState({ sortBy, sortDirection });
  }

  headerRenderer({
    dataKey,
    sortBy,
    sortDirection,
  }) {
    return (
      <div>
        Column One
        {sortBy === dataKey &&
          <SortIndicator sortDirection={sortDirection} />
        }
      </div>
    );
  }

  render() {
    const {
      headerHeight,
      height,
      rowHeight,
      sortBy,
      sortDirection,
    } = this.state;

    const list = this.dataList;
    const sortedList = this.isSortEnabled() ?
      (list.sortBy(item => item[sortBy]).update(list => sortDirection === SortDirection.DESC ?
        list.reverse() : list))
      : list;

    return (
      <AutoSizer disableHeight>
        {({ width }) => (
          <Table
            headerHeight={headerHeight}
            height={height}
            rowCount={list.length}
            rowGetter={({ index }) => sortedList[index]}
            rowHeight={rowHeight}
            sort={this.sort}
            sortBy={sortBy}
            sortDirection={sortDirection}
            width={width}
          >
            <Column
              dataKey='columnone'
              headerRenderer={this.headerRenderer}
              disableSort={!this.isSortEnabled}
              width={200}
              flexGrow={1}
            />
          </Table>
        )}
      </AutoSizer>
    );
  }
}

export default NewTable;

我的代码显示单击时ASC和DESC箭头向上和向下翻转,但实际排序不会发生。我错过了什么?

我真的不明白排序的位置。我看到了这些功能,但我不知道输出的位置。

谢谢!

编辑:数据以JSON格式输入。

1 个答案:

答案 0 :(得分:4)

您粘贴的代码段(如react-virtualized Table demo页面)在render函数中进行内联排序:

const sortedList = this._isSortEnabled()
  ? list
      .sortBy(item => item[sortBy])
      .update(
        list =>
          sortDirection === SortDirection.DESC ? list.reverse() : list
      )
  : list;

这可能不是您想要的生产应用程序,因为每次呈现组件时都必须重新排序数据。相反,您可能只想在sortBy字段或sortDirection更改时对数据进行排序,然后在组件state(或Redux)中存储数据的已排序版本如果你使用它。)

Table通过调用您提供的sort道具,告诉您何时需要对数据进行排序/使用。在上面的示例中,这意味着调用此函数:

sort({ sortBy, sortDirection }) {
  this.setState({ sortBy, sortDirection });
}

由于您在组件状态中存储排序条件,因此您还可以将排序结果存储在状态中:

sort({ sortBy, sortDirection }) {
  const sortedList = list
    .sortBy(item => item[sortBy])
    .update(
      list =>
        sortDirection === SortDirection.DESC ? list.reverse() : list
    );

  this.setState({ sortBy, sortDirection, sortedList });
}

唯一剩下的就是您的rowGetter函数使用sortedList中的state来检索行。