为什么在onGridReady中设置后ag-Grid清除过滤器

Why is ag-Grid clearing filter after being set in onGridReady

我遇到了奇怪的行为,如果我在 onGridReady 事件中设置了一个过滤器模型,它会在事后被删除。我一直在记录 filterChanged 事件,我看到它在我设置过滤器时被调用,但它永远不会被再次调用,但过滤器被清除而没有过滤器更改事件。当我使用社区时,我没有遇到过这种情况,但是当我升级到企业版并开始使用 setFilter 时,这种情况就开始发生了。有什么想法吗?

    onGridReady(params: ICellRendererParams): void {
        this.gridApi = params.api
        this.gridApi.sizeColumnsToFit()
        this.resetDefaults()
        window.addEventListener('resize', function() {
            setTimeout(function() {
                params.api.sizeColumnsToFit()
            })
        })
    }

    resetDefaults(): void {
        this.gridApi.setFilterModel({
            ColorStatus: {
                filterType: 'set',
                values: [ColorStatus.red.toString(), ColorStatus.yellow.toString()]
            }
        })
        this.gridApi.onFilterChanged(); //I've tried with and without this line
    }


奇怪的是,当我在 onGridReady 中设置排序时,排序模型不受影响,只有过滤器模型被清除。 与此同时,我已经将 resetDefaults() 移动到 onFirstDataRendered 事件,但这并不理想,因为用户会在它之前看到所有数据 被过滤。

尝试使用以下方法,而不是使用 gridApi.setFilterModel 设置 filterModel

  1. 使用colId获取Column的过滤器实例(在定义ColDef时设置)
  2. setModel 使用过滤器实例。
// define filterModel
const filterModel = {
   ColorStatus: {
      filterType: 'set',
      values: [ColorStatus.red.toString(), ColorStatus.yellow.toString()]
   }
};

const filterInstance = this.gridApi.getFilterInstance(colId); // <- ColorStatus column columnId
// you need to set it inside ColDef for this column

filterInstance.setModel(filterModel);

我的同事发现将 newRowsAction: 'keep' 添加到相关列的 filterParams 可以解决问题