我想通过外部存储来控制网格的排序、过滤和列状态。理想情况下,我想拦截 filterChanged 和 sortChanged 事件,将它们发送到商店,并让商店使用网格 API 以编程方式更新网格。
readonly GRID_CONFIG: GridOptions = {
onSortChanged: (event: SortChangedEvent) => {
// Tell store that the sort changed
this.sortChanged.emit(this.grid.api.getSortModel());
},
onFilterChanged: (event: FilterChangedEvent) => {
// Tell store that the filter changed
this.filterChanged.emit(this.grid.api.getFilterModel());
},
onFilterModified: (event: FilterModifiedEvent) => {
// This isn't useful because it only relates to the floating filters pre-apply...
}
}
不幸的是,一旦调用了 onFilterChanged 和 onSortChanged 事件,网格就已经更新了,因此来自存储的更新是多余的。
最接近我想要的是
isApplyServerSideTransaction
,因为此回调允许取消交易。
isApplyServerSideTransaction: (params: IsApplyServerSideTransactionParams) => {
// Emit model changes to the store so that the store can update the grid
this.sortChanged.emit(this.grid.api.getSortModel());
this.filterChanged.emit(this.grid.api.getFilterModel());
// Do not update the grid (redundant)
return false;
}
但是,这仅支持服务器端行模型完整模式。在我的情况下,这个回调永远不会被触发,因为我使用的是部分模式。
在应用模型更改之前是否有任何其他技巧可以挂钩,或者这只是不支持?
更新:我专门尝试拦截 UI 触发的排序/过滤模型更改。我知道这可以通过自定义过滤器(也许还有自定义标头?)来实现,但我更愿意利用 Ag-Grid 的内置 UI,而不是构建整个自定义副本,只是为了拦截一个事件。
由于您使用的是服务器端行模型,每当网格请求数据时,即每当您过滤/排序/分组等时,都会触发 getRows 回调。因此,如果您想拦截返回到网格的内容,您应该这样做它位于 getRows 回调中。
这可能是一个迟到的回应,但它可能会对某人有所帮助。 isApplyServerSideTransaction(params) 仅当触发 applyServersideTransaction() api 来从网格添加/更新/删除行时触发。