javascript - Sorting array in angular doesn't work -
i have following structure:
array( array('id' => 1, 'name' => 'aaa', 'thumbnailurl' => 'some1'), array('id' => 2, 'name' => 'ccc', 'thumbnailurl' => 'some2'), array('id' => 3, 'name' => 'bbb', 'thumbnailurl' => 'some3') )
and display this:
<div ng-controller="creationcontroller"> <input type="radio" name="sortby" id="id" ng-click="reverse=false;order('id',reverse)"><label for="id">id</label> <input type="radio" name="sortby" id="name" ng-click="reverse=false;order('name',reverse)"><label for="name">name</label> <div class="creations accordion" ng-repeat="creation in creations"> <h3> {if !empty("[[creation.thumbnailurl]]")} <img src="[[creation.thumbnailurl]]" class="creation-thumb-header"> {else} <img src="/image/placeholder.png" class="creation-thumb-header"> {/if} #[[creation.id]] [[creation.name]] </h3> <div class="row"> [[creation.name]] </div> </div> </div>
i use smarty that, along angular ([[ , ]] tags). problem array not being sorted. clicking radio buttons changes nothing.
what's wrong, , how this?
exampleapp.controller('creationcontroller', ['$scope', '$filter', function($scope, $filter) { var orderby = $filter('orderby'); $scope.creations = creations; $scope.order = function(predicate, reverse) { $scope.creations = orderby($scope.creations, predicate, reverse); }; $scope.order('id',true); }]);
also, there way using select? provide example?
you should use field , reverse information directly on repeat:
i made jsbin show this: jsbin angular orderby
at code:
<div class="creations accordion" ng-repeat="creation in creations | orderby : field : reverse">
this code take care of reverse field, invert on second click:
<button ng-click="order('id')">order id</button> <button ng-click="order('name')">order name</button>
with function:
$scope.order = function(field) { $scope.reverse = ($scope.field === field) ? ! $scope.reverse : false; $scope.field = field; };
Comments
Post a Comment