Friend
Friend

Reputation: 79

How do I change a filter during runtime and then apply it with ui-grid?

http://plnkr.co/edit/NO41Kp8ps03WPNPkSBdR?p=preview

My goal is to give my user pre-built filters that he can use to "run reports" on the grid. One of these reports is getting every row which has a cellValue that exists within an array stored on $scope.

I can tell through console.log() that the filter is taking properly on my ui-grid, however I cannot figure out how to make the grid run the rows through the filter. I do have noTerm = true on the filter and have tried various gridApi calls but to no avail.

Any suggestions?

$scope.toggleFiltering = function() {
    angular.forEach($scope.gridOptions.columnDefs, function(_col){
      if(_col.field == 'company')
      {
          _col.filter = {
            noTerm: true,
            condition: function(searchTerm, cellValue){
              return $scope.valuesForPhone.indexOf(cellValue) > -1;
            }
          }
      }
    })
};

Upvotes: 1

Views: 886

Answers (2)

Elijah Lofgren
Elijah Lofgren

Reputation: 1477

I found that one needs to define an empty column definition in the column definitions in order for programmatic filters to be applied e.g.:

{
    field : 'my_field',
    filter: {term: ''}
}

Upvotes: 0

imbalind
imbalind

Reputation: 1182

If you're simply aiming on toggling the filters I'd suggest you use the way shown in this tutorial where filters are toggled by pushing a button.

The code to toggle filters is:

$scope.toggleFiltering = function(){
  $scope.gridOptions.enableFiltering = !$scope.gridOptions.enableFiltering;
  $scope.gridApi.core.notifyDataChange( uiGridConstants.dataChange.COLUMN );
};

As you can see there is an option to enable/disable filters that can be updated programmatically and then the grid is updated by calling notifyDataChange.

If other than toggling filters you need a custom filter you can get it by instantiating a rowProcessor as you can see in this other tutorial.

The main code is:

$scope.gridApi.grid.registerRowsProcessor( $scope.singleFilter, 200 );

Where $scope.singleFilter could be a generic function accepting an array of rows, iterating through the rows, updating their visible property and returning the whole array, as you can see in the docs.

Upvotes: 0

Related Questions