【问题标题】:How to add sorting to table with react virtualized?如何通过反应虚拟化向表格添加排序?
【发布时间】:2018-01-17 09:29:09
【问题描述】:

我正在尝试使用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 格式输入。

【问题讨论】:

    标签: javascript reactjs react-virtualized


    【解决方案1】:

    您粘贴的代码 sn-p,如 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 函数使用来自statesortedList 而不是检索行。

    【讨论】:

    • 首先感谢Brian的快速回复,不胜感激。但它仍然没有工作。我什至使用了几乎所有的代码:jsfiddle.net/L69zaLtw/2,正在显示的错误:“list.sortBy 不是函数”。我在这里做错了什么?谢谢
    • 啊,有道理。在我的代码中,list 属性是一个Immutable.List,它有一个sortBy 函数。在你的情况下,如果它是一个数组,你会想要使用 Array.prototype.sort 方法来代替:)
    • sortBysortDirection 道具是否有很大的用途,如果您可以在任何情况下从状态中获取它们?
    • 我收到错误 TypeError: list.sortBy is not a function,但没有文档实现 sortBy 但它在所有源代码中。我该如何解决这个问题?也许我不明白 dataList 是如何被格式化的(或者它是从哪里传递过来的)
    猜你喜欢
    • 2019-06-24
    • 2018-01-06
    • 2019-08-29
    • 2015-04-14
    • 2017-05-05
    • 2019-03-09
    • 2019-06-12
    • 2019-08-01
    • 1970-01-01
    相关资源
    最近更新 更多