Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
2
rated 0 times [  8] [ 6]  / answers: 1 / hits: 17952  / 10 Years ago, sun, may 18, 2014, 12:00:00

This is the json that is generated using PHP



[{Sale:{id:1,customer_id:1,amount:15,created:2014-05-17}}]


Doing orderBy:id is obviously not working. I read in other posts that an orderBy function needs to be created for this kind of data structure. I am not able to create the function. Can anyone please tell me what is the correct way to approach this?


More From » angularjs

 Answers
27

These type of data manipulations I like to keep them in the proper angular objects and for that reason I would create my custom filter, something like:



var sampleSource=  [{Sale:{id:8,customer_id:1,amount:15,created:2014-05-17}}, {Sale:{id:5,customer_id:6,amount:15,created:2015-05-17}}];

var myApp = angular.module('myApp',[]);

myApp.filter('myFilter', function() {
return function(items) {
items.sort(function(a,b){
if (parseInt(a.Sale.id) > parseInt(b.Sale.id))
return 1;
if (parseInt(a.Sale.id) < parseInt(b.Sale.id))
return -1;
return 0; })
});



Important: I recommend the custom filter because as personal preference I do not like to overload my controllers or other objects with tasks(code) that I can separate on other objects which gives me more independence and cleaner code(one of the things I love about angular) but besides this personal preference I would say that this is not the only way but if you share my reasons behind it I hope it helps.



[#70958] Thursday, May 15, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
yvettel

Total Points: 517
Total Questions: 101
Total Answers: 102

Location: Vanuatu
Member since Wed, Oct 14, 2020
4 Years ago
yvettel questions
Tue, Jul 27, 21, 00:00, 3 Years ago
Thu, Aug 13, 20, 00:00, 4 Years ago
Wed, Apr 15, 20, 00:00, 4 Years ago
Wed, Apr 1, 20, 00:00, 4 Years ago
;