0

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;
    });

2 Answers 2

1

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); 
};
Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

I want to exclude search for Location field

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.