2014-03-25 63 views
0

工作嵌套屬性我有2個對象results_.keys(result[0])Ractivejs:如何獲得鑑於

r{ 
    data:{ 
    headers:['head1','head2'] 
    result:[ 
     {head1:'content1',head2:'content2'} 
     {head1:'content3',head2:'content4'} 
     {head1:'content5',head2:'content6'} 
    ] 
} 

產生headersheaders我對dinamically創建一個表,所以我創建這個:

<table class="ui celled table segment"> 
    <thead> 
    <tr> 
    {{#headers}} 
    <th>{{.}}</th> 
    {{/headers}} 
    </tr></thead> 
    <tbody> 
    {{#result:i}} 
    <tr> 
     {{#headers:h}} 
     <td>{{????}}</td> <-- Here is where I fail to know what to put into 
     {{/headers}} 
    </tr> 
    {{/result}} 
    </tbody> 
</table> 

有人可以幫我填補空白。所以,我可以創建一個表,顯示contents

如果我刪除{{#headers}}一部分,我已經知道的元素<td>{{.head1}}</td>工作完美,問題是我'產生的飛行不同的對象。

回答

0
{{#result:i}} 
    <tr> 
    {{#headers:h}} 
     <td>{{result[i][this]}}</td> 
    {{/headers}} 
    </tr> 
{{/result}} 

這部作品的原因是<td>重複的headers陣列中的每個項目,因爲它是一個headers段內 - 到目前爲止,那麼明顯。因此,我們可以使用this來指代當前標題(head1,head2等)。訣竅是獲取對當前行的引用 - 因爲您已經創建了i索引引用,所以我們可以使用result[i]輕鬆完成此操作。因此result[i][this]

這是演示提琴:http://jsfiddle.net/rich_harris/dkQ5Z/