繁体   English   中英

Angularjs - Deep Orderby - 如何处理多层排序?

[英]Angularjs - Deep Orderby - How to handle multiple layers of sorting?

我有一个事件表(请参见下面的数组),我试图按初始 > 进行中 > InReview > 已解决的顺序按 State 对它们进行排序。 然后在有组织的 State 中,我想按优先级对它们进行排序,因此 P1>P2>P3,然后按 Start_date 组织优先级,以便最旧的在顶部。

我似乎找不到任何在线排序粒度的例子。 有谁知道我会如何 go 关于这个?

这是我的数组:

$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>

这是一个如何编写排序 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);

sort_order中的序列将定义顺序。 如果您想颠倒顺序,只需 append .reverse().sort(...)

彼得的评论让我走向了正确的方向。 找到了一个超级简单的解决方案,我在这里或网上的任何地方都没有见过。

这是我在 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>

在我的 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;
    }           

};

它对我来说完美无缺。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM