ajax响应返回json对象,我想从构造一个表。json在一个数组中包含列标题,在另一个字典数组中包含实际数据。我希望表列根据标题数组进行排序:
{
"columns": ["id", "category"],
"data":[
{"category": "fruit","id": 1},
{"category": "diary","id": 2}
]
}
$(document).ready(function() {
$("#submit").click(function(event) {
$.ajax({
data: {
user: $('#user').val(),
},
type: 'POST',
dataType: "json",
url: '/process'
})
.done(function(response) {
if (response.error) {
alert('Error!');
}
else {
var html = '<table class="table table-bordered"><thead><tr>';
jQuery.each(response.columns, function(index, value) {
html += "<th>" + value + "</th>"
})
html += "</tr></thead><tbody><tr>"
jQuery.each(response.data, function(index, value) {
jQuery.each(response.columns, function(idx, col) {
html+="<td>" + value[col] + "</td>"
})
})
html+="</tr></tbody></table>"
$(resulttable).append(html);
}
});
event.preventDefault();
});
});
我得到了一张这样的桌子:
id category
1 fruit 2 diary
instead of
id category
1 fruit
2 diary
https://stackoverflow.com/questions/50650233
复制相似问题