Search - filter just for fore- and surname


#1

Hey guys,

i have here some data and want to filter the data.
Today i could filter it by a input-field (filter: search).
But this is searching in all values of the data, i just want to search just in the forename and surname.

<ion-item ng-repeat="group in groups | filter:search" class="item-remove-animate item-icon-right">

#2

By default you can only search through all object-properties or just one
Look at the example:
https://docs.angularjs.org/api/ng/filter/filter

If you want a custom search you can create your own filter or define an own filter function:
Look at the last code-example (“Discussion”-part)
http://fdietz.github.io/recipes-with-angular-js/common-user-interface-patterns/filtering-and-sorting-a-list.html


#3

Okay, the second link is good.
But it is still not 100 % what i need.
With:

ng-repeat="friend in friends | filter: { name: query, age: '20' } | orderBy: 'name'

It filters every entry with age=20 and the searchinput. But i need it to search for the value XYZ in “name” as well as in “age”. But with:

ng-repeat="friend in friends | filter: { name: query, age: query } | orderBy: 'name'

It is searching in both fields and showing no result.


#4

You’re looking for an OR search, rather than an AND search. You will need to create a custom filter for that.

app.filter('friends', function () {
    return function (items, text) {
        return items.filter(function (item) {
            var exp = new RegExp(text, 'i');
            return exp.test(item.name) || exp.test(item.age);
        });
    };
});

You can call this in your HTML or you can inject it in your controller as friendsFilter