【问题标题】:react virtualized grid sort icon反应虚拟化网格排序图标
【发布时间】:2018-01-06 16:56:14
【问题描述】:

我已经在 react-virtualized 的网格上实现了,用户可以通过单击图标对值进行排序。我希望该图标与react-bootstrap table 上的排序图标具有相同的行为。

目前,我的图标正在使用 this。当我单击列字符串时,列号不会回到圆形图标。

这是我的排序组件:

class SortColumn extends React.Component {
  constructor(props) {
    super(props);
    this.onClick = this.onClick.bind(this);
    this.state = {
      sortIcon: 'fa fa-circle',
      id: null,
    };
  }

  onClick() {
    const { columnId, sort } = this.props;
    const newSort = sort;
    newSort.id = columnId;

    if (sort.asc === true) {
      newSort.asc = false;
      this.setState({
        sortIcon: 'fa fa-chevron-down',
        id: columnId,
      });
    } else if (sort.asc === false) {
      newSort.asc = true;
      this.setState({
        sortIcon: 'fa fa-chevron-up',
        id: columnId,
      });
    }
    this.props.updateSort(newSort);
  }

  render() {
    return (
      <i
        onClick={this.onClick}
        styleName="columnSettingsSort"
        className={this.state.sortIcon} aria-hidden="true"
      />
    );
  }
}

你有什么想法吗?

【问题讨论】:

    标签: javascript reactjs sorting react-virtualized


    【解决方案1】:

    听起来您希望标题图标的工作方式类似于react-virtualized Table headers work

    对此的基本方法要求您在Grid 级别跟踪sort-bysort-direction。您似乎在列级别的组件状态中跟踪它,这将遇到您提到的问题。

    最简单的解决方法是移动组件状态以跟踪当前排序标准列组件进入updateSort回调。然后将排序标准作为道具传递给每个列组件。这样,当单击新的排序方向时,将(通过新的道具)通知旧列它不再处于活动状态。

    【讨论】:

    • 我不确定我是否理解了所有内容。这就是我所做的。我有3个组件,GridView渲染ColumnHeader,渲染SortColumn。我在我的 GridView 组件中放入了 columnId 和 sort-direction 的状态。我已经将 GridView 的排序方向作为道具传递给了 SortColumn。在 SortColumn 的渲染中,我正在检查 prop sort-direction 是升序还是降序,并显示相应的图标。现在,每次我单击 SortColumn 时,它都会更改所有列的图标。我是否遗漏或误解了什么?
    • 听起来你很接近了!您还需要存储 按列排序 本身。这样您就知道只在当前排序的列上显示向上/向下箭头。其他人应该显示他们在不活动时显示的任何默认 UI。有意义吗?
    猜你喜欢
    • 2018-01-17
    • 2019-06-24
    • 2018-01-22
    • 2017-05-05
    • 2017-01-06
    • 2020-03-24
    • 2019-03-09
    • 2019-06-12
    • 2022-01-10
    相关资源
    最近更新 更多