1

I am using a angularjs filter method on an repeated array of items and trying to filter numbers with a limitTo filter. The result is not getting applied to the repeat in the DOM.

Here is the html

<div ng-controller="demo as d">
      <input type="text" ng-model="d.test" ng-change="d.filterthis(d.test)"><br>
      <div ng-repeat="item in d.items | limitTo:d.limitto  track by $index">
            <span ng-show="!item.show">{{item.myno}}</span> - 
            <span ng-show="!item.show">{{$index}} - {{item.mystr}}</span><br>
      </div>
</div>

App.js filter function withing angularjs

this.filterthis = function(filter){
    that.items.map(function(filter){

            return function(obj){
                obj.show=true;
                if(obj.myno.toString().indexOf(filter) >-1){
                    console.log(obj);
                    obj.show=false;
                }
                return obj;
            }
    }(filter));
};

Items is a array like this

this.items = [{
        show:false,
        myno:10,
        mystr:"test1"
    }];

http://plnkr.co/edit/bTdlTpSeZuPyGpolXLEG

6
  • are you trying to not return items that have show:true applied based upon the values in the input field? Commented Jan 14, 2017 at 2:28
  • That was an inverse. Sorry for that method of usage. You can take show===hide and then have a look at it. The filter is my issue. Please check the plunkr Commented Jan 14, 2017 at 2:57
  • ng-show="!item.show" Commented Jan 14, 2017 at 2:58
  • right, but are you trying to just show the first three items that you're searching for? and not show any of the ones that dont match the search text? Commented Jan 14, 2017 at 3:38
  • yes. if i search for 19 it should show 19 and if I search for 1 it should show first three of all the searches starting with 1 limiting to 3 items Commented Jan 14, 2017 at 3:42

1 Answer 1

1

Having "show" as a key on your items, doesnt remove it from the ng-repeat, and so the limitTo filter will still return the items. Instead you should leverage the filter filter in the repeat like so

<input type="text" ng-model="d.search"><br>
<div ng-repeat="item in d.items | filter:{myno:d.search} | limitTo:d.limitto track by $index">
    <span>{{item.myno}}</span> - 
    <span>{{$index}} - {{item.mystr}}</span><br>
</div>

Note that order is important here, if you limitTo and then filter you are only filtering the limit'ed result. You can change the key upon which you filter by changing {myno:d.search} to a different key, alternatively if you want to search the whole object, simply use d.search.

Updated Plunker

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

1 Comment

Ok cool. I had a multiple filter implementation and didnt use the filter based on object. Now works great. This was my actual need converted. Thanks a ton... haxxxton. ;-) plnkr.co/edit/bQh4MoOfTAx7IsMquswZ

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.