2014-03-25 68 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/