I've recently started experimenting with AngularJS to create checkbox filters for my wine store. Here is an example of an item in my store:
{
"id": 17,
"name": "Ermelinda Freitas Reserva",
"tag_ids": [40, 12, 56, 6, 60],
"tag_names": ["Roasted", "Robust", "Blackberry", "Mouth-filling", "Animal"],
"price": 29,
"weight": "0",
"image": "1467208469_wijn1.jpg",
"stock": 57,
"year": 1998,
"special": 0,
"color_id": 1,
"color": "Red",
"region_id": 25,
"region": "Alentejo",
"country_id": 6,
"country": "Portugal",
"grape_ids": [34, 35, 20],
"grape_names": ["Castelão", "Touriga Naçional", "Cabernet Sauvignon"]
}
While I've successfully created filters for countries and other non-array properties, I am now facing a challenge with filtering by grapes. Here's how I started collecting unique grape values:
angular.forEach($scope.wines, function(wine){
angular.forEach(wine.grape_names, function(grape){
for(i = 0; i< wine.grape_names.length; i++){
if($scope.grapes.indexOf(wine.grape_names[i]) === -1) $scope.grapes.push(wine.grape_names[i]);
};
});
})
Now, the task is to check if an item has any grapes that match the selected filter. Here is where I need assistance:
$scope.filter.grape = {};
$scope.filterByGrape = function(wine){
angular.forEach(wine.grape_names, function(grape){
return $scope.filter.grape[grape] || noFilter($scope.filter.grape);
})
};
The filters :
<div class="col-lg-3 col-md-4 col-sm-6 " data-id="{{wine.id}}" ng-init="getQuantity(wine.id)" data-ng-repeat="wine in filteredWines =(wines | orderBy:defaultOrder | filter:filterByColor | filter:filterByCountry | filter:filterByGrape | priceRangeFilter:priceRangeSlider | yearRangeFilter:yearRangeSlider | filter:search) | start: (currentPage - 1) * itemsPerPage | limitTo:itemsPerPage">
https://i.sstatic.net/51gQX.png
All feedback and suggestions are appreciated!