0

Using Angular UI Grid http://ui-grid.info/docs/#/tutorial/321_singleFilter, I want to have a single button to filter "Company = Mixers" only. The filter works using the input but how do I apply a single filter to a button?

Plunker: http://plnkr.co/edit/R6PhMiBbaeqj9ErjdvY1?p=preview

HTML:

<div ng-controller="MainCtrl">

  <p><button ng-click='filterBtn()'>Filter for "Company = Mixers"</button></p>

  <p><input ng-model='filterValue'/><button ng-click='filter()'>Filter</button></p>

  <div id="grid1" ui-grid="gridOptions" class="grid"></div>

</div>

JS:

var app = angular.module('app', ['ngTouch', 'ui.grid']);

app.controller('MainCtrl', ['$scope', '$http', function ($scope, $http) {
  var today = new Date();
  $scope.gridOptions = {
    enableFiltering: false,
    onRegisterApi: function(gridApi){
      $scope.gridApi = gridApi;
      $scope.gridApi.grid.registerRowsProcessor( $scope.singleFilter, 200 );
    },
    columnDefs: [
      { field: 'name' },
      { field: 'company' }
    ]
  };

  $http.get('https://cdn.rawgit.com/angular-ui/ui-grid.info/gh-pages/data/500_complex.json')
    .success(function(data) {
      $scope.gridOptions.data = data;
  });

 $scope.filterBtn = function(val) {
   // filter for "Company = Mixers" only.
   $scope.gridApi.grid.dataSource.filter({
      field: "company",
      operator: "eq",
      value: "Mixers" //or val
    });
 };

  $scope.filter = function() {
    $scope.gridApi.grid.refresh();
  };

  $scope.singleFilter = function( renderableRows ){
    var matcher = new RegExp($scope.filterValue);
    renderableRows.forEach( function( row ) {
      var match = false;
      [ 'name', 'company', 'email' ].forEach(function( field ){
        if ( row.entity[field].match(matcher) ){
          match = true;
        }
      });
      if ( !match ){
        row.visible = false;
      }
    });
    return renderableRows;
  };
}]);

1 Answer 1

0

What you need is add filter programmatically to grid:

$scope.filterBtn = function(val) {
     $scope.gridApi.grid.columns['Company'].filters[0] = {
                        term: 'mixers'
        };

  };

Here some related question

Sign up to request clarification or add additional context in comments.

3 Comments

ui-grid.info/docs/#/tutorial/321_singleFilter : I am using Angular UI grid instead of Kendo. What is "operator"?
sorry i dindt notice that this question about angular grid) I didnt work with that grid but google says that the way of doing filtering
@simple you try this? stiil have problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.