简体   繁体   中英

How to pre-select AG-Grid filter dropdown checkbox values

Is there a way to pre-select a value or values for the filter checkbox?

I am currently saving the last filter so if a user filters, leaves the page, and then comes back the grid is still filtered. The issue I'm having is the filter drop down checkboxes does not reflect the filtered rows.

在此处输入图片说明

Here is the code I am using to set the saved filter:

if ($scope.onFilterChanged) {
 this.gridOptions.onFilterModified = function () {
        $scope.onFilterChanged({filter: ctrl.gridOptions.api.getFilterModel()});
  }
 }

if ($scope.currentFilter && $scope.onFilterChanged) {
    this.gridOptions.api.setFilterModel($scope.currentFilter);
} else {
    this.gridOptions.api.setFilterModel(null);
}

setFilterModel works great if I'm not leaving the page and coming back. But I'm not sure why it updates the rows and not the drop down options on page load..Is there a way to get the filtered rows and the check boxes to match on page load?

Yes its possible via filter API

You need to get filter instance first

let filterInstance = this.gridOptions.api.getFilterInstance('columnNameHere');

Then you can decide what should be in the filter

filterInstance.selectNothing();
filterInstance.selectValue("value one");
filterInstance.selectValue("value two");
...or...
let model = ["value one", "value two"];
filterInstance.setModel(model);

And on the last step - just inform the grid about changes

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