I am a python programmer, so am unsure how to do this in JavaScript.
Example input (from a REST service providing JSON):
[{"name": "foo", "id": 1024}, {"name": "bar", "id": 1025}]
Output:
<table>
<thead>
<tr><th>name</th> <th>id</th></tr>
</thead>
<tbody>
<tr><td>foo</td> <td>1024</td></tr>
<tr><td>bar</td> <td>1025</td></tr>
</tbody>
</table>
Attempt (this is as far as I've gotten, trying just with the list example first from the JQuery docs, before trying to make it a table):
<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<ul class="my-new-list"></ul>
<script>
$.getJSON('http://www.servicestack.net/ServiceStack.Northwind/customers?format=json', function(data) {
var items = [];
$.each(data, function(key, val) {
items.push('<li id="' + key + '">' + val + '</li>');
});
$('<ul/>', {
'class': 'my-new-list',
html: items.join('')
}).appendTo('body');
});
</script>
</body>
</html>
I'm sure it's just some n00bish mistake on my part, but unsure what it is. Could you please point it out?
Preferably I would like a python-like view with dictionary syntax (maybe Handlebars can provide this?), so that I can just go for person in this_list: <tr>person.name</tr>
.