Skip to content

Instantly share code, notes, and snippets.

@citylims
Last active January 20, 2016 22:45
Show Gist options
  • Save citylims/b44869ca11b65c816c23 to your computer and use it in GitHub Desktop.
Save citylims/b44869ca11b65c816c23 to your computer and use it in GitHub Desktop.
app.filter('selectedCategories', function(){
return function(items, cats){
if(!cats){
return items;
}
var choices = cats.reduce(function(p,cat){
if (cat.checked){
p.push(cat.catName);
}
return p;
},[]);
if (items) {
return !choices.length ? items :items.filter(function(item){
return item.catName.filter(function(cat){
return choices.indexOf(cat) >-1;
}).length;
});
}
};
});
app.controller('MainCtrl', function ($http, $scope) {
var vm = this
$http.get(foo).success(function(data) {
$scope.categories = data.map(function(cat) {
return {catName: cat.name}
});
});
$http.get(bar).success(function(data) {
vm.listToMatch = data;
})
});
//example usage in template
//...
<div ng-repeat="item in categories" style="float: left; padding: 10px;">
<b><input type="checkbox" ng-model="item.checked" ng-change="save(item)" />{{item.neighborhoods_name}}</b>
</div>
<div>
<ul>
<li ng-repeat="item in vm.listToMatch | selectedCategories: categories">
//only show item.catName if matches a category in selectedCategories
<p>{{item.catName}}</p>
</li>
</ul>
</div>
//...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment