Ag-grid 服务器端分页/过滤/排序数据

Joe*_*oel 5 ag-grid angular

我正在尝试实现对分页/过滤/排序数据的服务器端请求,但文档非常混乱。

该示例在无限行模型(https://www.ag-grid.com/javascript-grid-infinite-scrolling/#pagination)中提供了.json为此使用的文件。此示例从服务器加载整个 .json 文件,然后使用客户端函数 ( sortAndFilter()sortData()filterData()) 进行排序和过滤。这不是服务器端,而是客户端,因为所有数据都是从服务器完全加载的。如果这个文件有 1Gb 的数据呢?

在现实场景中,我们不会从服务器加载所有数据(就像示例中加载整个 json 文件一样)。每次用户单击下一页时,我们都会向服务器发出请求,传递页面/过滤器和排序数据的参数,并加载此过滤/排序的数据,然后显示在网格中。

我缺少什么?Ag-grid 的做法是否有所不同,还是我完全迷失了?任何带有模拟服务器的简单示例都会有很大帮助。

有一些支持请求已打开和关闭(#2237、#1148 ...),但没有澄清。我希望有人能澄清这一点。

Rom*_*dan 4

您需要实现数据源对象。需要指定获取数据的方法。然后使用网格 API 设置此数据源对象。

应用程序组件.html:

<div style="display:flex; width:100%; height:100%; flex-direction:column; position:absolute;">

  <div style="flex-grow:0">
    <p>{{ info }}</p>
  </div>

  <div style="flex-grow:1; height: 1px;">
    <ag-grid-angular style="width: 100%; height: 100%" class="ag-theme-balham"
                      [gridOptions]="gridOptions"
                      [columnDefs]="columnDefs"
                      (gridReady)="onGridReady($event)"
                      #grid
    ></ag-grid-angular>
  </div>

</div>
Run Code Online (Sandbox Code Playgroud)

应用程序组件.ts

import { Component, HostListener, Input, ViewChild } from '@angular/core';
import { GridOptions, IDatasource, IGetRowsParams, ColDef } from 'ag-grid';
import { AgGridNg2 } from 'ag-grid-angular';
import { Observable } from 'rxjs';
import 'rxjs/add/observable/of';


@Component({
  selector: 'my-app',
  templateUrl: './app.component.html'
})
export class AppComponent {

  public columnDefs: any[];
  public rowData: any[];
  public gridOptions: any;
  public info: string;
  @ViewChild('grid') grid: AgGridNg2;

  constructor() {
    this.columnDefs = [
      { headerName: 'One', field: 'one' },
      { headerName: 'Two', field: 'two' },
      { headerName: 'Three', field: 'three' }
    ];

    this.gridOptions = {
      rowSelection: 'single',
      cacheBlockSize: 100,
      maxBlocksInCache: 2,
      enableServerSideFilter: false,
      enableServerSideSorting: false,
      rowModelType: 'infinite',
      pagination: true, 
      paginationAutoPageSize: true
    };

  }


    private getRowData(startRow: number, endRow: number): Observable<any[]> {
      //this code I'm included there only for example you need to move it to 
      //service
      let params: HttpParams = new HttpParams()
          .append('startRow', `${startRow}`)
          .append('endRow', `${endRow}`);

      return this.http.get(`${this.actionUrl}/GetAll`, {params: params})
          .pipe(
              map((res: any) => res.data)
          );
    }

    onGridReady(params: any) {
      console.log("onGridReady");
      var datasource = {
        getRows: (params: IGetRowsParams) => {
          this.info = "Getting datasource rows, start: " + params.startRow + ", end: " + params.endRow;

          this.getRowData(params.startRow, params.endRow)
                    .subscribe(data => params.successCallback(data));

        }
      };
      params.api.setDatasource(datasource);

    }

}
Run Code Online (Sandbox Code Playgroud)

这是网格数据源的粗略示例。