2016-03-11 2 views
0

Я пытаюсь создать две таблицы из массива.Handlebars js, получить конкретный массив объектов и цикл через

"tables": [ 
{ 
"tableName": "table1", 
"dataRow": ["name": "test858"] 
}, 
{ 
"tableName": "table2", 
"anotherRow": ["name": "test123"] 
} 
] 

я пытался делать это

{{#each tables}} 
      <table> 
       <thead> 
        <tr> 
         <th>{{tableName}}</th> 
         <th></th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr> 
         {{#if dataRow}} 
         <td>test1</td> 
         {{else}} 
         <td>test2</td> 
         {{/if}} 
        </tr> 
       </tbody> 
      </table> 
{{/each}} 

печатается два tablenames и "test2"

Как я могу перебрать обоих массивов?

ответ

0

JSFiddle Рабочий пример:

var entry_template = document.getElementById('entry-template').innerHTML; 
var tables = [ 
{ 
"tableName": "table1", 
"dataRow": [{"name": "test858"}] 
}, 
{ 
"tableName": "table2", 
"anotherRow": [{"name": "test123"}] 
} 
]; 

var template = Handlebars.compile(entry_template); 
var template_with_data = template(tables); 

document.getElementById('data').insertAdjacentHTML('afterbegin', template_with_data); 

Шаблон:

<script id="entry-template" type="text/x-handlebars-template"> 
{{#each this}} 
      <table> 
       <thead> 
        <tr> 
         <th>{{tableName}}</th> 
         <th></th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr> 
         {{#if dataRow}} 
         <td>test1</td> 
         {{else}} 
         <td>test2</td> 
         {{/if}} 
        </tr> 
       </tbody> 
      </table> 
{{/each}} 
</script> 
<div id="data"> 
</div> 
Смежные вопросы