1

I would like to adjust my angular text search so that it isn't word-order sensitive. I have to search a big json object but basically if there {"name":"John Doe"} in the file, I want the searchText 'Doe John' to return positive. Any suggestions? Thanks

<input type="text" ng-model="searchText">
<ul>
    <li ng-repeat="item in items | filter:searchText">
       {{ item }}
    </li>
</ul>

EDIT:

i got it working thanks! http://jsfiddle.net/Lp02huqm/

2

1 Answer 1

3

I actually created a similar filter for something.

app.filter("searchText", function () {
    return function (items, filterBy) {
        var filterBy = filterBy.split(/\s+/);

        if (!filterBy.length) {
            return items;
        }
        return items.filter(function (item) {
            return filterBy.every(function (word) {
                return ~item.indexOf(word);
            });
        });
    };
});

Note that I use every, so you would need "Doe John" for "John Doe." If you wanted "Doe" or "John" to also match, you could use .some.

Sign up to request clarification or add additional context in comments.

3 Comments

This is cool! Would you mind include an example of how to use this in html? It would be a little strange as ng-repeat="item in items | searchText:searchText".
@runTarm just | searchText
thanks! the Array.every function was very usefull! here's a working example jsfiddle.net/Lp02huqm

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.