简体   繁体   English

在 ag-grid 更新后保持过滤器

[英]keep filter after ag-grid update

I'm trying to keep a grid's filter after updating a row.我试图在更新一行后保留网格的过滤器。

When I click on a row in the grid, I open a dialog where I update the informations for this row, and then I look for the clicked row in the grid's rowData after that I update the corresponding record in the rowData with the values from the dialog, as following :当我单击网格中的一行时,我会打开一个对话框,在其中更新该行的信息,然后在网格的 rowData 中查找被单击的行,然后使用来自对话框,如下:

row[0].x = dg.x;
row[0].y = dg.y;
dg.gridOptions.rowData[index] = row[0];
dg.gridOptions.api.setRowData(newRows);

But after this I loose the filter on the grid, I did some search and I tried all the following solutions :但是在此之后我松开了网格上的过滤器,我进行了一些搜索并尝试了以下所有解决方案:

  1. Setting the gridOptions property deltaRowDataMode to true .gridOptions属性deltaRowDataMode设置为true

  2. filterParams: {apply: true, newRowsAction: 'keep'}

But none of these has worked.但这些都没有奏效。

How can I solve this ?我该如何解决这个问题?

Use this method ( gridOptions.api.refreshCells() ) instead of setRowData .使用此方法( gridOptions.api.refreshCells() )而不是setRowData Or if you need to use setRowData , save the filter model before the call and apply the filter again afterwards using these methods:或者,如果您需要使用setRowData ,请在调用之前保存过滤器模型,然后使用以下方法再次应用过滤器:

const model = this.gridOptions.api.getFilterModel();
//some code
this.gridOptions.api.setFilterModel(model);

You can set the filterParams newRowsAction to keep, like that您可以将 filterParams newRowsAction 设置为保留,就像这样

dg.defaultColDef = { filterParams: { newRowsAction: 'keep'}} ;

refer to https://www.ag-grid.com/javascript-grid-filtering/index.php参考https://www.ag-grid.com/javascript-grid-filtering/index.php

I had same issue and this what I did to resolve it (kinda hacky).我有同样的问题,这就是我为解决它所做的(有点 hacky)。

  1. Subscribe to rowDataChanged event of the ag grid.订阅 ag 网格的 rowDataChanged 事件。
(rowDataChanged)="onRowDataChanged($event)"
  1. Inside the onRowDataChanged put your filtration logiconRowDataChanged里面放你的过滤逻辑
// Get a reference to the name filter instance
const filterInstance = this.gridApi.getFilterInstance('fieldNameHere');
// Call some methods on Set Filter API that don't apply the filter
filterInstance.setModel({
    type: 'contains', // type of filter
    filter: 'yourData' // set filter on data
});
// Get grid to run filter operation again
this.gridApi.onFilterChanged();

onRowDataChanged will be triggered twice, first when the grid clears everything and second when the data reloaded. onRowDataChanged将被触发两次,第一次是在网格清除所有内容时,第二次是在数据重新加载时。 So, you should put some conditions to avoid any errors.因此,您应该设置一些条件以避免任何错误。

For example, I needed to set the filter from the data that was loaded after the refresh this is what I did:例如,我需要从刷新后加载的数据中设置过滤器,这就是我所做的:

const filtered = this.rowData.filter(x => x.Id === this.Id);
if (filtered.length > 0) {
    // Get a reference to the name filter instance
    const filterInstance = this.gridApi.getFilterInstance('fieldNameHere');
    // Call some methods on Set Filter API that don't apply the filter
    filterInstance.setModel({
        type: 'contains', // type of filter
        filter: filtered[0].name // set filter on data
    });
    // Get grid to run filter operation again
    this.gridApi.onFilterChanged();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM