reactjs - react-virtualized ,在哪里调用 forceUpdateGrid 更新表数据?

标签 reactjs react-virtualized

在对表格数据进行排序时,我似乎无法调用 forceUpdateGrid 来更新表格数据,如 original docs 中所述

我收到这个错误

'forceUpdateGrid' is not defined

我需要在用户点击表格标题时更新表格数据,目前它是在页面滚动后更新的。我在表格中有 3 列,我需要按每个列标题对表格进行排序和更新

import React from 'react';
import { Column, Table, SortDirection, SortIndicator } from 'react-virtualized';
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
import _ from 'underscore';
import 'react-virtualized/styles.css';

import { fakeJson } from './Data/fakeJson';

const datalist  = fakeJson;
const list = datalist; 
class TableComponent2 extends React.Component {

  constructor(){
    super();
     this.state = {
       sortBy: 'username',
       sortDirection: SortDirection.DESC,
       sortedList: list
     }
   }

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

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

    render() {
      return (
        <AutoSizer disableHeight>
          {({ width }) => (
            <Table
              headerHeight={20}
              height={740}
              rowCount={datalist.length}
              rowGetter={({ index }) => this.state.sortedList[index]}
              rowHeight={60}
              width={width}
              sort={this.sort}
              sortBy={this.state.sortBy}
              sortDirection={this.state.sortDirection}
            >
              <Column
                dataKey='id'
                width={200}
                flexGrow={1}
                label='ID'
              />
              <Column
                dataKey='name'
                width={200}
                flexGrow={1}
                label='NAME'
              />
              <Column
                dataKey='username'
                width={200}
                flexGrow={1}
                label='USERNAME'
              />
            </Table>
          )}
        </AutoSizer>
      );
    }
  }

  export default TableComponent2;

最佳答案

forceUpdateGrid 是 Table 组件公开的公共(public)方法,您可以使用我在 Table 上定义的引用,例如 this.tableRef.forceUpdateGrid

import React from 'react';
import { Column, Table, SortDirection, SortIndicator } from 'react-virtualized';
import AutoSizer from 'react-virtualized/dist/commonjs/AutoSizer';
import _ from 'underscore';
import 'react-virtualized/styles.css';

import { fakeJson } from './Data/fakeJson';

const datalist  = fakeJson;
const list = datalist; 
class TableComponent2 extends React.Component {

  constructor(){
    super();
     this.state = {
       sortBy: 'username',
       sortDirection: SortDirection.DESC,
       sortedList: list
     }
   }

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

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

    render() {
      return (
        <AutoSizer disableHeight>
          {({ width }) => (
            <Table
              ref={(ref) => this.tableRef = ref}
              headerHeight={20}
              height={740}
              rowCount={datalist.length}
              rowGetter={({ index }) => this.state.sortedList[index]}
              rowHeight={60}
              width={width}
              sort={this.sort}
              sortBy={this.state.sortBy}
              sortDirection={this.state.sortDirection}
            >
              <Column
                dataKey='id'
                width={200}
                flexGrow={1}
                label='ID'
              />
              <Column
                dataKey='name'
                width={200}
                flexGrow={1}
                label='NAME'
              />
              <Column
                dataKey='username'
                width={200}
                flexGrow={1}
                label='USERNAME'
              />
            </Table>
          )}
        </AutoSizer>
      );
    }
  }

  export default TableComponent2;

关于reactjs - react-virtualized ,在哪里调用 forceUpdateGrid 更新表数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54475248/

相关文章:

reactjs - 功能组件在 setState 更新 Hook 后不重新渲染

mysql - useSelector 函数不更新分派(dispatch)函数后的状态 - react Hook

react-virtualized - 如何在 react 虚拟化表中的单元格之间绘制边框

javascript - React Virtualized 列表在滚动时是否可以捕捉到整行?

reactjs - 项目列表之间的 react 窗口间距

javascript - 图片不存在时不显示下一个/图片组件

javascript - 如何使用调用位置而不是定义位置的上下文执行方法?

reactjs - 在组件重新渲染时删除了 Material-UI 自定义 JSS 样式

reactjs - 在 react 虚拟化列表中使用可折叠面板

javascript - 无限滚动的动态高度( react 虚拟化)