1

I have a list with the items (name: String, age: Int, checked:Boolean). This list is displayed with an ng-repeat. I want to enable the user to search the list using a searchfield, but the search must not affect the checked-values.

  • the search should only trigger, if the users enters something in the searchfield
  • if the seachfield is filled, the search should filter as usual, but the checked items must not be filtered out.

I tried to create a custom filter. I have problems in understanding the $filter('filter') function with my OR-logic.

Could anyone help me untie the knot in my brain?

app.filter('mySearchFilter', function($filter) {
    return function(data, searchText) {

        if(!searchText || searchText.length === 0) {
            return data;
        }

        console.log(searchText);

        return $filter('filter')(data, searchText);
        //how can I provide additional, OR-concatinated, filter-criteria?
    }
});

Check out my plunk for the minimal-example-code.

http://plnkr.co/edit/3xHLOrSPD3XZy2K9U2Og?p=preview

1 Answer 1

1

As I understand you, you are looking for a union function. Underscore provide such a function. With this you may write your filter in this way:

app.filter('mySearchFilter', function($filter) {
   return function(data, searchText) {

       if(!searchText || searchText.length === 0) {
           return data;
       }

       var allChecked = data.filter(function(d){return d.checked});
       var allMatched = $filter('filter')(data, searchText);
       return _.union(allMatched, allChecked);
   }
});

have a look at: http://documentcloud.github.io/underscore/#union and don't forget to include the script:

<script src="http://documentcloud.github.io/underscore/underscore.js"></script>

PLUNKR

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

1 Comment

Thank you, Michael, for your solution. I'm new to AngularJS and constantly struggling with javascript. I'm working in scala usually and used to its comfort, typesafety, etc... Underscore takes some of the pain away ;-)

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.