Search code examples
javascripthtmlangularjsfilterangularjs-ng-repeat

How to implement a loop in a multi parameters filter in a ng-repeat?


I would like to make a loop in a filter parameters

Hey,
I'm doing a multi filters table. Each column as an input to add a string to filter on.
For each column, I call the parameter name with the filter input in the filter function :paramN: filter[n]
Is there a way to make a loop in it so I won't have to write every parameter one by one ?
I have everything I need in an array but I don't know how (if it's possible) to call it in the filter.
Should I use a custom filter ? If so, what would be the syntax, and, would it slow the process ?

Here the code of my filter :

<tr ng-repeat="folder in listFolders|
    filter:{param1:filter[1],
            param2:filter[2],
            param3:filter[3],
            ...
            paramN: filter[n]
            } as listVisible
">

Here is my array :

$scope.parametres=[
  {int:1, libelle:'name1', variable:'param1'},
  {int:2, libelle:'name2', variable:'param2'},
  {int:3, libelle:'name3', variable:'param3'},
  ...
  {int:n, libelle:'nameN', variable:'paramN'}
]

Thanks !


Solution

  • In case someone someday face the same problem as me :

    I made this function in my controller (it's called every time a change is made in the filters inputs via a ng-change)

    $scope.filteringFunction = function(){ 
        var filtered=angular.copy($scope.listFolders);
        for(var i=0; i<$scope.parametres.length; i++){
            if($scope.filter[$scope.parametres[i].int]!==''){
                var filter =$rootScope.toNoCase($scope.filter[$scope.parametres[i].int]);
                var param = $scope.parametres[i].variable;
                filtered = filtered.filter(function (item) {
                    return $rootScope.toNoCase(item[param]).includes(filter);
                });
            }
        }
        $scope.listFoldersFiltered=filtered;
    };
    

    and I changed a bit the ng-repeat (basically remove the filter and change the list listFolders to listFoldersFiltered)

    <tr ng-repeat="folder in listFoldersFiltered as listVisible">
    

    If someone know an other method to do it, I'm all ears !