如何使用嵌套财产AngularJS的OrderBy [英] How to orderby in AngularJS using Nested property
问题描述
这是使用PHP产生JSON
[{卖:{ID:1,CUSTOMER_ID:创建,15,:1,量,2014- 05-17}}]
做排序依据:ID
显然是行不通的。我在一个排序依据功能需要为这种数据结构要创建其他职位读取。我不能够创建功能。谁能告诉我什么是解决这个正确的方法是什么?
这些类型的数据操纵我喜欢让他们在适当的角度对象,因为这个原因我会创造我的自定义过滤器,是这样的:
VAR sampleSource = [{卖:{ID:8,CUSTOMER_ID:1,量:15,创造: 2014年5月17日}},{卖:{ID:5,CUSTOMER_ID:6,量:15,创造:2015年5月17日}}];VAR对myApp = angular.module('对myApp',[]);myApp.filter('myFilter',函数(){
返回功能(项){
items.sort(功能(A,B){
如果(parseInt函数(a.Sale.id)GT; parseInt函数(b.Sale.id))
返回1;
如果(parseInt函数(a.Sale.id)LT; parseInt函数(b.Sale.id))
返回-1;
返回0; })
});
重要:我推荐的自定义过滤器,因为个人preference我不喜欢超负荷我的控制器或其他物体与任务(code),我可以在其他对象分开这给了我更多的独立性和清洁code(我爱约角度的原因之一),但是除此之外的个人preference我会说,这是的不的唯一途径,但如果你分享它背后我的原因,我希望它能帮助。
块引用>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?解决方案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.
这篇关于如何使用嵌套财产AngularJS的OrderBy的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!