简体   繁体   中英

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

I have a project using Angular 2 with ag-grid.

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".

Much appreciated.

I see. Your approach is dangerously slow. Every time the filter function is called, you are going to make to http calls. Since filter is part of the ag-grid api, you don't have control over when the function gets called. 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.

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. You can do it like this:

gridOptions.api.onFilterChanged();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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