2010-04-01 6 views
1

Ceci est similaire à this question, mais j'ai pensé que je le reformulerais un peu différemment afin de me rendre plus clair. Je cette JSON retour d'un appel .ajax $:en boucle sur les données json en JavaScript

{ "COLONNES": [ "PERSONID", "PRENOM", "LastName"], "DATA": [[1001, "Scott", » Wimmer "], [1002," Phillip "," Senn "], [1003," Paul "," Nielsen "]]}

Q: En JavaScript, comment puis-je l'analyser pour faire une table telle que :

<table> 
<thead> 
    <tr> 
    <th>PersonID</th> 
    <th>First Name</th> 
    <th>Last Name</th> 
    </tr> 
</thead> 
<tbody> 
    <tr> 
    <td>1001</td> 
    <td>Scott</td> 
    <td>Wimmer</td> 
    </tr> 
    <tr> 
    <td>1002</td> 
    <td>Phillip</td> 
    <td>Senn</td> 
    </tr> 
    <tr> 
    <td>1003</td> 
    <td>Paul</td> 
    <td>Nielsen</td> 
    </tr> 
</tbody> 
</table> 

Répondre

3
var yourJson = {"COLUMNS":["PERSONID","FIRSTNAME","LASTNAME"],"DATA":[[1001,"Scott","Wimmer"],[1002,"Phillip","Senn"],[1003,"Paul","Nielsen"]];  
var table = '<table>'; 

table += '<thead><tr><th>' + yourJson.COLUMNS.join('</th><th>') + '</th></tr></thead>'; 
table += '<tbody>'; 

for (var i=0;i<yourJson.DATA.length;i++) { 
    table += '<tr><td>' + yourJson.DATA[i].join('</td><td>') + '</td></tr>'; 
}; 

table += '</tbody>'; 
table += '</table>'; 
+0

Merci beaucoup Matt Lunn! Cette réponse a vraiment ouvert les portes de l'inondation pour moi! –

2

Vous pouvez utiliser le moteur de templating côté client, comme jTemplates ou pure pour atteindre facilement.