Angularjs - Deep Orderby - 如何处理多层排序?
Angularjs - Deep Orderby - How to handle multiple layers of sorting?
我有 table 个事件(请参阅下面的数组),我正在尝试按状态对它们进行排序,顺序为“初始”>“正在进行”>“审查中”>“已解决”。然后在有组织的状态中,我想按优先级对它们进行排序,因此 P1>P2>P3 然后按 Start_date 组织优先级,以便最旧的在顶部。
我似乎无法在网上找到这种粒度排序的任何例子。有人知道我会怎么做吗?
这是我的数组:
$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>
这里有一个想法,如何编写可以轻松适应此类其他排序问题的排序函数。
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);
sort_order
中的序列将定义顺序。如果您想颠倒顺序,只需将 .reverse()
附加到 .sort(...)
.
Petr 的评论让我找到了正确的方向。找到了一个超级简单的解决方案,我在这里或网上从未见过。
这是我在 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>
在我的控制器中,我创建了 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;
}
};
它在我这边工作得很好。
我有 table 个事件(请参阅下面的数组),我正在尝试按状态对它们进行排序,顺序为“初始”>“正在进行”>“审查中”>“已解决”。然后在有组织的状态中,我想按优先级对它们进行排序,因此 P1>P2>P3 然后按 Start_date 组织优先级,以便最旧的在顶部。
我似乎无法在网上找到这种粒度排序的任何例子。有人知道我会怎么做吗?
这是我的数组:
$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>
这里有一个想法,如何编写可以轻松适应此类其他排序问题的排序函数。
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);
sort_order
中的序列将定义顺序。如果您想颠倒顺序,只需将 .reverse()
附加到 .sort(...)
.
Petr 的评论让我找到了正确的方向。找到了一个超级简单的解决方案,我在这里或网上从未见过。
这是我在 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>
在我的控制器中,我创建了 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;
}
};
它在我这边工作得很好。