如何在虚拟化的反应中向表添加排序?

AO1*_*O17 4 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;
Run Code Online (Sandbox Code Playgroud)

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

我不太了解排序在哪里进行。我看到了函数,但是看不到输出到哪里。

谢谢!

编辑:数据输入为JSON。

bva*_*ghn 5

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

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

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

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

sort({ sortBy, sortDirection }) {
  this.setState({ sortBy, sortDirection });
}
Run Code Online (Sandbox Code Playgroud)

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

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

  this.setState({ sortBy, sortDirection, sortedList });
}
Run Code Online (Sandbox Code Playgroud)

剩下的唯一事情就是您的rowGetter函数使用sortedListfrom state而不是检索行。

  • 嗯,很有道理。在我的代码中,list属性是一个Immmable.List,它具有sortBy函数。在您的情况下,如果它是一个数组,则需要改用`Array.prototype.sort`方法:) (5认同)