我正在尝试实现服务器端对分页/过滤/排序数据的请求,但是文档非常混乱。
该示例提供了无限行模型(https://www.ag-grid.com/javascript-grid-infinite-scrolling/#pagination)为此使用.json
文件的功能。本示例从服务器加载整个.json文件,然后使用客户端函数(sortAndFilter()
,sortData()
,filterData()
)进行排序和过滤。这不是服务器端,而是客户端,因为所有数据都是从服务器完全加载的。该文件是否包含1Gb数据?
在现实世界中,我们不会从服务器加载所有数据(就像示例中加载了整个json文件一样)。每次用户单击下一页,传递页面/过滤器和排序数据的参数以及加载此过滤/排序的数据然后在网格中显示时,我们都会向服务器发出请求。
我缺少什么? Ag-grid会这样做吗?还是我完全迷路了?任何带有模拟服务器的简单示例都会有很大帮助。
有些支持票已打开和关闭(#2237,#1148 ...),但未作说明。我希望有人能说清楚。
答案 0 :(得分:2)
您需要实现数据源对象。您需要在其中指定获取数据的方法。然后使用网格API设置此数据源对象。
app.component.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>
app.component.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);
}
}
这是网格数据源的粗略示例。