javascript - AngularJS - parse JSON -
i have list of photographs being generated following snippet. render table structure, each photo being cell in table. id of each photo example 1d means photo in first row of table , in 4th/d column.
<ul> <li class="row"> <ul> <li class="photo" id="photo-1a">1a</li> <li class="photo" id="photo-1b">1b</li> <li class="photo" id="photo-1c">1c</li> <li class="photo" id="photo-1d">1d</li> <li class="photo" id="photo-2a">2a</li> <li class="photo" id="photo-2b">2b</li> <li class="photo" id="photo-2c">2c</li> <li class="photo" id="photo-2d">2d</li> <li class="photo" id="photo-3a">3a</li> <li class="photo" id="photo-3b">3b</li> <li class="photo" id="photo-3c">3c</li> <li class="photo" id="photo-3d">3d</li> </ul> </li> </ul> i have json includes whether photo available or not. json string along these lines:
[{"row":1,"position":"a","available":true},{"row":1,"position":"b","available":false},{"row":1,"position":"c","available":false},{"row":1,"position":"d","available":false},{"row":2,"position":"a","available":true},{"row":2,"position":"b","available":false},{"row":2,"position":"c","available":false},{"row":2,"position":"d","available":false},{"row":3,"position":"a","available":true},{"row":3,"position":"b","available":false},{"row":3,"position":"c","available":false},{"row":3,"position":"d","available":false}] now need parse json string , when of these photos have "available:true" in json string, add class photo-available in html. new angular , not sure if there easy way assign class available photos. glad if can tell me use or how it.
edit: angular code this:
<ul class="table-rows"> <li class="photo-row" ng:repeat="photo in photos" ng:class="'photo-' + photo.row + photo.position"> <ul class="table-photos"> <li class="photo photo-available" ng:class="selectedornot(photo)" ng:init="photo.selected = false" ng:click="photo.selected = !photo.selected"> <div class="photo-number">{{photo.row + photo.position}}</div> </li> </ul> </li> <div class="clear"></div>
update3
reason unable restore previous selections overwriting photo's selected property ng-init:
ng:init="photo.selected = false" ng-class="{'selected': photo.selected, 'available': photo.available}" when combine these two, 'selected' class never added because photo.selected has been hardcoded false. need remove ng-init, , previous selection trigger ng-class add correct class.
here working demo: http://plnkr.co/tvdhrilaffcn55h6mogu
original answer
if list of photos not same array list of available photos, can use directive add class.
app.directive('availablephoto', function($filter) { return { restrict: 'a', scope: true, link: function(scope, element, attr) { var id = attr.id var regex = /photo-(.)(.)/g; var match = regex.exec(id); var row = match[1] var position = match[2] var photo = $filter('filter')(scope.photos, {row:row, position:position}, false) console.log(photo); if (photo[0].available) { element.addclass('available'); } } } }); then attach each list item this:
<li class="photo" id="photo-1a" available-photo>1a</li> here demo: http://plnkr.co/wjcmlf2m39fcunvopyna
update1
based on update, see there 1 array populating list, , contains available flag. therefore, don't need custom directive - ngclass work. here integrated code sample:
<ul class="table-rows"> <li class="photo-row" ng:repeat="photo in photos" ng:class="'photo-' + photo.row + photo.position"> <ul class="table-photos"> <li class="photo" ng-class="{'available': photo.available}" ng:init="photo.selected = false" ng:click="photo.selected = !photo.selected"> <div class="photo-number">{{photo.row + photo.position}} </div> </li> </ul> </li> <div class="clear"></div> </ul> i have update plunker demonstrate this.
http://plnkr.co/wjcmlf2m39fcunvopyna
update2
since need ngclass add multiple classes, use this:
ng-class="{'selected': photo.selected, 'available': photo.available}" demonstration of selected + available: http://plnkr.co/wjcmlf2m39fcunvopyna
Comments
Post a Comment