How to filter _angularjs using Filterfilter function in Angularjs

Source: Internet
Author: User

ANGULARJS filter, the Chinese name "filter" is used to filter the value of variables, or format the output, get their desired results or format dongdong. Angularjs has a filterfilter function to filter the collection, which is very convenient.

The source code is roughly as follows:

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) {
...
}
}
}) 
}
}

The controller section is as follows:

Angular
. Module (' app ')
. Controller (' Mainctrl ', [' $scope ',
function ($scope) {
$scope. Users = $ Scope.users = [
{name: ', email: ', joined:2015-1-1}
];
$scope. Filter = {
fuzzy: ',
name: '
};
...
}]);

Search all any fields

<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>

Search for a field

<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>

If you want the name field to match exactly:

<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>

Search time period

The Contrlller section is modified to:

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, n ');
$scope. maxdate = new Date ();
$scope. Min = function (actual, expected) {return
actual >= expected;
};
$scope. max = function (actual, expected) {return
actual <= expected;
}
]);

The page section is:

<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>

The above is a small set to share the ANGULARJS in how to use Filterfilter function filter related knowledge, hope to help you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.