使用AgGridReact进行无限滚动

时间:2018-05-23 09:57:38

标签: react-redux ag-grid ag-grid-react

我正在尝试使用ag grid react组件实现无限滚动,但它似乎不起作用。

这是我的实施:

import { AgGridReact } from 'ag-grid-react';
import 'ag-grid/dist/styles/ag-grid.css';
import 'ag-grid/dist/styles/ag-theme-balham.css';   

class TasksGridContainer extends React.Component {
    constructor(props) {
        super(props);

        this.state = {
            loading: true,
            gridOptions: {
                //virtual row model
                rowModelType: 'infinite',
                paginationPageSize: 100,
                cacheOverflowSize: 2,
                maxConcurrentDatasourceRequests: 2,
                infiniteInitialRowCount: 1,
                maxBlocksInCache: 2,
                components: {
                    loadingRenderer: function(params) {
                        console.log('loadingCellRenderer', params);
                        if (params.value !== undefined) {
                            return params.value;
                        } else {
                            return '<img src="https://raw.githubusercontent.com/ag-grid/ag-grid-docs/master/src/images/loading.gif">';
                        }
                    }
                },
                defaultColDef: {
                    editable: false,
                    enableRowGroup: true,
                    enablePivot: true,
                    enableValue: true
                }
            }
        };

    }

    componentDidMount() {
        this.props.actions.getAssignedTasks();
        this.props.actions.getTeamTasks();
    }

    componentWillReceiveProps(newProps) {
        if (this.props.taskView.taskGrid.listOfTasks.length > 0) {
            this.setState({
                loading: false ,
                gridOptions: {
                    datasource:  this.props.taskView.taskGrid.listOfTasks
                }

            });

        }
    }

    render() {

        return (
            <div id="tasks-grid-container">
                <div style={Style.agGrid} id="myGrid" className="ag-theme-balham">
                    <AgGridReact
                        columnDefs={this.props.taskView.taskGrid.myTaskColumns}
                        rowData={this.props.taskView.taskGrid.listOfTasks}
                        gridOptions={this.state.gridOptions}>
                    </AgGridReact>
                </div>
            </div>
        );
    }
}

TasksGridContainer.propTypes = {
    listOfTasks: PropTypes.array,
    actions: PropTypes.object
};

const mapStateToProps = ({ taskView }) => {
    return {
        taskView: {
            taskGrid: {
                listOfTasks: taskView.taskGrid.listOfTasks,
                myTaskColumns: taskView.taskGrid.myTaskColumns,
                teamTaskColumns: taskView.taskGrid.teamTaskColumns
            }

        }

    }
};

const mapDispatchToProps = (dispatch) => {
    return {
        actions: bindActionCreators(taskGridActions, dispatch)
    };
}
module.exports = connect(mapStateToProps, mapDispatchToProps)(TasksGridContainer);
一旦props.taskView.taskGrid.myTaskColumns可用,就会设置

columnDefs。 一个示例columndef:

[
  {
    cellRenderer: "loadingRenderer", checkboxSelection: true, field: "action", headerCheckboxSelection: true, headerCheckboxSelectionFilteredOnly: true, headerName: "Action"
  },
  {
    "activity"headerName: "Activity Name"
  }
]

虽然网格加载正常,但是当我滚动它应该调用“loadingRenderer”组件。但是,当我滚动时,我无法看到任何加载gif。 我在实施中做错了吗?

1 个答案:

答案 0 :(得分:0)

实际问题是没有正确调用道具而且没有onGridReady函数来使用gridAPi。

我修改了代码并开始工作:

<AgGridReact
   components={this.state.components}
   enableColResize={true}
   rowBuffer={this.state.rowBuffer}
   debug={true}
   rowSelection={this.state.rowSelection}
   rowDeselection={true}
   rowModelType={this.state.rowModelType}
   paginationPageSize={this.state.paginationPageSize}
   cacheOverflowSize={this.state.cacheOverflowSize}
   maxConcurrentDatasourceRequests={this.state.maxConcurrentDatasourceRequests}
   infiniteInitialRowCount={this.state.infiniteInitialRowCount}
   maxBlocksInCache={this.state.maxBlocksInCache}
   columnDefs={this.props.columns}
   rowData={this.props.rowData}
   onGridReady={this.onGridReady}
   >
   </AgGridReact>

陈述:

this.state = {
            components: {
                loadingRenderer: function(params) {
                  if (params.value !== undefined) {
                    return params.data.action;
                  } else {
                    return '<img src="https://raw.githubusercontent.com/ag-grid/ag-grid-docs/master/src/images/loading.gif">';
                  }
                }
              },
              rowBuffer: 0,
              rowSelection: "multiple",
              rowModelType: "infinite",
              paginationPageSize: 100,
              cacheOverflowSize: 2,
              maxConcurrentDatasourceRequests: 2,
              infiniteInitialRowCount: 1,
              maxBlocksInCache: 2
        };

onGridReady函数:

 onGridReady = (params, data = []) => {
        this.gridApi = params;
        this.gridColumnApi = params.columnApi;
        this.updateData(params,data);
    }