Gary
Gary

Reputation: 2339

Filter Number in an angular object using limitto

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

Upvotes: 1

Views: 392

Answers (1)

haxxxton
haxxxton

Reputation: 6442

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

Upvotes: 1

Related Questions