I have the following .ajax
$.ajax({
url: urlpath,
type: 'POST',
dataType: 'json',
data: JSON.stringify(json),
contentType: 'application/json; charset=utf-8',
success: function (data) {
alert(JSON.stringify(data));
},
error: error
});
What I am passing back is a list with 3 columns. When I do alert(JSON.stringify(data)); it shows me the data - 3 columns and 4 rows of data. How do I go about parsing this and storing it into a table?
Say you have a table with an id="my-table"
, you can substitute your alert with something like:
$('#my-table tr').each(function (r) {
$('td', this).each(function (c) {
// here you cycle on every td (column) of a row to populate it
// example with 3 columns assuming this json structure
// {
"row1": [ 100, 200, 300],
"row2": [ 50, 200, 400 ],
"row3": [ 10, 300, 200]
// }
this.innerHTML = data["row"+r][c]
})
});
$('#my-table tr')...
find create an array with your table rows$('td', this)...
apply an anonimous function on every td
of the loop current tr
(this
acts as the search context for tds