javascript - JS output from data structure to table -
data structure var x = { a: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}], b: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}], c: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}], d: [{name:"john", phone:777},{name:"john", phone:777},{name:"john", phone:777}] } function function showtable(trnum,x,a) { var tablecode = "<table><tbody>"; (var i=0; i< x.a.length;i++) { (var j=0; j< trnum; j++) { tablecode += "<tr><td>"+x.a[i].name+"</td><td>"+x.a[i].phone+"</td></tr>"; } } tablecode += "</tbody></table>"; $("#elem").append(tablecode); } markup <body> <button onclick="showtable(4,x,"a");"></button> <div id="elem"> </div> </body>
i'm trying output data table according letter. 1 object on 1 table row.(all objects in table within 1 letter). code doesn't work- unexpected token "}"
. oddly enough, points <div id="elem">
line. there's no such character!! how can fix this??
update: thanks, error disappeared... outputs whole structure, how make output 'a' objects??
<button onclick="showtable(4,x,"a");"></button>
should (doublequotes):
<button onclick="showtable(4,x,'a');"></button>
compare 1 yours (see .a
, [a]
)
for (var i=0; i< x[a].length;i++){ (var j=0; j< trnum; j++){ tablecode += "<tr><td>"+x[a][i].name+"</td><td>"+x[a][i].phone+"</td></tr>"; } }
Comments
Post a Comment