简体   繁体   English

Angular 2 AG-grid,对象键需要一个功能值

[英]Angular 2 AG-grid, object key needs a function value

I have a project using Angular 2 with ag-grid. 我有一个使用ag-grid的Angular 2的项目。

colDef = {column: 'name', filter: myFunction()}

public myFunction () {
    http.get('params_doesn't matter').subscribe((response: any) => {
        console.log('params work, everything is fine');
        http.get({response.id}).subscribe((res:any) => {
            console.log('this works, still fine, the value that is returned is an object and i cannot pass it to "filter"');
            return res;
        })
    })
}

The response i get is an array of objects but the key "filter" from my object needs to take "res". 我得到的响应是一个对象数组,但是来自我对象的键“ filter”需要取“ res”。

Much appreciated. 非常感激。

I see. 我懂了。 Your approach is dangerously slow. 您的方法危险缓慢。 Every time the filter function is called, you are going to make to http calls. 每次调用filter函数时,您都将进行http调用。 Since filter is part of the ag-grid api, you don't have control over when the function gets called. 由于filter是ag-grid api的一部分,因此您无法控制何时调用该函数。 It will be called at least every time the user types into the filter box. 至少每次用户在过滤器框中键入时,都会调用此方法。 The API requires a synchronous call to ensure that filters are applied quickly. API需要同步调用以确保快速应用过滤器。

What you want to do is ensure that the filter is not applied until the user is ready for it, you can do this by adding the following: 您想要做的是确保在用户准备好过滤器之前不应用它,您可以通过添加以下内容来做到这一点:

colDef = {column: 'name', filter: myFunction(), filterParams: 
  { applyButton: true }}

And then inside of your http result handler, you need to re-apply the filter. 然后在您的http结果处理程序中,您需要重新应用过滤器。 You can do it like this: 您可以这样做:

gridOptions.api.onFilterChanged();

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

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