javascript - 如何使用 react 虚拟化向表添加排序?

标签 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 格式输入。

最佳答案

您粘贴的代码片段,例如 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 Prop 告诉您何时需要对数据进行排序/重新排序。在上面的示例中,这意味着调用此函数:

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 函数使用 state 中的 sortedList 而不是检索行。

关于javascript - 如何使用 react 虚拟化向表添加排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45591557/

相关文章:

javascript - 上传前检测文件大小: NOT html5

javascript - 如何在 AngularJS 中使用 ng-repeat 时显示特定项目内的数据?

javascript - 使用 React 和 Electron - react 路由器错误

javascript - 与 react/redux 共享 actions 和 reducers

react-virtualized - 我如何告诉 Masonry 组件它的单元格已经改变了它们的高度?

javascript 操作作用域链内的值

reactjs - 使用 Material UI 导致无效的钩子(Hook)调用警告

css - 背景隐藏行边框?

javascript - React-Virtualized List 滚动时跳过行

javascript - Aurelia 简单绑定(bind)不起作用