1 回答
TA贡献2016条经验 获得超9个赞
首先,使用 HTML 创建一个空表。
<table id="dynamic_table"></table>
接下来,我们可以使用 jQuery 填充表格。
var data_from_post = ...
//create head by looping through keys of first object in result
var table_head = $('<tr></tr>').wrap('<thead></thead>');
$.each(data_from_post[0], function(key) {
table_head.append('<th>' + key + '</th>');
});
//create body by looping through each result, then looping through each key in that result
var table_body = $('<tbody></tbody>');
$.each(data_from_post, function(index, values) {
var row = $('<tr></tr>');
$.each(values, function(key, value) {
//here you can perform changes to the 'value' variable if you need it to be different in the table
//for example, wrap the `_DwgNo` with a button
if(key == '_DwgNo') {
value = '<button>' + value + '</button>'
}
row.append('<td>' + value + '</td>');
});
table_body.append(row);
});
//add the head and body to the table
$('#dynamic_table').append(table_head).append(table_body);
这是包含此解决方案的 jsfiddle
https://jsfiddle.net/2Lon5vj3/1/
- 1 回答
- 0 关注
- 85 浏览
添加回答
举报