农业网格服务器端无限滚动访问道具

时间:2019-04-07 18:17:07

标签: javascript reactjs ag-grid

我正尝试按照其文档here中的描述来实现ag-grid服务器端行模型。我试图做的是将api调用及其参数作为道具传递给网格组件。问题在于,当尝试通过this.props或通过this.state访问状态时,它们都是未定义的。我的代码如下:

onGridReady(params) {
    this.gridApi = params.api;
    this.gridColumnApi = params.columnApi;

    this.gridApi.showLoadingOverlay();
      var dataSource = {
        rowCount: null,
        getRows: function(params) {
          setTimeout(function() {
            let serviceParams = this.props.dataServiceParams ?  this.props.dataServiceParams.slice() : {};
            serviceParams.pageSize = this.state.paginationPageSize; // this will be the rows returned per service call
            serviceParams.index =  // if there is more than 1 page for the pagesize this is the index/page to return.
            serviceParams.sortAndFilters = gridUtility.combineSortAndFilters(params.sortModel, params.filterModel);

            this.props.dataService(serviceParams)
                .then(out => {
                  var rowsThisPage = out;
                  var lastRow = -1;
                  params.successCallback(rowsThisPage, lastRow);
                });

            params.context.componentParent.gridApi.hideOverlay();
          }, 500);
        }
      };

      params.api.setDatasource(dataSource);
  };

dataService道具包含我的service / api调用,而dataServiceParams具有该服务所需的任何参数。我正在添加其他参数来处理排序,过滤和返回所需的数据页/索引。我在这里想念什么?

1 个答案:

答案 0 :(得分:0)

您需要使用Arrow function来访问父范围的属性。查看下面的getRowssetTimeout代码。

  var dataSource = {
    rowCount: null,
    getRows: (params) => {
      setTimeout(() => {
        let serviceParams = this.props.dataServiceParams ?  this.props.dataServiceParams.slice() : {};
        serviceParams.pageSize = this.state.paginationPageSize; // this will be the rows returned per service call
        serviceParams.index =  // if there is more than 1 page for the pagesize this is the index/page to return.
        serviceParams.sortAndFilters = gridUtility.combineSortAndFilters(params.sortModel, params.filterModel);

        this.props.dataService(serviceParams)
            .then(out => {
              var rowsThisPage = out;
              var lastRow = -1;
              params.successCallback(rowsThisPage, lastRow);
            });

        params.context.componentParent.gridApi.hideOverlay();
      }, 500);
    }
  };