2013-03-23 16 views
8

Perché lo stesso codice oggetto JSON genera l'output con elementi ul, ma non con un tag table.Il modello di baffi non viene visualizzato nella tabella tbody

ho il mio modello di baffi come:

<div id="template-ul"> 
    <h3>{{name}}</h3> 
    <ul> 
     {{#students}} 
     <li>{{name}} - {{age}}</li> 
     {{/students}} 
    </ul> 
</div> 

<div id="template-table"> 
    <table> 
     <thead> 
      <th>Name</th> 
      <th>Age</th> 
     </thead> 
     <tbody> 
     {{#students}} 
      <tr> 
       <td>{{name}}</td> 
       <td>{{age}}</td> 
      </tr> 
     {{/students}} 
     </tbody> 
    </table> 
</div> 

ecco il codice javascript:

var testing = { 
    "name" : "student-collection", 
    "students" : [ 
     { 
      "name" : "John", 
      "age" : 23 
     }, 
     { 
      "name" : "Mary", 
      "age" : 21 
     } 
    ] 
}; 

var divUl = document.getElementById("template-ul"); 
var divTable = document.getElementById("template-table"); 

divUl.innerHTML = Mustache.render(divUl.innerHTML, testing); 
divTable.innerHTML = Mustache.render(divTable.innerHTML, testing); 

Ecco il codice a jsFiddle - http://jsfiddle.net/pRSjH/2/

+0

favore cambia il "Nome: Giovanni" a "fname: Maria", questo non risolverà il problema più grande, ma impedirà l'inutile studenti-collezioni a stampa nella tabella. –

risposta

1

divTable.innerHTML rendimenti this invece di corretta template . Probabilmente succede perché il browser tenta di rendere l'HTML non valido. Penso che si può mettere il modello in <script> tag per risolvere questo problema (the fiddle)

+1

Ehi, grazie. Che funzioni. E quello che ho fatto è stato, creare un file table.tpl separato e recuperarlo al volo e quando è stato caricato, renderizzato il modello con i dati. – divinedragon

27

si può anche commento baffi tag in tbody. E la tabella sarà costruita correttamente.
Il mio modello di esempio:

<div id="template-table"> 
    <table> 
     <thead> 
      <th>Name</th> 
      <th>Age</th> 
     </thead> 
     <tbody> 
     <!--{{#students}}--> 
      <tr> 
       <td>{{name}}</td> 
       <td>{{age}}</td> 
      </tr> 
     <!--{{/students}}--> 
     </tbody> 
    </table> 
</div> 
+0

Man mi hai appena salvato. Questa dovrebbe essere la risposta corretta. Grazie – tiagojpdias

Problemi correlati