How to add sorting to table with react virtualized?

萝らか妹 提交于 2019-12-05 18:44:08

The code snippet you pasted, like the react-virtualized Table demo page, do the sorting inline, within the render function:

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

This probably isn't what you want for a production app, since it would have to re-sort the data each time a component rendered. Instead you'd probably want to sort the data only once- when the sortBy field or sortDirection change- and then store a sorted version of the data in your component state (or in Redux if you use it).

Table tells you when the data needs to be sorted/resorted by calling the sort prop you provide. In your example above, that means this function is called:

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

Since you're storing the sort criteria in your component state, you can also store the sorted result in state also:

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

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

The only thing that's left is for your rowGetter function to use the sortedList from state rather to retrieve rows.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!