我最近开始摆弄AngularJS。我正在为我的葡萄酒商店制作复选框过滤器。 项目如下所示:
{
"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"]
}
我设法为国家/地区的其他非数组属性制作过滤器,如下所示:
for(i = 0; i< wines.length; i++){
if($scope.countries.indexOf(wines[i].country) === -1) $scope.countries.push(wines[i].country);
};
但现在我正在尝试为葡萄制作一个。我开始收集数组中的所有唯一值:
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]);
};
});
})
所以现在我需要检查一个项目是否有任何与所选过滤器匹配的葡萄,这就是我被卡住的地方:
$scope.filter.grape = {};
$scope.filterByGrape = function(wine){
angular.forEach(wine.grape_names, function(grape){
return $scope.filter.grape[grape] || noFilter($scope.filter.grape);
})
};
过滤器:
<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">
非常欢迎所有帮助!
答案 0 :(得分:0)
问题是你只是检查forEach for grape_names的第一次迭代,你从函数那里返回。
$scope.filter.grape = {};
$scope.filterByGrape = function(wine){
var found = noFilter($scope.filter.grape);
angular.forEach(wine.grape_names, function(grape){
found = $scope.filter.grape[grape] || found;
})
return found;
};