Juan Carlos Oropeza
Juan Carlos Oropeza

Reputation: 48197

How filter using one array

Im trying some angularJS on this example

http://www.w3schools.com/angular/tryit.asp?filename=try_ng_filters_filter

<ul>
  <li ng-repeat="x in names | filter : 'i'">
    {{ x }}
  </li>
</ul>

But how you filter for multiple choices? In my case I have a model with all the cars. And the user select a list of ID int[] SelectedCars from a tree list.

So I want something like

<ul>
  <li ng-repeat="x in names | filter : SelectedCars[]">
    {{ x }}
  </li>
</ul>

Upvotes: 0

Views: 71

Answers (1)

Jeff Diederiks
Jeff Diederiks

Reputation: 1380

JSFiddle

That should work now.

<!DOCTYPE html>
<html>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
<body>

<div ng-app="myApp" ng-controller="namesCtrl">

<ul>
    <li ng-repeat="x in names | filter : 'i'">
        {{ x }}
    </li>
</ul>

</div>

<script>
angular.module('myApp', []).controller('namesCtrl', function($scope) {
        $scope.names = [
                'Jani',
                'Carl',
                'Margareth',
                'Hege',
                'Joe',
                'Gustav',
                'Birgit',
                'Mary',
                'Kai'
        ];
        $scope.selectedCars = [1, 2, 5];
        $scope.carsToDisplay = [];
        for (var i = 0; i < $scope.selectedCars.length; i++) {
                $scope.carsToDisplay.push($scope.names[$scope.selectedCars[i]]);
        }
        var p = document.getElementById('p');
        p.innerHTML = $scope.carsToDisplay;
});
</script>

<p id="p">This example displays only the names containing the letter "i".</p>

</body>
</html>

Upvotes: 1

Related Questions