[英]Angularjs - Deep Orderby - How to handle multiple layers of sorting?
I have a table of Incidents ( See array below) and im trying to sort them by State in the order of Initial > Ongoing > InReview > Resolved.我有一个事件表(请参见下面的数组),我试图按初始 > 进行中 > InReview > 已解决的顺序按 State 对它们进行排序。 Then within the organised State, I want to sort them by Priority so P1>P2>P3 and then organise the priority by Start_date so that the oldest is on the top.
然后在有组织的 State 中,我想按优先级对它们进行排序,因此 P1>P2>P3,然后按 Start_date 组织优先级,以便最旧的在顶部。
I cant seem to find any example of such granularity of sorting online.我似乎找不到任何在线排序粒度的例子。 Would anyone know how I would go about this?
有谁知道我会如何 go 关于这个?
Here is my array:这是我的数组:
$scope.Incidents= [{
Start_date: "2021-12-01 09:20:00"
State: "Initial"
Priority: "P1"
},{
Start_date: "2021-11-01 07:20:00"
State: "Ongoing"
Priority: "P2"
},{ Start_date: "2021-10-01 05:20:00"
State: "Resolved"
Priority: "P3"
},{ Start_date: "2021-12-01 09:48:00"
State: "Ongoing"
Priority: "coach"
},{ Start_date: "2021-11-20 06:55:00"
State: "InReview"
Priority: "P1"
},{ Start_date: "2021-08-01 09:20:00"
State: "InReview"
Priority: "P2"
}];
<div ng-repeat="incident in Incidents| orderBy:Custom_order >
<div>{{incident.Priority}} - {{incident.Priority}} - {{incident.Start_date}}</div>
</div>
Here is an idea how to write a sort function that can easily be adapted to other sorting problems of this kind.这是一个如何编写排序 function 的想法,该排序可以轻松适应此类其他排序问题。
let sort_order = ["Initial", "Ongoing", "InReview", "Resolved"];
let sort_func = function(a, b){
return sort_order.indexOf(a.State) - sort_idx.indexOf(b.State);
}
$scope.Incidents.sort(sort_func);
The sequence inside sort_order
will define the order. sort_order
中的序列将定义顺序。 In case you want the order reversed, just append .reverse()
to .sort(...)
.如果您想颠倒顺序,只需 append
.reverse()
到.sort(...)
。
Petr`s comment sent me into the right direction.彼得的评论让我走向了正确的方向。 Found a super easy solution that i havent seen anywhere here or online.
找到了一个超级简单的解决方案,我在这里或网上的任何地方都没有见过。
Heres what i added in my ng-repeat:这是我在 ng-repeat 中添加的内容:
<div ng-repeat="incident in Incidents| orderBy:[Custom_order_State,Custom_order_Priority, 'Start_date'] >
<div>{{incident.State}} - {{incident.Priority}} - {{incident.Start_date}}</div>
</div>
In my controller I created 2 custom order functions like so:在我的 controller 中,我创建了 2 个自定义订单函数,如下所示:
$scope.Custom_order_State= function (incident) {
if(incident.State=== 'Initial'){
return 1;
}
if(incident.État === 'Ongoing'){
return 2;
}
if(incident.État === 'InReview'){
return 4;
}
if(incident.État === 'Resolved'){
return 5;
}
};
$scope.Custom_order_Priority= function (incident) {
if(incident.Priority=== 'P1'){
return 1;
}
if(incident.Priority=== 'P2'){
return 2;
}
if(incident.Priority=== 'P3'){
return 3;
}
};
Its working perfectly on my end.它对我来说完美无缺。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.