Angularjs中如何使用filterFilter函數過濾_AngularJS

來源:互聯網
上載者:User

AngularJS的filter,中文名“過濾器”是用來過濾變數的值,或者格式化輸出,得到自己所期望的結果或格式的東東。AngularJS中有一個filterFilter函數用來對集合過濾,非常方便。

原始碼大致如下:

function filterFilter(){return function(aray, expression comparator){if(!isArray(array)) return array;var comparatorType = typeof(comparator),predicates = [],evaluatedObjects = [];predicates.check = function(value){for(var j = 0; j < predicates.length; jii){if(!predicates[j](value){return false;})}return true;} if(comparatorType != 'function'{if(comparatorType === 'boolean' && comparator){comparator = function(obj, text){return angular.equals(obj, text);}} else {comparator = function(obj, text){...}}}) }}

controller部分如下:

angular.module('app').controller('MainCtrl', ['$scope',function($scope) {$scope.users = $scope.users = [{name: '', email: '', joined: 2015-1-1}];$scope.filter = {fuzzy: '',name: ''};...}]);

搜尋所有任意欄位

<input type="text" ng-model="filter.any" ><tr ng-repeat="user in users | filter: filter.any"><td>{{user.name}}</td><td>{{user.email}}</td><td>{{user.joined | date}}</td></tr>

搜尋某個欄位

<input type="text" ng-model="filter.name"><tr ng-repeat="user in users | filter: filter.any| filter: {name: filter.name}"><td>{{user.name}}</td><td>{{user.email}}</td><td>{{user.joined | date}}</td></tr>

如果想name欄位完全符合:

<tr ng-repeat="user in users | filter: filter.any| filter: {name: filter.name}:true"><td>{{user.name}}</td><td>{{user.email}}</td><td>{{user.joined | date}}</td></tr>

搜尋時間段

contrlller部分修改為:

angular.module('app').controller('MainCtrl', ['$scope',function($scope) {$scope.users = $scope.users = [{name: '', email: '', joined: 2015-1-1}];$scope.filter = {fuzzy: '',name: ''};$scope.minDate = new Date('January 1, 2000');$scope.maxDate = new Date();$scope.min = function(actual, expected) {return actual >= expected;};$scope.max = function(actual, expected) {return actual <= expected;};}]);

頁面部分為:

<input type="text" ng-model="fromDate" data-min-date="{{minDate}}"><input type="text" ng-model="untilDate" data-max-date="{{maxDate}}"><tr ng-repeat="user in users | filter: filter.any| filter: {name: filter.name}| filter: {joined: untilDate}:max| filter: {joined: beforeDate}:min"><td>{{user.name}}</td><td>{{user.email}}</td><td>{{user.joined | date}}</td></tr>

以上所述是小編給大家分享的Angularjs中如何使用filterFilter函數過濾的相關知識,希望對大家有所協助。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.