遍歷對像數組以構建 div 結構 (Iterate over array of object to build a div structure)


問題描述

遍歷對像數組以構建 div 結構 (Iterate over array of object to build a div structure)

我正在嘗試在 Reports 數組中使用角度 ng‑repeat 這些數據創建如下圖所示的結構。我知道我必須使用某種迭代來實現它,但我很迷茫。

在此處輸入圖片描述

$scope.Reports = 
 [
 { Id: 1, Name: 'Report One', Year: 2016, Month: 5 },
 { Id: 2, Name: 'Report Core', Year: 2016, Month: 5 },
 { Id: 3, Name: 'Report Alpha', Year: 2016, Month: 3 },
 { Id: 4, Name: 'Report Moon', Year: 2015, Month: 5 },
 { Id: 5, Name: 'Report Sky', Year: 2015, Month: 2 }
 ];

我不確定是否我將需要創建另一個數組來存儲一些數據。像這樣的東西

$scope.years = [];
$scope.months = [];

任何幫助將不勝感激。謝謝


參考解法

方法 1:

May be this will give you some idea ‑

<tr ng‑repeat="(key, value) in Reports">
  <td> {{key}} </td> <td> {{ value }} </td>
</tr>

Let me know if it works or not

方法 2:

I think You can do it using angular.filter module

<ul ng‑repeat="(key, value) in Reports | groupBy: 'Year'">
  {{ key }}
  <ul ng‑repeat="(key1, value1) in value | groupBy: 'Month'">
    O{{key1}} 
  <li ng‑repeat="p in value1">
    {{p.Name }} 
  </li>
</ul>
</ul>

JSBin Hope it will Help

方法 3:

You can do this if you can find a way to group by your reports based on date from Controller. Here is the code pen

$scope.getReportGroup = function() {
var groupedReports = {};
angular.forEach($scope.Reports, function(obj, i) {
  if (!groupedReports.hasOwnProperty(obj.Year)) {
    groupedReports[obj.Year + ''] = [];
  }
  groupedReports[obj.Year + ''].push(obj);
});
return groupedReports;
}

(by Gilberto QuinterouzaifRoli AgrawalRishab777)

參考文件

  1. Iterate over array of object to build a div structure (CC BY‑SA 2.5/3.0/4.0)

#ng-repeat #javascript #angularjs #arrays #angularjs-ng-repeat






相關問題

ng-repeat 加載後不顯示表行 (ng-repeat not showing table rows after loading)

僅在 ng-repeat 中將 ng-if 應用於所選項目 (Apply ng-if in ng-repeat only to selected item)

AngularJS:獲取複雜過濾數組的大小 (AngularJS: Get size of complex filtered array)

單擊時更改 ng-class(嵌套 ng-repeat) (Changing ng-class when clicked (nested ng-repeat))

帶有選擇/選項下拉框的嵌套 ng-repeat 問題 (Nested ng-repeat issue with select/options drop down box)

錯誤:[ngRepeat:dupes] 不允許在轉發器中重複。使用“track by”表達式指定唯一鍵 (Error: [ngRepeat:dupes] Duplicates in a repeater are not allowed. Use 'track by' expression to specify unique keys)

在 AngularJS 中保存在 ng-repeat 中生成的字段 (Saving a field generated in ng-repeat in AngularJS)

遍歷對像數組以構建 div 結構 (Iterate over array of object to build a div structure)

ng-repeat 中 IE 下拉列表中的 Angularjs 無法正確呈現 (Angularjs in IE dropdowns in ng-repeat do not render correctly)

動態 ng-repeat 表單的優雅解決方案 (Elegant solution for a dynamic ng-repeat form)

如何區分 Rs 和 % 值? (How to differentiate Rs and % values?)

難以為一組對象運行 ng-repeat (difficulty running ng-repeat for an array of objects)







留言討論