[英]Dynamic HTML Table row creation using JSON
Question: Looking for Jquery or javascript solution to create dynamic table rows with colspan using below JSON structure. 问题:正在寻找Jquery或javascript解决方案,使用以下JSON结构使用colspan创建动态表行。 I am facing issue to create the required rows.
我面临创建所需行的问题。 I have tried many scenarios but cannot locate the TR to append the TH.
我已经尝试了许多方案,但是找不到TR来附加TH。
your help is much appreciated. 非常感谢您的帮助。
Here is my JSON format 这是我的JSON格式
[{"data":"test","nodes":[{"data":"5.00%","nodes":[{"data":"5.00%","nodes":[{"data":"Jul 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Aug 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Sep 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Oct 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Nov 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Dec 2014","nodes":[{"data":"Result","nodes":null}]}]},{"data":"70.00%","nodes":[{"data":"Jul 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Aug 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Sep 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Oct 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Nov 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Dec 2014","nodes":[{"data":"Result","nodes":null}]}]},{"data":"80.00%","nodes":[{"data":"Jul 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Aug 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Sep 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Oct 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Nov 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Dec 2014","nodes":[{"data":"Result","nodes":null}]}]}]},{"data":"5.00%","nodes":[{"data":"5.00%","nodes":[{"data":"Jul 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Aug 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Sep 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Oct 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Nov 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Dec 2014","nodes":[{"data":"Result","nodes":null}]}]},{"data":"70.00%","nodes":[{"data":"Jul 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Aug 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Sep 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Oct 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Nov 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Dec 2014","nodes":[{"data":"Result","nodes":null}]}]},{"data":"80.00%","nodes":[{"data":"Jul 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Aug 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Sep 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Oct 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Nov 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Dec 2014","nodes":[{"data":"Result","nodes":null}]}]}]}]}]
Here is what i am trying to achieve 这是我想要达到的目标
enter image description here 在此处输入图片说明
HTML structure HTML结构
<table>
<tr>
<th colspan="36">test</th>
</tr>
<tr>
<td colspan="18">5.00%</td>
<td colspan="18">5.00%</td>
</tr>
<tr>
<td colspan="6">5.00%</td>
<td colspan="6">70.00%</td>
<td colspan="6">80.00%</td>
<td colspan="6">5.00%</td>
<td colspan="6">70.00%</td>
<td colspan="6">80.00%</td>
</tr>
<tr>
<td colspan="1">Jul 2014</td>
<td colspan="1">Aug 2014</td>
<td colspan="1">Sep 2014</td>
<td colspan="1">Oct 2014</td>
<td colspan="1">Nov 2014</td>
<td colspan="1">Dec 2014</td>
<td colspan="1">Jul 2014</td>
<td colspan="1">Aug 2014</td>
<td colspan="1">Sep 2014</td>
<td colspan="1">Oct 2014</td>
<td colspan="1">Nov 2014</td>
<td colspan="1">Dec 2014</td>
<td colspan="1">Jul 2014</td>
<td colspan="1">Aug 2014</td>
<td colspan="1">Sep 2014</td>
<td colspan="1">Oct 2014</td>
<td colspan="1">Nov 2014</td>
<td colspan="1">Dec 2014</td>
<td colspan="1">Jul 2014</td>
<td colspan="1">Aug 2014</td>
<td colspan="1">Sep 2014</td>
<td colspan="1">Oct 2014</td>
<td colspan="1">Nov 2014</td>
<td colspan="1">Dec 2014</td>
<td colspan="1">Jul 2014</td>
<td colspan="1">Aug 2014</td>
<td colspan="1">Sep 2014</td>
<td colspan="1">Oct 2014</td>
<td colspan="1">Nov 2014</td>
<td colspan="1">Dec 2014</td>
<td colspan="1">Jul 2014</td>
<td colspan="1">Aug 2014</td>
<td colspan="1">Sep 2014</td>
<td colspan="1">Oct 2014</td>
<td colspan="1">Nov 2014</td>
<td colspan="1">Dec 2014</td>
</tr>
</table>
I don't intend to do the assignment for you but hopefully this JSFiddle will help you with an idea of how you can go about doing this. 我无意为您完成任务,但希望这个JSFiddle可以帮助您了解如何进行此操作。 I've also included a very simple way to sort the columns based on the header that is clicked.
我还提供了一种非常简单的方法,可根据所单击的标题对列进行排序。 Programmatically adding the colspans shouldn't be too difficult after you understand what I've done here to render this table.
在理解了我在此处呈现此表所做的工作之后,以编程方式添加colspans并不难。 Take a look at document.getElementById().setAttribute(attributeName, value) for a way to do that.
看一下document.getElementById()。setAttribute(attributeName,value)来做到这一点。
https://www.w3schools.com/jsref/met_element_setattribute.asp https://www.w3schools.com/jsref/met_element_setattribute.asp
Let me know if you have any questions :) 有任何问题请告诉我:)
https://jsfiddle.net/cxb7h2ze/7/ https://jsfiddle.net/cxb7h2ze/7/
HTML 的HTML
<table>
<thead>
<tr>
<th onclick="column(0)">
Column 1
</th>
<th onclick="column(1)">
Column 2
</th>
</tr>
</thead>
<tbody id="table">
</tbody>
</table>
<button onclick="renderArray()">
Render Array
</button>
</button>
JS JS
array = [
{column1: "Alphabetical name",
column2: 2},
{column1: "Couch Potato",
column2: "3"},
{column1: "Blah Blah",
column2: 1},
];
var toggle = [0,0]
window.renderArray = function() {
var x;
var tr;
var td;
var text;
var button;
var table = document.getElementById("table");
// reset table so it doesn't append a duplicate set
table.innerHTML = "";
for (x = 0; x < array.length; x++) {
// create a row
tr = document.createElement("tr");
tr.setAttribute("ondblclick", "alert('hello there this is row')");
// create a cell
td = document.createElement("td");
// create text to go in cell
text = document.createTextNode(array[x].column1);
// append text to cell
td.appendChild(text);
//append cell to row
tr.appendChild(td);
// create a second cell
td = document.createElement("td");
// create a new text to go in cell
text = document.createTextNode(array[x].column2);
// append text to cell
td.appendChild(text);
// append cell to row
tr.appendChild(td);
// create cell
td = document.createElement("td");
// add row to column
table.appendChild(tr);
}
}
window.column = function(id) {
toggle[id]++;
if (id == 0) {
// first column
array.sort(function(a, b) {
if (toggle[id] % 2 == 0) {
return a.column1.toLowerCase() < b.column1.toLowerCase();
}
else {
return a.column1.toLowerCase() > b.column1.toLowerCase();
}
})
}
else {
array.sort(function(a, b) {
if (toggle[id] % 2 == 0) {
return a.column2 < b.column2;
}
else {
return a.column2 > b.column2;
}
})
}
renderArray();
}
You could use recursion for this. 您可以为此使用递归。 This version will also determine the
colspan
values dynamically, based on what is in the input. 此版本还将根据输入内容动态确定
colspan
值。 They will only be added when more than 1. Note that a colspan
of zero makes no sense. 仅当大于1时才添加它们。请注意,
colspan
为零是没有意义的。
function objectToTable(data) { var $table = $("<table>"), $tableRows = $("tr", $table); function recurse(data, depth) { if (!data) return 1; if (depth >= $tableRows.length) { $table.append($("<tr>")); $tableRows = $("tr", $table); } var totalSpan = 0; $tableRows.eq(depth).append(data.map(function(column) { var colspan = recurse(column.nodes, depth+1); // Maybe you want the deepest values to be wrapped in normal TD tags: var $th = $(column.nodes ? "<th>" : "<td>").text(column.data); if (colspan > 1) $th.attr("colspan", colspan); totalSpan += colspan; return $th; })); return totalSpan; } recurse(data, 0); return $table; } var data = [{"data":"5.00%","nodes":[{"data":"Jul 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Aug 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Sep 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Oct 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Nov 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Dec 2014","nodes":[{"data":"Result","nodes":null}]}]},{"data":"70.00%","nodes":[{"data":"Jul 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Aug 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Sep 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Oct 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Nov 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Dec 2014","nodes":[{"data":"Result","nodes":null}]}]},{"data":"80.00%","nodes":[{"data":"Jul 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Aug 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Sep 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Oct 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Nov 2014","nodes":[{"data":"Result","nodes":null}]},{"data":"Dec 2014","nodes":[{"data":"Result","nodes":null}]}]}]; $("body").append(objectToTable(data));
table { border-collapse: collapse; } th, td { border: 1px solid; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.