How to filter displayed data in ui-grid by using angularjs?

十年热恋 提交于 2019-12-04 12:39:13

May be this is what you are looking for.

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: 'address' }          
    ]
  };

  $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', 'address'].forEach(function( field ){
        if ( row.entity[field].match(matcher) ){
          match = true;
        }
      });
      if ( !match ){
        row.visible = false;
      }
    });
    return renderableRows;
  };
}])

});

The code is copied from this link. The

onRegisterApi: function(gridApi){
      $scope.gridApi = gridApi;
      $scope.gridApi.grid.registerRowsProcessor( $scope.singleFilter, 200 );
    }

and "$scope.singleFilter" is the main thing you need to look.

Instead of using the default filter, define a custom filter which filters based on the name and address only. Here is the basic logic.

Filter:

app.filter('customfilter', function () {
    //Your logic to filter based on name and address
});

Controller:
Inject filter in the controller and use it.

$scope.refreshData = function() {
    $scope.gridOpts.data = $filter('customfilter')(arguments for filter);
};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!