UI Gird过滤器 - 无效的正则表达式

时间:2016-11-07 12:03:27

标签: angularjs angular-ui-grid

我正在使用Angular UI网格来显示字段。

我有一个文本框,用户可以过滤网格中的所有列。但是当用户输入“(”或“*”等文本时会生成错误:正则表达式无效

以下是代码:

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: 'gender', cellFilter: 'mapGender' },
      { field: 'company' },
      { field: 'email' },
      { field: 'phone' },
      { field: 'age' },
      { field: 'mixedDate' }
    ]
  };

  $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.gridOptions.data[0].age = -5;

      data.forEach( function addDates( row, index ){
        row.mixedDate = new Date();
        row.mixedDate.setDate(today.getDate() + ( index % 14 ) );
        row.gender = row.gender==='male' ? '1' : '2';
      });
    });
    
  $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;
  };
}])
.filter('mapGender', function() {
  var genderHash = {
    1: 'male',
    2: 'female'
  };

  return function(input) {
    if (!input){
      return '';
    } else {
      return genderHash[input];
    }
  };
});
.grid {
  width: 500px;
  height: 450px;
}
<!doctype html>
<html ng-app="app">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-touch.js"></script>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.3/angular-animate.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/csv.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/pdfmake.js"></script>
    <script src="http://ui-grid.info/docs/grunt-scripts/vfs_fonts.js"></script>
    <script src="http://ui-grid.info/release/ui-grid.js"></script>
    <link rel="stylesheet" href="http://ui-grid.info/release/ui-grid.css" type="text/css">
    <link rel="stylesheet" href="main.css" type="text/css">
  </head>
  <body>

<div ng-controller="MainCtrl">
  <input ng-model='filterValue'/><button ng-click='filter()'>Filter</button>
  <div id="grid1" ui-grid="gridOptions" class="grid"></div>
</div>


    <script src="app.js"></script>
  </body>
</html>

感谢。

1 个答案:

答案 0 :(得分:1)

您可以转义RegEx输入(有关可以使用的功能,请参阅the answer to this question):

str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");

然后,您可以在$scope.singleFilter函数中使用以下代码:

$scope.singleFilter = function(renderableRows) {
    var filterVal = undefined;
    if ($scope.filterValue != undefined) {
        // Escape RegEx
        filterVal = $scope.filterValue.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
    }
    ...
}

我在问题代码中创建了this Fiddle。您将能够从小提琴中看到,因为不再生成Javascript错误。由于您只是在“名称”,“公司”和“电子邮件”列上进行过滤,因此我看不到任何包含'('的数据,例如,如果您添加列'手机',您应该能够看到在过滤器中输入(9有效。