2016-11-07 6 views
1

フィールドを表示するために角度UIグリッドを使用しています。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

:無効な正規表現の下

はコードがあります。 RegEx入力をエスケープすることができます(使用できる機能については、the answer to this questionを参照してください)。

str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&"); 
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が機能することを確認してください。

関連する問題