【发布时间】:2016-01-11 05:26:10
【问题描述】:
我想实现基于复选框的过滤器。我的餐馆有多个特色菜,我存储在一个数组中。还有一个或多个餐馆可能有相同的特色菜。
我已经将专业存储在不同的对象中。连同 id 和 name。
现在,每当我选择一个特色菜时,我想检查该特色菜是否存在于餐厅的特色菜系列中。如果是,那么返回这家餐厅。
这就是我到目前为止所做的。
HTML
<div ng-app="myApp" ng-controller="myCtrl">
<input type="text" ng-model="search_cat.name">
<br>
<b>Category:</b>
<div ng-repeat="cat in cuisines">
<b><input type="checkbox" ng-model="filterxyz[cat.id]" /> {{cat.name}}</b>
</div>
<hr />
<div ng-repeat="w in filtered=(restaurants | filter:filterByCategory) ">
{{w.name}}
</div>
<hr />
Number of results: {{filtered.length}}
</div>
JS
var app = angular.module('myApp', []);
app.controller('myCtrl', function ($scope) {
$scope.restaurants = [
{ name: "Restaurant A", specialities:['1','2','3'] },
{ name: "Restaurant B", specialities:['1','2'] },
{ name: "Restaurant C", specialities:['1','2','3','4']},
{ name: "Restaurant D", specialities:['1','2','3','4','5']},
{ name: "Restaurant E", specialities:['1']},
{ name: "Restaurant F", specialities:['3'] },
{ name: "Restaurant G", specialities:['1','4']},
{ name: "Restaurant H", specialities:['1','2','3','4'] }
];
$scope.cuisines=[
{ name: "Speciality A1", id: "1"},
{ name: "Speciality A2", id: "2"},
{ name: "Speciality A3", id: "3"},
{ name: "Speciality A4", id: "4"},
{ name: "Speciality A5", id: "5"}
];
$scope.filterxyz = {};
$scope.filterByCategory = function (restaurant) {
return $scope.filterxyz[restaurant.specialities] || noFilter($scope.filterxyz);
};
function noFilter(filterObj) {
for (var key in filterObj) {
if (filterObj[key]) {
return false;
}
}
return true;
}
});
【问题讨论】:
-
您能具体说明您遇到的问题吗?
-
例如,当我选择特色菜 A1 时,它必须获取除餐厅 F 之外的所有餐厅。因为餐厅 F 中不存在特色菜 A1