stacklf
stacklf

Reputation: 41

ag-grid setModel equivalent of selectValue

Using ag-grid 23.2.1, it looks like our current approach to filtering is on it's way out and listed as deprecated. Reading over the setModel documentation, I don't readily see a way to replace some of our selectValue, selectNothing, etc. usage.

Example 1: we clear the selection and then pick a couple of values on some condition else we select all

let filterInstance = this.gridOptions.api.getFilterInstance('make');
this.cacheFilterModel = this.gridOptions.api.getFilterModel();
if (condition) {
    filterInstance.selectNothing();
    filterInstance.selectValue('thing1');
    filterInstance.selectValue('thing2');
}
else {
    filterInstance.selectEverything();
}
filterInstance.applyModel();
this.gridOptions.api.onFilterChanged();

Example 2: we have some filter values in a column toggle-able via checkboxes, which call a method like the below. if one of the values is checked it gets added to the existing filter, if unchecked the value is removed. There could be values already filtered and I don't really see a way to contextually select/unselect values using setModel.

filterExample (make, add) {
    let filterInstance = this.gridOptions.api.getFilterInstance('make');
    if (add) {
        filterInstance.selectValue(make);
    }
    else {
        filterInstance.unselectValue(make);
    }
    filterInstance.applyModel();
    this.gridOptions.api.onFilterChanged();
}

Are there setModel equivalents for these?

Upvotes: 4

Views: 2923

Answers (1)

Michael Quintero
Michael Quintero

Reputation: 56

I had a similar issue with setColumnFilter where I had to pass a few values. You can use filterInstance.setModel to set these values by passing an array like so:

    filterInstance.setModel({ values: ['value1', 'value2'] })
    filterInstance.applyModel();
    gridOptions.api.onFilterChanged()

here is a link to how to do it from the docs. I found this hard to find on google so providing it here.

https://www.ag-grid.com/javascript-grid-filter-set-api/

Upvotes: 2

Related Questions