javascript 如何使用嵌套属性在 AngularJS 中排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23723417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to orderby in AngularJS using Nested property
提问by user727728
This is the json that is generated using PHP
这是使用PHP生成的json
[{"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?
这样做orderBy:id
显然是行不通的。我在其他帖子中读到需要为这种数据结构创建一个 orderBy 函数。我无法创建该函数。谁能告诉我解决这个问题的正确方法是什么?
采纳答案by Dalorzo
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 notthe only way but if you share my reasons behind it I hope it helps.
重要提示:我推荐自定义过滤器,因为作为个人偏好,我不喜欢用任务(代码)重载控制器或其他对象,我可以将这些任务(代码)分离到其他对象上,这给了我更多的独立性和更清晰的代码(我喜欢的事情之一) angular)但除了这个个人偏好之外,我想说这不是唯一的方法,但如果你分享我背后的原因,我希望它会有所帮助。
回答by KinoP
Suppose you have:
假设你有:
$scope.sales = [{"Sale":{"id":"1"}},{"Sale":{"id":"2"}},{"Sale":{"id":"3"}}];
Why not just do following:
为什么不做以下事情:
<div ng-repeat="sale in sales | orderBy:'Sale.id':true">
{{ sale.Sale.id }}
</div>
It works well with my case. May be Angular did not support it at the time you asked the question.
它适用于我的情况。可能是 Angular 在您提出问题时不支持它。
回答by Michalis
myapp.filter('orderBySub', function () {
return function (items, field, reverse) {
byString = function(o, s) {
s = s.replace(/\[(\w+)\]/g, '.');
s = s.replace(/^\./, '');
var a = s.split('.');
for (var i = 0, n = a.length; i < n; ++i) {
var k = a[i];
if (k in o) {
o = o[k];
} else {
return;
}
}
return o;
}
items.sort(function (a, b) {
if (byString(a, field) > byString(b, field))
return reverse ? -1 : 1;
if (byString(a, field) < byString(b, field))
return reverse ? 1 : -1;
return 0;
})
return items;
}
});
Example
例子
ng-repeat="task in tasks | orderBySub: 'field1.subfield2.subsubfield3':true"
true = reverse
真 = 反向