Reputation: 1271
I'm trying to do a search field filter using angular filter for specific fields and which should be an OR
condition
This is demo implementation http://jsfiddle.net/tomalex0/ck19wvdx/2/.
I'm trying to search only on Name
and Gender
but not Location
.
Whatever combination i tried it works more like AND
condition. Is there an inbuilt way to do OR condition
in filter
?
<div ng-app="myApp" ng-controller="myController">
<input type="text" ng-model="search">
<ul>
<li ng-repeat="user in users | filter: search">{{user.Name}} | {{user.Location}}
</li>
</ul>
</div>
var data = [
{
Name: "John Smith",
Gender: "M",
Location: "Wales"
},
{
Name: "Sally Smith",
Gender: "F",
Location: "USA"
},
{
Name: "Curtis Timson",
Gender: "M",
Location: "England"
},
{
Name: "Sam Wallace",
Gender: "M",
Location: "England"
},
{
Name: "Caroline James",
Gender: "F",
Location: "Scotland"
}
];
var myApp = angular.module("myApp", []);
myApp.controller("myController", function($scope){
$scope.users = data;
});
Upvotes: 0
Views: 1818
Reputation: 31779
You may need to use a filter function instead of directly passing the search string to the filter. The relevant code is below. Here is ur fiddle modified.
<li ng-repeat="user in users | filter: filter">
$scope.filter = function(item) {
var regex = new RegExp($scope.search, 'gi');
return item.Name.match(regex) || item.Gender.match(regex);
};
Upvotes: 1
Reputation: 15104
Your demonstration works like an OR
operator to me.
For example if i write sa
in the input, the result is :
Sally Smith | USA
Sam Wallace | England
With a AND
, Sam Wallace | England
will be hidden because England
contains no sa
.
Upvotes: 0