2016-01-15 41 views
0

看着我的数据多维数组不循环,即将通过:可通过与车把

data: { 
    content: [ 
    [ 
    "School Name", 
    "Location", 
    "Type", 
    "No. eligible pupils", 
    "Average points per student", 
    "Average points per exam entry", 
    "% obtaining two facilitating subjects" 
    ], 
    [ 
    "Colchester Royal Grammar School", 
    "Colchester", 
    "State", 
    "349", 
    "1428", 
    "263.3", 
    "77%" 
    ], and so on... 
] 
} 

我通过这个阵列阵列试图循环,创建一个表。因此,对于每个阵列,我需要将其包装在<tr></tr>中,并且对于每个阵列中的每个元素,我需要将它包装在<td></td>中。我需要区分第一行,以便使用<thead><th>,但目前我正试图让我的头绕着正确的结构。

我的代码所做的只是创建一个包含整个事物的<td>,而不是多个<tr> s或<td> s。

 {{#each data.content}} 

      <tr> 

       {{#each this}} 
        <td>{{ this }}</td> 
       {{/each}} 

      </tr> 

     {{/each}} 
+0

是数据数组或数据对象ECT? – sundar

+1

'data:[content:[]]'这个语法是错误的。不应该是'data:{content:[]}'? – Venugopal

+0

1秒,出现错误,编辑现在.. – geodeath

回答

2

你不能直接使用您的template.because内部的数据要传递的数据对象编译模板function.so你应该用它来指当前上下文。

更好地利用模块参数,以避免更多的这种reference.which的数量将使代码更难理解

无块参数

{{#each this.content}} 
    <tr> 
     {{#each this}} 
      <td>{{ this }}</td> 
     {{/each}} 
    </tr> 
{{/each}} 

用块参数,

{{#each this.content as | rowKey, row |}} 
     <tr> 
      {{#each row as | colKey, column|}} 
       <td>{{ column }}</td> 
      {{/each}} 
     </tr> 
{{/each}} 

它当模板变大时具有更多优点