0

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>
3
  • I think you may have to write your own custom filter to do this. Check out this answer. Commented Mar 17, 2016 at 21:20
  • @Lex Yes, that answer is exactly what I was looking for, even has a jsFiddle. Thanks you. Commented Mar 17, 2016 at 21:22
  • can you show your names json and which fields you have to filters? Commented Mar 18, 2016 at 2:42

1 Answer 1

1

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>
Sign up to request clarification or add additional context in comments.

1 Comment

I get the idea, but In your fiddle you print three first names, not 1,2,5

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.