Random Name
Random Name

Reputation: 387

How to activate the filtering function in the checkbox angular?

i have block with filters:

 <form action="">
                    <input type="checkbox" id="inp1">
                    <label for="inp1">All items</label>
                    <input type="checkbox"  id="inp2">
                    <label for="inp2">New today</label>
                    <input type="checkbox" id="inp3">
                    <label for="inp3">New this week</label>
                    <input type="checkbox" id="inp4">
                    <label for="inp4">Free delivery</label>
                </form>

Filters for "new today" and "New this week":

  $scope.newThisWeek = function(products){
                     $scope.time = (Date.now() - Date.parse(products.add_update_time));

                     if ($scope.time <= 604800000) {
                         return $scope.products;
                     } else {

                         return null;
                     }

                };

                $scope.newToday = function(products){
                       $scope.time = (Date.now() - Date.parse(products.add_update_time));

                       if ($scope.time <= 86400000) {
                           return $scope.products;
                       } else {

                           return null;
                       }
                };

I use filters as there:

<div class="catalog-item" ng-repeat="product in products |filter:newThisWeek|filter:newToday">
 <a href="{{product.external_url}}" class="item-title">{{ product.name }}</a>
</div>

How to activate the filtering function in the checkbox angular?

Upvotes: 0

Views: 67

Answers (1)

shershen
shershen

Reputation: 9993

You need to fist bind the inputs in your html to the $scope, using ng-model attribute:

..
 <input type="checkbox" id="inp2" ng-model="newTodayActivated">
 <label for="inp2">New today</label>
..other inputs

By this you create a $scope.newTodayActivated property that will be updated (toggle true/false) on change of the checkbox state . Then you can use this value in the filter expression functions:

$scope.newToday = function(products){      
  //use $scope.newTodayActivated - as Boolean here in your logic
  if($scope.newTodayActivated) {
   //do stuff
  } else {
  }
}

Upvotes: 1

Related Questions