I want to implement checkbox based filters. I have restaurants which has multiple specialties which i have stored in an array.Also one or more restaurants may have same specialty.
I have stored the specialties in different object.Along with the id and name.
Now whenever i select a specialty i want to check if this specialty is present in the specialty array of a restaurant. if yes then return this restaurant.
This is what i managed to do so far.
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;
}
});