简体   繁体   中英

Is it possible to filter for multiple strings in AG-Grid?

I have been using

myTable.api.setQuickFilter(string1);

with great success for a while, but I would like to be able to filter for string1, string2, or string3 at the same time and see all the results at once. Is this possible? I cannot find anything on the ag-grid docs.

Since the custom filter example here focuses on individual column, using External filter seems to be the ideal solution.

You need to implement 2 methods - on gridOptions : isExternalFilterPresent() and doesExternalFilterPass(node) .

  isExternalFilterPresent() {
    return true; // to indicate external filtering is on
  }

  doesExternalFilterPass(node) {

// filter only if searchtext is there
   if (this.searchText) {
        return Object.values(node.data).includes(this.searchText.split(' ')[0]) // 'string1'
         || Object.values(node.data).includes(this.searchText.split(' ')[1]) // 'string2'
         || Object.values(node.data).includes(this.searchText.split(' ')[2]); // 'string3'
    }
   return true;
}

// assuming this is bound to your input field
  externalFilterChanged(newValue) {
    this.searchText = newValue;
    this.gridApi.onFilterChanged(); // letting the grid know filter has changed
  }

From docs -

isExternalFilterPresent
is called exactly once every time the grid senses a filter change. It should return true if external filtering is active or false otherwise. If you return true, doesExternalFilterPass() will be called while filtering, otherwise doesExternalFilterPass() will not be called.

doesExternalFilterPass
is called once for each row node in the grid. If you return false, the node will be excluded from the final set.

If the external filter changes, you need to call api.onFilterChanged() to tell the grid.

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