我試圖根據顯示的創建日期對它們進行分組,以顯示它們的列表。這裏是我的控制器代碼:如何將列表中的元素進行分組並在angularjs中顯示
$scope.itemList = function() {
return itemService.getList();
};
這裏是我的HTML:
<div ng-repeat="date in itemDates = (itemList()| unique:dtTime)">
<div id="itemDay" class="itemFont">{{getAbbrevatedDate(date.dtTime)}}</div>
<div ng-repeat="item in itemList()"">
<div ng-show="getAbbrevatedDate(date.dtTime) === getAbbrevatedDate(item.dtTime)" id="itemRow">
<div id="itemRowContent">
<div id="itemName" class="itemFont">{{item.name}}</div>
<div id="itemDescription" class="itemFont">{{item.description}}</div>
</div>
</div>
</div>
</div>
在itemList中的數據是:
[{
"name":"itemx001",
"dtTime":"2016-01-13T10:17:31.279-05:00",
"description":"this item delivered last week"
},
{
"name":"itemx002",
"dtTime":"2016-01-13T10:17:31.279-05:00",
"description":"this item delivered yesterday"
},
{
"name":"itemx003",
"dtTime":"2016-01-12T10:17:31.279-05:00",
"description":"this item delivered last month"
},
{
"name":"itemx004",
"dtTime":"2016-01-12T10:17:31.279-05:00",
"description":"this item delivered last November"
}
{
"name":"itemx005",
"dtTime":"2016-01-12T10:17:31.279-05:00",
"description":"this item delivered last October"
}]
getAbbrevatedDate()返回今天,昨天,上週等。
我收到以下輸出:
Today
itemx001
this item delivered last week
itemx002
this item delivered yesterday
Today
itemx001
this item delivered last week
itemx002
this item delivered yesterday
yesterday
itemx003
this item delivered last month
itemx004
this item delivered last November
itemx005
this item delivered last October
yesterday
itemx003
this item delivered last month
itemx004
this item delivered last November
itemx005
this item delivered last October
It is repeating the loop for each item. But I am trying to display like this
Today
itemx001
this item delivered last week
itemx002
this item delivered yesterday
yesterday
itemx003
this item delivered last month
itemx004
this item delivered last November
itemx005
this item delivered last October
如何將這些項目按創建日期分組而不重複?我嘗試了獨特的,但它沒有奏效。
應該dtTime是在過濾器中的字符串?即'unique:'dtTime'' –
爲什麼你有'itemList()'的嵌套重複?取出 – charlietfl
是的,它是字符串。 getAbbrevatedDate()將dtTime作爲參數並返回縮寫的日期。 – hanu