这是我的JSON格式
{
"result": {
"buildname1": [{
"table1": ["xxx","yyy"]
}, {
"table2": ["xxx","yyy"]
}, {
"table3": ["xxx","yyy"]
}],
"buildname2": [{
"table1": ["xxx","yyy"]
}, {
"table2": ["xxx","yyy"]
}, {
"table3": ["xxx","yyy"]
}]
},
"Build sets": "yyy",
"destinationPath": "xxx",
"status": 1
}
这是我用来动态创建表格的函数。
function generateTable(data){ //data is the parsed JSON Object from an ajax request
$("#test-table tbody").empty();//Empty the table first
if(data.result != null){
$.each(data.result,function(key,value){
var buildName ="<tr><td rowspan='"+value.length+"'>"+key+"<span class='cyan darken-1 white-text badge'>"+value.length+" base tables</span></td>";
var baseTable ="";
for(i=0;i<value.length;i++){
if( i == 0 ){
for(var k in value[0]){
baseTable ="<td rowspan='"+value[0][k].length+"'>"+k+"</td></tr>";
}
}
else{
for(var key in value[i]){
baseTable = baseTable + "<tr><td rowspan='"+value[i][key].length+"'>"+key+"</td></tr>";
}
}
}
$("#test-table").append(buildName + baseTable);
});
}
}
这是我想要实现的目标
HTML
<table id="test-table" class="bordered responsive-table">
<thead>
<tr>
<th>Build Name</th><th>Base Table</th><th>Query List</th>
</tr>
</thead>
</table>
问题:
我成功创建了前两列(尽管有点难看,以为我可以稍后改进它),我被困在第三列。我发布的代码正确地创建了前两列,但是在rowspan(第三列)中的rowspan逻辑似乎无法实现。请指导我。
答案 0 :(得分:4)
老实说我以前从未使用过rowspan
,但在阅读this stack answer之后我明白了 - 我强烈建议你这样做。在那之后,只需要确定从JSON到DOM的元素的顺序。
这是一个工作演示:
var data = '{"result":{"buildname1":[{"table1":["xxx","yyy", "zzz"]},{"table2":["xxx","yyy"]}],"buildname2":[{"table1":["xxx","yyy", "zzz"]},{"table2":["xxx","yyy"]},{"table3":["xxx","yyy"]}]},"Build sets":"yyy","destinationPath":"xxx","status":1}';
function generateTable(data) { //data is the parsed JSON Object from an ajax request
data = JSON.parse(data);
$("#test-table tbody").empty(); //Empty the table first
$.each(data.result, function(key, elem) {
var baseHtml = "";
var childrenHtml = "";
var maxRowSpan = 0;
$.each(elem, function(index, inner_elem) {
var innerElemKey = Object.keys(inner_elem)[0];
var arr = inner_elem[innerElemKey];
var elemRowSpan = arr.length;
maxRowSpan += arr.length;
if (index === 0) {
childrenHtml += ('<td rowspan="' + elemRowSpan + '">' + innerElemKey + '</td>');
$.each(arr, function(indx, child) {
if (indx === 0) {
childrenHtml += ('<td rowspan="1">' + child + '</td>' + '</tr>');
} else {
childrenHtml += ('<tr><td rowspan="1">' + child + '</td>' + '</tr>');
}
});
} else {
childrenHtml += ('<tr><td rowspan="' + elemRowSpan + '">' + innerElemKey + '</td>');
$.each(arr, function(indx, child) {
if (indx === 0) {
childrenHtml += ('<td rowspan="1">' + child + '</td>' + '</tr>');
} else {
childrenHtml += ('<tr><td rowspan="1">' + child + '</td>' + '</tr>');
}
});
}
});
baseHtml += ('<tr><td rowspan="' + maxRowSpan + '">' + key + '</td>');
$("#test-table").append(baseHtml + childrenHtml);
});
}
$(function() {
generateTable(data);
});
td {
border: 1px solid black;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="test-table" class="bordered responsive-table">
<thead>
<tr>
<th>Build Name</th>
<th>Base Table</th>
<th>Query List</th>
</tr>
</thead>
</table>
<table id="test-table" class="bordered responsive-table">
<thead>
<tr>
<th>Build Name</th><th>Base Table</th><th>Query List</th>
</tr>
</thead>
</table>
<table id="test-table" class="bordered responsive-table">
<thead>
<tr>
<th>Build Name</th>
<th>Base Table</th>
<th>Query List</th>
</tr>
</thead>
<tbody>
<tr>
<td rowspan="5">buildname1</td>
<td rowspan="3">table1</td>
<td rowspan="1">xxx</td>
</tr>
<tr>
<td rowspan="1">yyy</td>
</tr>
<tr>
<td rowspan="1">zzz</td>
</tr>
<tr>
<td rowspan="2">table2</td>
<td rowspan="1">xxx</td>
</tr>
<tr>
<td rowspan="1">yyy</td>
</tr>
<tr>
<td rowspan="7">buildname2</td>
<td rowspan="3">table1</td>
<td rowspan="1">xxx</td>
</tr>
<tr>
<td rowspan="1">yyy</td>
</tr>
<tr>
<td rowspan="1">zzz</td>
</tr>
<tr>
<td rowspan="2">table2</td>
<td rowspan="1">xxx</td>
</tr>
<tr>
<td rowspan="1">yyy</td>
</tr>
<tr>
<td rowspan="2">table3</td>
<td rowspan="1">xxx</td>
</tr>
<tr>
<td rowspan="1">yyy</td>
</tr>
</tbody>
</table>