2016-12-05 60 views
0

我有以下對象,其中包含2個固定屬性(OrderId和Purchasedate,以及一個attribues數組,我試圖用orderBy選項將它放入ng-repeat中。當通過點擊標題應用分類屬性(的OrderId和而purchaseDate)工作確定,但我不明白工作的第三屬性等。ng-repeat orderby對象的動態長度

上表中顯示的行是正確的。

物體看起來像 例如

$scope.orders; 
[ 
    { OrderId: "P888291", PurchaseDate : "2016-12-02", 
    Items: { elt : [ { Name: "City", Value: "New York"}, { Name: "Street", Value: "Broadway 5" }, { Name: "Country", Value: "USA" } ] } }, 
{ OrderId: "P334498", PurchaseDate : "2016-11-02", 
    Items: { elt : [ { Name: "City", Value: "London" }, { Name: "Street", Value: "WestMinister 3" }, { Name: "Country", Value: "Great Brittain" } ] } }, 
{ OrderId: "G393383", PurchaseDate : "2016-11-28", 
    Items: { elt : [ { Name: "City", Value: "Milan" }, { Name: "Street", Value: "Pizza 8" }, { Name: "Country", Value: "Italy" } ] } }, 
{ OrderId: "P978381", PurchaseDate : "2015-05-25", 
    Items: { elt : [ { Name: "City", Value: "Seattle" }, { Name: "Street", Value: "Houston 9" }, { Name: "Country", Value: "US" } ] } }, 
{ OrderId: "P983394", PurchaseDate : "2015-06-05", 
    Items: { elt : [ { Name: "City", Value: "Amsterdam" }, { Name: "Street", Value: "Damrak 5" }, { Name: "Country", Value: "Netherlands" } ] } }, 
{ OrderId: "G678994", PurchaseDate : "2015-04-01", 
    Items: { elt : [ { Name: "City", Value: "The Hague" }, { Name: "Street", Value: "Markt 22" }, { Name: "Country", Value: "Netherlands" } ] } }, 

{ OrderId: "P128994", PurchaseDate : "2016-12-04", 
    Items: { elt : [ { Name: "City", Value: "The Hague" }, { Name: "Street", Value: "Plein 7" }, { Name: "Country", Value: "Netherlands" } ] } }, 
]; 

請告知和代碼放在:

http://www.w3schools.com/code/tryit.asp?filename=FAG7BWVK8BYH

回答

0

您可以自定義過濾器的邏輯嘗試(https://docs.angularjs.org/api/ng/filter/orderBy) 例如:

JS:

$scope.filterOrderFn = function(orderobj) 
{ 
// Do 

if(...) 
{ 
    return _something_// this will be your sorted order according to your first condition 
} 
else if(...) 
{ 
    return _something_ // this will be your sorted order according to your second condition if require 
} 

return false; // otherwise it won't be within the orderlist 
}; 

HTML:

... 
<article data-ng-repeat="order in orders | orderBy:filterOrderFn" class="order"> 
... 

如果您需要非常特定的排序行爲,您可以編寫自己的過濾器(儘管orderBy對於大多數使用情況應該足夠了)。正如你可能知道你可以將許多過濾器鏈接在一起,因此添加自定義過濾器函數並不會強制您使用搜索對象刪除之前的過濾器(它們將無縫地一起工作)。