Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
52
rated 0 times [  53] [ 1]  / answers: 1 / hits: 21797  / 12 Years ago, tue, december 11, 2012, 12:00:00

I have the user object defined as below.



$scope.user = [{id: 1, friends:
[
{name: 'John', age: 21, sex: 'M'},
{name: 'Brad', age: 32, sex: 'M'}
]
}]


I have the following code:



<input type=text ng-model=searchText>
<div ng-repeat=friend in user.friends | filter:searchText>
{{friend.name}} {{friend.age}}
</div>


Here whenever I search, I get results for name, age as well as sex. But I want to search only for name and age and I don't want sex to be searchable. Can anyone help me with how I can achieve this?


More From » angularjs

 Answers
8

I'm not sure if this is what you are after. If you want to have one input field to matched multiple properties you need a filter function to be passed to filter.



$scope.user = {id: 1, friends: [{name: 'John', age: 21, sex: 'M'}, {name: 'Brad', age: 32, sex: 'M'}, {name: 'May', age: 64, sex: 'F'}]};

$scope.searchFilter = function (obj) {
var re = new RegExp($scope.searchText, 'i');
return !$scope.searchText || re.test(obj.name) || re.test(obj.age.toString());
};


Here's a fiddle example
http://jsfiddle.net/fredrik/26fZb/1/


[#81493] Sunday, December 9, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
antonb

Total Points: 424
Total Questions: 104
Total Answers: 101

Location: Serbia
Member since Tue, Jul 26, 2022
2 Years ago
;