0

i've checqued this : AngularJS filter based on array of strings?

But i've still got difficulties to know how to do :

My data model is this, they are footballers :

$scope.footballers = [
        {'identifiant':1,'prenom':'Jean','nom':'Valjean','categorie':1,'ville':'Détroit','age':12,'date_embauche':'','salaire':25,'photo':'1.jpg','vitesse':55,'agilite':3,'deduction':25,'choisi':false},
        {'identifiant':2,'prenom':'Aziz','nom':'Jojo','categorie':2,'ville':'Paris','age':14,'date_embauche':'','salaire':25,'vitesse':57,'agilite':31,'deduction':25,'choisi':false},
        {'identifiant':3,'prenom':'Thierry','nom':'Goubert','categorie':1,'ville':'Paris','age':17,'date_embauche':'','salaire':28,'photo':'2.jpg','vitesse':45,'agilite':3,'deduction':2,'choisi':false},
        {'identifiant':4,'prenom':'Roland','nom':'Grondin','categorie':2,'ville':'Paris','age':14,'date_embauche':'','salaire':25,'vitesse':5,'agilite':34,'deduction':2,'choisi':false},
        {'identifiant':5,'prenom':'Gogok','nom':'Rodolphe','categorie':1,'ville':'Paris','age':17,'date_embauche':'','salaire':28,'photo':'3.jpg','vitesse':68,'agilite':75,'deduction':2,'choisi':false},
        {'identifiant':6,'prenom':'Thierry','nom':'Chalamerto','categorie':1,'ville':'Paris','age':17,'date_embauche':'','salaire':28,'vitesse':55,'agilite':57,'deduction':75,'choisi':false},
        {'identifiant':7,'prenom':'Gawivk','nom':'Gonzogues','categorie':2,'ville':'Paris','age':14,'date_embauche':'','salaire':25,'vitesse':10,'agilite':44,'deduction':2,'choisi':false},
        {'identifiant':8,'prenom':'Thomas','nom':'Choubal','categorie':1,'ville':'Paris','age':12,'date_embauche':'','salaire':28,'vitesse':5,'agilite':3,'deduction':2,'choisi':false}
    ];

Now, I would like to display only the footballer who has the identifiant 2,3 and 8 for example.

Let's say I 've got this array :

var iwanttofilter = [2,3,8];

How could i do to filter with angularJs, firstly, in my ng-repeat, and secondly directly into my controller ?

Thank you.

2
  • You'll want to use an AngularJS filter: docs.angularjs.org/api/ng/filter/filter Commented Apr 11, 2017 at 16:35
  • there is no example of filtering with an arrays of values Commented Apr 11, 2017 at 16:40

3 Answers 3

2

In pure angular way

var filteredList = $filter('filter')($scope.footballers, function (i) {
  return (i.identifiant === 2 || i.identifiant === 3 || i.identifiant === 8);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, let's try this!
This is Exactly what i need, except that it doesn't pick values from the iwanttofilter array. how could i add a angular.foreach or a for loop inside the function to iterate over the iwanttofilter array ?
1

you can create a custom filter like this

 .filter('cust',function(){
      var iwanttofilter = [2,3,8];
      return function(item){ 
         return item.filter(o=>iwanttofilter.find(k=> o.identifiant == k))        
      }
 })

in here array will filter according the iwanttofilter array and return the result

Demo

angular.module("app",[])
.controller("ctrl",function($scope){

$scope.footballers = [
        {'identifiant':1,'prenom':'Jean','nom':'Valjean','categorie':1,'ville':'Détroit','age':12,'date_embauche':'','salaire':25,'photo':'1.jpg','vitesse':55,'agilite':3,'deduction':25,'choisi':false},
        {'identifiant':2,'prenom':'Aziz','nom':'Jojo','categorie':2,'ville':'Paris','age':14,'date_embauche':'','salaire':25,'vitesse':57,'agilite':31,'deduction':25,'choisi':false},
        {'identifiant':3,'prenom':'Thierry','nom':'Goubert','categorie':1,'ville':'Paris','age':17,'date_embauche':'','salaire':28,'photo':'2.jpg','vitesse':45,'agilite':3,'deduction':2,'choisi':false},
        {'identifiant':4,'prenom':'Roland','nom':'Grondin','categorie':2,'ville':'Paris','age':14,'date_embauche':'','salaire':25,'vitesse':5,'agilite':34,'deduction':2,'choisi':false},
        {'identifiant':5,'prenom':'Gogok','nom':'Rodolphe','categorie':1,'ville':'Paris','age':17,'date_embauche':'','salaire':28,'photo':'3.jpg','vitesse':68,'agilite':75,'deduction':2,'choisi':false},
        {'identifiant':6,'prenom':'Thierry','nom':'Chalamerto','categorie':1,'ville':'Paris','age':17,'date_embauche':'','salaire':28,'vitesse':55,'agilite':57,'deduction':75,'choisi':false},
        {'identifiant':7,'prenom':'Gawivk','nom':'Gonzogues','categorie':2,'ville':'Paris','age':14,'date_embauche':'','salaire':25,'vitesse':10,'agilite':44,'deduction':2,'choisi':false},
        {'identifiant':8,'prenom':'Thomas','nom':'Choubal','categorie':1,'ville':'Paris','age':12,'date_embauche':'','salaire':28,'vitesse':5,'agilite':3,'deduction':2,'choisi':false}
    ];
    
    var iwanttofilter = [2,3,8];
    $scope.cust = function(){
      return function(item){
        return iwanttofilter.find(k=> item.identifiant == k)
      }
    }
    
})
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
 <div ng-repeat="item in footballers | filter:cust() track by $index ">{{item.identifiant}} </div>
</div>

1 Comment

But the iwanttofilter array is inside the controller, not inside the filter, i dont know what to do. the iwanttofilter array values might change anytime.
0

You pass the iwanttofilter into the filter, and filter your list based on each item.

MyApp.filter("fewerFootballers", [
    function() {
        return function(footballers, iwanttofilter) {
            return arrayIntersection(footballers, iwanttofilter);

            function arrayIntersection(a, b) {
                return a.filter(function(x) {
                    return b.indexOf(x.identifiant) != -1;
                });
            }
        }
    }]);

In your html you use the filter.

{{ $scope.footballers | fewerFootballers: $scope.iwanttofilter }}

3 Comments

Ok let's try this
Did you have any luck?
Yes it is working thank you a lot, but i've had to use the filter directly inside a function ... See this stackoverflow.com/questions/43353644/…

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.