Matthew Brzezinski
Matthew Brzezinski

Reputation: 1794

Filtering out in for loop

I'm trying to create a drop down list and when I am creating it I want to filter by a field. For example:

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

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

    <select ng-model="cost" ng-options="x.cost for x in costs">
    </select>

    </div>

    <script>
    var app = angular.module('myApp', []);
        app.controller('myCtrl', function($scope) {
        $scope.costs = [{'cost': 1, 'difficulty' : 3}, {'cost': 2, 'difficulty' : 2}, {'cost': 3, 'difficulty' : 3}];
    });
    </script>

    <p>This example shows how to fill a dropdown list using the ng-options directive.</p>

    </body>
    </html>

How would I filter by difficulty in this example? Is it possible to do something like

ng-options="x.cost for x in costs where x.difficulty >= 3"

Upvotes: 2

Views: 40

Answers (2)

Sreekanth
Sreekanth

Reputation: 3130

There are multiple ways you could acheive this. - Use a custom filter - Filter the lists at the controller level and bind it to the view accordingly.

Here is what you could do.

var app = angular.module("sampleApp", []);

app.controller("sampleController", ["$scope",
  function($scope) {
    $scope.data = "Hello";
    $scope.difficulty = 3;
    $scope.costs = [{
      'cost': 1,
      'difficulty': 3
    }, {
      'cost': 2,
      'difficulty': 2
    }, {
      'cost': 3,
      'difficulty': 3
    }];

    $scope.filteredCosts = $scope.costs.filter(function(item) {
      return item.difficulty >= 3
    });

  }
]);

app.filter("difficultyFiler", function() {
  return function(costs, difficulty) {
    return costs.filter(function(item) {
      return item.difficulty >= difficulty
    });
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>
<div ng-app="sampleApp">
  <div ng-controller="sampleController">

    <select ng-model="cost">
      <option ng-repeat="item in costs">
        {{item.cost}}
      </option>
    </select>

    <select ng-model="cost">
      <option ng-repeat="item in filteredCosts">
        {{item.cost}}
      </option>
    </select>

    <input type="text" ng-model="difficulty" />

    <select ng-model="cost">
      <option ng-repeat="item in costs | difficultyFiler : difficulty">
        {{item.cost}}
      </option>
    </select>
  </div>

  <p>This example shows how to fill a dropdown list using the ng-options directive.</p>

</div>
</div>

Upvotes: 0

kukkuz
kukkuz

Reputation: 42352

Use a custom filter like this:

  $scope.myFilter = function(x) {
    return x.difficulty >= 3;
  }

and apply it in ng-options like this:

 <select ng-model="cost" ng-options="x.cost for x in costs | filter : myFilter">
 </select>

Demo below:

var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
  $scope.costs = [{
    'cost': 1,
    'difficulty': 3
  }, {
    'cost': 2,
    'difficulty': 2
  }, {
    'cost': 3,
    'difficulty': 3
  }];

  $scope.myFilter = function(x) {
    return x.difficulty >= 3;
  }
});
<!DOCTYPE html>
<html>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>

<body>

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

    <select ng-model="cost" ng-options="x.cost for x in costs | filter : myFilter">
    </select>

  </div>

  <p>This example shows how to fill a dropdown list using the ng-options directive.</p>

</body>

</html>

Upvotes: 3

Related Questions