javascript中如何使用json生成html表格?相信很多新手小白還沒(méi)學(xué)會(huì)這個(gè)技能,通過(guò)這篇文章的總結(jié),希望你能學(xué)會(huì)學(xué)會(huì)這個(gè)技能。以下資料是實(shí)現(xiàn)的步驟。

之前公司有一個(gè)需求是:通過(guò)js來(lái)生成html。而且大部分都是生成表格,直接通過(guò)字符串拼接的話,代碼的可復(fù)用性太低的,所以寫(xiě)了個(gè)通用的json轉(zhuǎn)html表格的工具。
代碼
htmlKit = {
_tags: [], html: [],
_createAttrs: function (attrs) {
var attrStr = [];
for (var key in attrs) {
if (!attrs.hasOwnProperty(key)) continue;
attrStr.push(key + "=" + attrs[key] + "")
}
return attrStr.join(" ")
},
_createTag: function (tag, attrs, isStart) {
if (isStart) {
return "<" + tag + " " + this._createAttrs(attrs) + ">"
} else {
return "</" + tag + ">"
}
},
start: function (tag, attrs) {
this._tags.push(tag);
this.html.push(this._createTag(tag, attrs, true))
},
end: function () {
this.html.push(this._createTag(this._tags.pop(), null, false))
},
tag: function (tag, attr, text) {
this.html.push(this._createTag(tag, attr, true) + text + this._createTag(tag, null, false))
},
create: function () {
return this.html.join("")
}
};
function json2Html(data) {
var hk = htmlKit;
hk.start("table", {"cellpadding": "10", "border": "1"});
hk.start("thead");
hk.start("tr");
data["heads"].forEach(function (head) {
hk.tag("th", {"bgcolor": "AntiqueWhite"}, head)
});
hk.end();
hk.end();
hk.start("tbody");
data["data"].forEach(function (dataList, i) {
dataList.forEach(function (_data) {
hk.start("tr");
data["dataKeys"][i].forEach(function (key) {
var rowsAndCol = key.split(":");
if (rowsAndCol.length === 1) {
hk.tag("td", null, _data[rowsAndCol[0]])
} else if (rowsAndCol.length === 3) {
hk.tag("td", {"rowspan": rowsAndCol[0], "colspan": rowsAndCol[1]}, _data[rowsAndCol[2]])
}
});
hk.end()
})
});
hk.end();
hk.end();
return hk.create()
}使用說(shuō)明
HtmlKit
htmlKit是創(chuàng)建html標(biāo)簽的工具
| 函數(shù)名 | 作用 | 例子 |
|---|---|---|
| start (tag, attrs) | 創(chuàng)建未封閉標(biāo)簽頭 | start("table", {"cellpadding": "10", "border": "1"}),輸出<table cellpadding="10" border="1"> |
| end () | 創(chuàng)建上一個(gè)start函數(shù)的標(biāo)簽尾 | 上面執(zhí)行了start("table"),再執(zhí)行end(),輸出</table> |
| tag (tag, attr, text) | 創(chuàng)建封閉標(biāo)簽 | tag("th", {"bgcolor": "AntiqueWhite"}, "hello"),輸出<th bgcolor="AntiqueWhite">hello</th> |
json2Html
json轉(zhuǎn)Html
例子:
var data = [
{
"chinese": 80,
"mathematics": 89,
"english": 90
}
];
var total = 0;
data.forEach(function (value) {
for (key in value) {
total += value[key];
}
});
var htmlMetadata = {
"heads": ["語(yǔ)文", "數(shù)學(xué)", "英語(yǔ)"],
"dataKeys": [["chinese", "mathematics", "english"], ["text","1:2:total"]], // rowspan:colspan:value
"data": [data, [{"text": "合計(jì)","total": total}]]
};
var html = json2Html(htmlMetadata);
console.info(html);輸出結(jié)果(結(jié)果為了好看,格式化了):
<table cellpadding=10 border=1>
<thead>
<tr>
<th bgcolor=AntiqueWhite>語(yǔ)文</th>
<th bgcolor=AntiqueWhite>數(shù)學(xué)</th>
<th bgcolor=AntiqueWhite>英語(yǔ)</th>
</tr>
</thead>
<tbody>
<tr>
<td>80</td>
<td>89</td>
<td>90</td>
</tr>
<tr>
<td>合計(jì)</td>
<td rowspan=1 colspan=2>259</td>
</tr>
</tbody>
</table>效果:
| 語(yǔ)文 | 數(shù)學(xué) | 英語(yǔ) |
|---|---|---|
| 80 | 89 | 90 |
| 合計(jì) | 259 | |
看完這篇文章,你們學(xué)會(huì)javascript中使用json生成html表格的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀。
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
分享名稱:javascript中如何使用json生成html表格-創(chuàng)新互聯(lián)
本文地址:http://www.chinadenli.net/article40/cccgho.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供搜索引擎優(yōu)化、定制網(wǎng)站、網(wǎng)站設(shè)計(jì)、企業(yè)建站、ChatGPT、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容