gpt4 book ai didi

javascript - json2html - 创建 HTML 表格

转载 作者:行者123 更新时间:2023-11-29 21:57:52 36 4
gpt4 key购买 nike

我试图找到好的 JS 插件来帮助我生成 HTML <table />结构并用 JSON 数据填充它。

我找到了 json2html ,但不幸的是,我没有在插件的官方网站上看到任何有用的示例或文档,也没有看到任何我可以查看的第 3 方网站。

我创建了一个 function()这将为我生成一些随机数据,我将在这个演示示例中使用这些数据。在我将用于 json2html 的变量中存储 JSON 模板并创建一个 <div />带有一个 ID,该 ID 将用于使用此插件解析 JSON 生成的数据。

// Demo function to generate random content
// ->
function generateChartData() {
for (var i = 0; i < 5; i++) {
var file_id = Math.floor(Math.random() * 90000) + 10000,
file_name = 'Dummy File ' + Math.floor(Math.random() * 20) + 30 + '.zip',
file_clicks = Math.round(Math.random() * 480) + 820,
file_downloads = Math.round(Math.random() * 160) + 420,
file_conversion = (file_clicks / file_downloads).toFixed(2),
file_profit = Math.round(Math.random() * 120) + 310,
file_cpa = (file_profit / file_downloads).toFixed(2),
file_epc = (file_profit / file_clicks).toFixed(2);

chartData.push({
file_id: file_id,
file_name: file_name,
file_clicks: file_clicks,
file_downloads: file_downloads,
file_conversion: file_conversion,
file_cpa: file_cpa,
file_epc: file_epc,
file_profit: file_profit
});
}

// create variables but no values
var total_clicks, total_downloads, total_conversion, total_profit, total_cpa, total_epc;
}
// <-

// Create html template for further json parsing
var template = {
"tag": "table",
"class": "table table-striped table-hover",
"children": [
{
"tag": "thead",
"id": "json-head",
"children": [
{
"tag": "tr",
"children": [
{
"tag": "th",
"html": "ID"
},
{
"tag": "th",
"html": "File Name"
},
{
"tag": "th",
"html": "Clicks"
},
{
"tag": "th",
"html": "Downloads"
},
{
"tag": "th",
"html": "Conversion"
},
{
"tag": "th",
"html": "Average CPA"
},
{
"tag": "th",
"html": "EPC"
},
{
"tag": "th",
"html": "Profit"
}
]
}
]
},
{
"tag": "tbody",
"id": "json-body",
"children": [
{
"tag": "tr",
"children": [
{
"tag": "td",
"html": "${file_id}"
},
{
"tag": "td",
"html": "${file_name}"
},
{
"tag": "td",
"html": "${file_clicks}"
},
{
"tag": "td",
"html": "${file_downloads}"
},
{
"tag": "td",
"html": "${file_conversion}"
},
{
"tag": "td",
"html": "${file_cpa}"
},
{
"tag": "td",
"html": "${file_epc}"
},
{
"tag": "td",
"html": "${file_profit}"
}
]
}
]
},
{
"tag": "tfoot",
"id": "json-foot",
"children": [
{
"tag": "tr",
"children": [
{
"tag": "td",
"colspan": "2",
"html": "Total / Average"
},
{
"tag": "td",
"html": "${total_clicks}"
},
{
"tag": "td",
"html": "${total_downloads}"
},
{
"tag": "td",
"html": "${total_conversion}"
},
{
"tag": "td",
"html": "${total_cpa}"
},
{
"tag": "td",
"html": "${total_epc}"
},
{
"tag": "td",
"html": "${total_profit}"
}
]
}
]
}
]
};

// Empty array for json data
var chartData = [];

// Calling DEMO function to generate json data
generateChartData();

// Parse json data and generate html
$("#json-parse").json2html(chartData, template);

但是,您可能会注意到我的 JSON 模板或 json2html 调用有问题。它创建无效表并为每个 JSON 数据解析生成表。很遗憾,这不是我想要的。

我的想法是创建以下模板并将所需信息解析为 <tbody /> & <tfoot />但不要重新创建它们。

<table class="table table-striped table-hover">
<thead id="json-head">
<tr>
<th>ID</th>
<th>File Name</th>
<th>Clicks</th>
<th>Downloads</th>
<th>Conversion</th>
<th>Average CPA</th>
<th>EPC</th>
<th>Profit</th>
</tr>
</thead>

<tbody id="json-body">
<tr>
<td>${file_id}</td>
<td>${file_name}</td>
<td>${file_clicks}</td>
<td>${file_downloads}</td>
<td>${file_conversion}</td>
<td>${file_cpa}</td>
<td>${file_epc}</td>
<td>${file_profit}</td>
</tr>
</tbody>

<tfoot id="json-foot">
<tr>
<td colspan="2">Total / Average</td>
<td>${total_clicks}</td>
<td>${total_downloads}</td>
<td>${total_conversion}</td>
<td>${total_cpa}</td>
<td>${total_epc}</td>
<td>${total_profit}</td>
</tr>
</tfoot>

不幸的是,我无法提供 JSFiddle,因为我找不到它的任何 CDN。但这里的屏幕截图将向您展示不良结果的样子。

json2html - Bad outcome

我知道这是我的 JSON 结构或我没有正确设置的插件调用,但如果你不介意在这里帮助我。这确实令人头疼,但也可能对其他用户有所帮助。

最佳答案

json2Html 正在一个循环内生成您的模板,其计数器等于您的 json 对象计数。所以你看到的结果没有错。如果您不想重复页眉和页脚,只需将它们用作另一个带有对象数组的模板即可。
更新:感谢@Volune example .

// Create html template for further json parsing
var headertemplate = {
"tag": "thead",
"id": "json-head",
"children": [
{
"tag": "tr",
"children": [
{
"tag": "th",
"html": "ID"
},
{
"tag": "th",
"html": "File Name"
},
{
"tag": "th",
"html": "Clicks"
},
{
"tag": "th",
"html": "Downloads"
},
{
"tag": "th",
"html": "Conversion"
},
{
"tag": "th",
"html": "Average CPA"
},
{
"tag": "th",
"html": "EPC"
},
{
"tag": "th",
"html": "Profit"
}
]
}
]
}
var footertemplate = {
"tag": "tfoot",
"id": "json-foot",
"children": [
{
"tag": "tr",
"children": [
{
"tag": "td",
"colspan": "2",
"html": "Total / Average"
},
{
"tag": "td",
"html": "${total_clicks}"
},
{
"tag": "td",
"html": "${total_downloads}"
},
{
"tag": "td",
"html": "${total_conversion}"
},
{
"tag": "td",
"html": "${total_cpa}"
},
{
"tag": "td",
"html": "${total_epc}"
},
{
"tag": "td",
"html": "${total_profit}"
}
]
}
]
}
var template = {
"tag": "table",
"class": "table table-striped table-hover",
"children": [
{
"tag": "tbody",
"id": "json-body",
"children": [
{
"tag": "tr",
"children": [
{
"tag": "td",
"html": "${file_id}"
},
{
"tag": "td",
"html": "${file_name}"
},
{
"tag": "td",
"html": "${file_clicks}"
},
{
"tag": "td",
"html": "${file_downloads}"
},
{
"tag": "td",
"html": "${file_conversion}"
},
{
"tag": "td",
"html": "${file_cpa}"
},
{
"tag": "td",
"html": "${file_epc}"
},
{
"tag": "td",
"html": "${file_profit}"
}
]
}
]
}
]
};

// Empty array for json data
var chartData = [];

// Calling DEMO function to generate json data
generateChartData();
// Parse json data and generate html
$("#json-parse").json2html(chartData[0], headertemplate);
$("#json-parse").json2html(chartData, template);
$("#json-parse").json2html(chartData[0], footertemplate); //use your total json object with one array inside, instead chartData[0];

关于javascript - json2html - 创建 HTML 表格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25431505/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com