javascript - Angular: Filter Multiple Items -
javascript - Angular: Filter Multiple Items -
i'm new angular , i'm having issues filtering function.
first of all, content getting pulled in json file. pretty simple:
[ { "id":"0", "animal":"cat", "breed":"siamese", "name":"kevin", "photo": "/images/kevin.jpg" }, { "id":"1", "animal":"dog", "breed":"pug", "name":"barney", "photo": "/images/barney.jpg" } ]
and forth.
html on page. nav points come own json files have nil data, meant create menus , pass value:
<ul> <li ng-repeat="item in animal_data"> <a href="" ng-click="filterdata(item.animal)">{{item.animal | uppercase}}</a> </li> </ul> <ul> <li ng-repeat="item in breed_data"> <a href="" ng-click="filterdata(item.breed)">{{item.breed | uppercase}}</a> </li> </ul>
script filter:
$scope.filterdata = function(item) { var passfiltertype = $filter('filtertype')($scope.original_data, item); if(passfiltertype.length > 0) { $scope.isloading = true; $scope.issuccessful = false; $scope.percentloaded = 0; $scope.thumbnails = passfiltertype; loadimages(); }else{ console.log("error"); } } function loadimages() { var passimages = getimages(); if(passimages.length > 0) { preloader.preloadimages(passimages).then(handleresolve, handlereject, handlenotify); } }
my plan convert li's check boxes , brings me point need help. there way take i've set utilize filtering multiple animals , breeds?
thanks can help.
i think might you're looking (run code snippet @ end), or give thought or two. might want reorganize code purposes.
the key points are:
ng-reapeat checkboxes based off unique breeds , animals. used unique filter ui.filters, roll own.
<label ng-repeat="item in animal_data | unique:'breed'">{{item.breed}} <input type="checkbox" ng-model="selected[item.breed]"></input> </label>
using built in filter on ng-repeat lists results, , providing function look evaluates true selected items checkboxes.
<li ng-repeat="item in animal_data | filter:check(selected)">
and creating function:
$scope.check = function(selected) { homecoming function(item) { if (selected[item.animal] || selected[item.breed]) homecoming true; else homecoming false; }; }
and together...
class="snippet-code-js lang-js prettyprint-override">var app = angular.module('testapp', ['ui.directives', 'ui.filters']); app.controller('testctrl', function($scope) { $scope.selected = {}; $scope.animal_data = [{ "id": "0", "animal": "cat", "breed": "siamese", "name": "kevin", "photo": "/images/kevin.jpg" }, { "id": "1", "animal": "dog", "breed": "pug", "name": "barney", "photo": "/images/barney.jpg" }, { "id": "2", "animal": "dog", "breed": "poodle", "name": "charlie", "photo": "/images/barney.jpg" }]; $scope.check = function(selected) { homecoming function(item) { if (selected[item.animal] || selected[item.breed]) homecoming true; else homecoming false; }; } $scope.filterdata = function(item) { var passfiltertype = $filter('filtertype')($scope.original_data, item); if (passfiltertype.length > 0) { $scope.isloading = true; $scope.issuccessful = false; $scope.percentloaded = 0; $scope.thumbnails = passfiltertype; loadimages(); } else { console.log("error"); } } }); function loadimages() { var passimages = getimages(); if (passimages.length > 0) { preloader.preloadimages(passimages).then(handleresolve, handlereject, handlenotify); } }
class="snippet-code-html lang-html prettyprint-override"><script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/angular-ui/0.4.0/angular-ui.min.js"></script> <div ng-app='testapp'> <div ng-controller="testctrl"> <li ng-repeat="item in animal_data">{{item}}</li> <ul> breed: <label ng-repeat="item in animal_data | unique:'breed'">{{item.breed}} <input type="checkbox" ng-model="selected[item.breed]"></input> </label> <br/>animal: <label ng-repeat="item in animal_data | unique:'animal'">{{item.animal}} <input ng-model="selected[item.animal]" type="checkbox"></input> </label> <table width="400"> <thead> <th>animal</th> <th>breed</th> <th>name</th> <th>link</th> <tr ng-repeat="item in animal_data | filter:check(selected)"> <td>{{item.animal | uppercase}}</td> <td>{{item.breed}}</td> <td>{{item.name}}</td> <td> <a href="" ng-click="filterdata(item)">filterdatalink</a> </td> </tr> </table> </div> </div>
javascript jquery json angularjs filtering
Comments
Post a Comment