GridApi.setFilterModel在ag-grid中无法正常工作

huangapple go评论157阅读模式
英文:

GridApi.setFilterModel ag-grid not working properly

问题

以下是已翻译的内容:

我有以下的代码

    onGridReady = (event: GridReadyEvent) => {     
       this.gridApi.setFilterModel(init.filterModel);
    }

`init.filterModel`- 它是一个对象,执行`setFilterModel`后有2个键
在`this.gridApi.getFilterModel()`中只有一个键,而不是两个键。

但在这个变体中

    onGridReady = (event: GridReadyEvent) => {
        this.gridApi.setFilterModel(init.filterModel); // sleep(1ms)     
        new Promise(resolve => setTimeout(resolve, 1)).then(() => {   } 
    }

`gridApi.getFilterModel()`返回有效结果,如何在不使用这个 hack 的情况下解决它?

请注意,代码部分已被保留,未进行翻译。

英文:

I have following code

onGridReady = (event: GridReadyEvent) => {     
   this.gridApi.setFilterModel(init.filterModel);
}

init.filterModel- it is object with 2 keys after setFilterModel executed
in this.gridApi.getFilterModel() there's only one key instead of two key.

but in this variant

onGridReady = (event: GridReadyEvent) => {
    this.gridApi.setFilterModel(init.filterModel); // sleep(1ms)     
    new Promise(resolve => setTimeout(resolve, 1)).then(() => {   } 
}

gridApi.getFilterModel() returns valid result, how it can be solved without this hack?

gridApi.getFilterModel() returns valid result, how it can be solved without this hack?

答案1

得分: 0

修改您的onGridReady事件如下所示:

onGridReady: (params) => {
  var filterModel = params.api.getFilterModel();
  console.log('filterModel first', filterModel);
},

以及您的onFirstDataRendered事件如下所示:

onFirstDataRendered: (params) => {
  var hardcodedFilter = {
    country: {
      type: 'set',
      values: ['Ireland', 'United States'],
    },
    age: { type: 'lessThan', filter: '30' },
    athlete: { type: 'startsWith', filter: 'Mich' },
    date: { type: 'lessThan', dateFrom: '2010-01-01' },
  };
  params.api.setFilterModel(hardcodedFilter);

  filterModel = params.api.getFilterModel();
  console.log('filterModel last', filterModel);
},

结果:

GridApi.setFilterModel在ag-grid中无法正常工作

正如您所见,我们的filterModel对象的第一个状态与其最后一个状态不同,无需等待使用承诺。

Plunker链接:https://plnkr.co/edit/hh4DRg6LD3XwsrZR?preview

*注意:*我在这里使用了纯JavaScript,如果您使用框架,可能需要稍微修改它。

英文:

It's hard to spot your exact problem without more details but let's give it a shot

Modify your onGridReady event like the following

onGridReady: (params) => {
  var filterModel = params.api.getFilterModel();
  console.log('filterModel first', filterModel);
},

and your onFirstDataRendered event like the following

onFirstDataRendered: (params) => {
  var hardcodedFilter = {
    country: {
      type: 'set',
      values: ['Ireland', 'United States'],
    },
    age: { type: 'lessThan', filter: '30' },
    athlete: { type: 'startsWith', filter: 'Mich' },
    date: { type: 'lessThan', dateFrom: '2010-01-01' },
  };
  params.api.setFilterModel(hardcodedFilter);

  filterModel = params.api.getFilterModel();
  console.log('filterModel last', filterModel);
},

Result:

GridApi.setFilterModel在ag-grid中无法正常工作

As you see, the first state of our filterModel object is different from its last state, no need to wait between using a promise.

Plunker: https://plnkr.co/edit/hh4DRg6LD3XwsrZR?preview

NOTE: I have used plain javascript here, you may need to modify it a little bit if you use a framework

huangapple
  • 本文由 发表于 2023年7月20日 15:31:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/76727606.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定