angularjs - ng-repeat auto sorting incorrectly -
i know in current version of angular, ng-repeat auto sorts. problem sorting doesn't appear correct.
taking object numbered keys:
{ "1": "value", "2": "value", "10": "value" } ng-repeat sorts 1, 10, 2. i'm familiar sort of sorting , throwing 0 on front of number should fix it. adding 0 requires additional code , need stripped out display.
likewise, converting array caused ng-repeat loop through of empty values (3-9) , creates excess elements, generating duplicate error.
how can make ng-repeat sort object keys if integers?
i not able find solution didn't change data structure, here example of how can done using keys sort object array. here html:
<ul ng-controller="examplecontroller"> <li ng-repeat="item in filtered">{{ item.value }}</li> </ul> and here code:
controller('examplecontroller', ['$scope', function($scope) { $scope.list = { "1": "1 value", "10": "10 value", "5": "5 value", "2": "2 value" }; $scope.$watch('list', function (newval) { $scope.filtered = object.keys(newval).map(function (key) { return { key: key, value: newval[key] }; }); }); }]); the output of code looks this:
- 1 value
- 2 value
- 5 value
- 10 value
note creates array of key/value pair objects easier work with. here plunker example of working: http://plnkr.co/edit/q0bylemztzmd1k8vztlq?p=preview
Comments
Post a Comment