AngularJs How to filter ngRepeat with another array elements
Asked Answered
C

4

10

Is there any option to filter from $scope.items where the ID exist in the array $scope.myitems ?

ng-repeat="item in items | filter:{item.id == myitems}

Demo: http://codepen.io/anon/pen/rOeGYB

angular.module('myapp', [])
  .controller("mycontroller", function($scope) {
    $scope.items = [
      {
        "id": 1,
        "name": "Melodie"
      }, {
        "id": 2,
        "name": "Chastity"
      }, {
        "id": 3,
        "name": "Jescie"
      }, {
        "id": 4,
        "name": "Hamish"
      }, {
        "id": 5,
        "name": "Germaine"
      }, {
        "id": 6,
        "name": "Josephine"
      }, {
        "id": 7,
        "name": "Gail"
      }, {
        "id": 8,
        "name": "Thane"
      }, {
        "id": 9,
        "name": "Adrienne"
      }, {
        "id": 10,
        "name": "Geoffrey"
      }, {
        "id": 11,
        "name": "Yeo"
      }, {
        "id": 12,
        "name": "Merrill"
      }, {
        "id": 13,
        "name": "Hoyt"
      }, {
        "id": 14,
        "name": "Anjolie"
      }, {
        "id": 15,
        "name": "Maxine"
      }, {
        "id": 16,
        "name": "Vance"
      }, {
        "id": 17,
        "name": "Ashely"
      }, {
        "id": 18,
        "name": "Dominic"
      }, {
        "id": 19,
        "name": "Cora"
      }, {
        "id": 20,
        "name": "Bo"
      }
    ];
    $scope.myitems = ['0', '3', '6', '10', '19']
  });
<link href="//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.14/angular.min.js"></link>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myapp" ng-controller="mycontroller" class="container">
  <h4>My items</h4>
  <ul class="list-group">
    <li class="list-group-item" ng-repeat="item in items | filter:{}">{{item.name}}</li>
  </ul>
  <h4>Total items</h4>
  <ul class="list-group">
    <li class="list-group-item" ng-repeat="item in items">{{item.name}}</li>
  </ul>
</div>
Christen answered 17/9, 2015 at 18:59 Comment(2)
possible duplicate of Is it possible to filter angular.js by containment in another array?Olmsted
It might be easier to create a custom filter in another file, and then call it. That way you can do as much complex logic as you want with one small filter declaration.Rheinland
H
10

Your problem is that you are trying to get a match on a sub-property of the object(s) you are iterating over.

From the docs:

Note that a named property will match properties on the same level only, while the special $ property will match properties on the same level or deeper. E.g. an array item like {name: {first: 'John', last: 'Doe'}} will not be matched by {name: 'John'}, but will be matched by {$: 'John'}.

I would recommend you will make your custom filter. I have changed your code by implementing custom filter, working copy of your requirement.

<li class="list-group-item" ng-repeat='item in items | customArray:myitems:"id"'>{{item.name}}</li>

See complete plunker here https://codepen.io/anon/pen/PPNJLB

Haematosis answered 17/9, 2015 at 19:11 Comment(0)
T
7

Use this excellent filter from this answer :

https://mcmap.net/q/593572/-is-it-possible-to-filter-angular-js-by-containment-in-another-array

.filter('inArray', function($filter){
    return function(list, arrayFilter, element){
        if(arrayFilter){
            return $filter("filter")(list, function(listItem){
                return arrayFilter.indexOf(listItem[element]) != -1;
            });
        }
    };
});
Theatricals answered 17/9, 2015 at 19:20 Comment(0)
I
0
angular.forEach($scope.items, function (v, k) { 
   console.log($scope.myitems[v.id - 1]);
   $scope.myitems[v.id- 1].name = v.name;
}, $scope.myitems);

Just for some hint. Hope helps

Ionian answered 17/9, 2015 at 19:41 Comment(0)
Y
0

Tha above example given by Kashif Mustafa perfectly works. The only change we had to do is, parse the ids into integers from string.

cshtml:

    <div ng-repeat="e in AllEntities | customArray:SelectedEntities:'id'">{{ e.label }}</div>

controller:

     var eIds = detail.entityIds;
     var eArray = eIds.split(",");

     if (eArray.length > 0) {
         $scope.showEntities = true;
         $scope.showNone = false;
         for (var i = 0; i < eArray.length; i++) {
            $scope.SelectedEntities.push(parseInt(eArray[i]));
           }
     }

filter:

(your controller).filter('customArray', function ($filter) {
    return function (list, arrayFilter, element) {
        if (arrayFilter) {
            return $filter("filter")(list, function (listItem) {
                return arrayFilter.indexOf(listItem[element]) != -1;
            });
        }
    };
});
Yumuk answered 27/10, 2017 at 5:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.