簡體   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