3gwebtrain
3gwebtrain

Reputation: 15293

Angular - how to make filter by case sensitive

In my app, I am using the search box to filter the persons. the persons name are static(at present), how can i make this filter case sensitive?

because people can enter their name by their wish here. so I am looking this to filter by how the way they entered their name.

here is my config, but not working;

<div class="container">
        <h1>Directive Test 1</h1>
        <input type="search" name="search" ng-model="search" placeholder="Search a person" id="">
        <div class="list" ng-controller="List">
            <ul>
                <li ng-repeat="person in people | filter:search:upperCase"> // not working! {{person.name}}</li>
            </ul>
        </div>
    </div>

Any one help me please?

UPDATE

my js code :

var List = function($scope){
    $scope.people = [
        {name:'Mo',age:20},
        {name:'Si', age:22},
        {name:'Yo', age:33}
    ]
}

Demo is here(buggy)

Upvotes: 1

Views: 4595

Answers (2)

Raghavendra
Raghavendra

Reputation: 5387

uppercase and filter are two different fitlers

change this

person in people | filter:search:upperCase

to this

person in people | filter:search | uppercase

uppercase filter is used when you want to render output html in uppercase

Now, to filter case-sensitive, use custom case sensitive filter

Updated fiddle: http://jsfiddle.net/Tw7kW/

Upvotes: 0

runTarm
runTarm

Reputation: 11547

You could supply a comparator function to the filter like this:

<li ng-repeat="person in people | filter:search:comparatorFunction">

and in a controller:

$scope.comparatorFunction = function (name, search) {
    return (''+name).indexOf(''+search) > -1;
};

Hope this helps.

Upvotes: 4

Related Questions