【问题标题】:Why is ag-Grid clearing filter after being set in onGridReady为什么在 onGridReady 中设置后 ag-Grid 清除过滤器
【发布时间】:2020-06-11 20:07:21
【问题描述】:

我遇到了奇怪的行为,如果我在 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 事件,但这并不理想,因为用户将在其之前看到所有数据 被过滤掉。

【问题讨论】:

    标签: ag-grid ag-grid-angular


    【解决方案1】:

    尝试以下方法,而不是使用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);
    

    【讨论】:

    • 不幸的是,从过滤器实例中使用 setModel 似乎有同样的问题
    • 你能不能重复一下这个问题?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2018-09-29
      • 2020-07-15
      • 2019-08-30
      • 2017-11-19
      • 2017-12-08
      • 2020-12-01
      • 2021-09-05
      • 2020-04-08
      • 2017-01-10
      相关资源
      最近更新 更多