angularjs - orderBy not working in HTML divs -
i trying implement ng-repeat functionality divs. need sort data first (according rank has, provided object) , name div. creating custom orderby function . sorting functionality works well. however, getting order of divs same. (infact, opposite of need). tried using reverse functionalities, doesn't work.
my sorting code looks this:
var res = { "toyota": 2, "ford": 1, "chrysler": 4, "hyundai": 3, "nissan": 5 }; sortedval = object.keys(res).sort(function (a, b){ return res[a] - res[b]; });
my html div looks this,
<div class="card" ng-repeat="name in data | orderby: sortedval ">
i correct output in console. however, divs arranged in order given below, no matter do.
nissan, chrysler, hyundai, toyota, ford.
i need them in order: ford, toyota, hyundai, chrysler, nissan.
note: don't worry "name in data" part, works well. having problems "orderby".
orderby
either takes string object property or function defines order. no need calculate sortedval
yourself, need give orderby
method used calculate sortedval
:
$scope.orderfn = function(a, b) { return res[a] - res[b] } <div ng-repeat="name in data | orderby: orderfn"></div>
plunker: http://plnkr.co/edit/loeblx28wjav4rcvrxwq?p=preview
Comments
Post a Comment