Ngfor对象列表,并从主数组中某个对象内的数组中再次循环

时间:2017-04-10 05:26:12

标签: javascript jquery html angular typescript

您好,我有以下数据结构,

this._days=[

            {
               "day": "",
               "timing": {}
            },
            {
               "day": "Monday",
               "timing": [
                       {'9:00AM-10:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'10:00AM-11:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'11:00AM-12:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'12:00AM-13:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'13:00AM-14:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}
                    ]
            },
            {
               "day": "Tuesday",
               "timing": [
                       {'9:00AM-10:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'10:00AM-11:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'11:00AM-12:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'12:00AM-13:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}, 
                       {'13:00AM-14:00AM': {'subject': 'Physics','teacher': 'Amanda','Location': 'Room 05'}}
                    ]
            },
...

我想显示像

这样的值

enter image description here

在我尝试的html中,

<table width="100%" align="center" height=100%;>
    <tr class="day">
        <th *ngFor="let row of _days">{{row.day}}</th>
    </tr>
    <tr class="time" *ngFor="let x of _days.timing">
        <th style="width:100px">{{_days.timing[x][key]}}</th>
            <td>{{_days.timing[x].value}}</td>
    </tr>
</table>

无论如何,我可以在我的HTML中实现这一点,这样我就能得到预期的结果,如图所示。先谢谢你们。

1 个答案:

答案 0 :(得分:1)

首先,您需要根据时间过滤数据

var tabelRows = {};
var firstRow = [];
_days.forEach(function(v,i){
   v.timing.forEach(function(val,ind) {
      var row = Object.keys(val)[0];
      if(firstRow.indexOf(row) == -1) {
           firstRow.push(row);
           tabelRows[row] = [val[row]];
      } else {
      tabelRows[row].push(val[row]);
      }
   });
});

然后执行forEach循环,例如:

<tr class="time" *ngFor="let x of firstRow">
        <th style="width:100px">{{x}}</th>
            <td *ngFor="let y of tabelRows[x]">{{y.subject}}</td>
    </tr>