JavaScript根據(jù)json生成html表格的示例代碼
之前公司有一個(gè)需求是:通過js來生成html。而且大部分都是生成表格,直接通過字符串拼接的話,代碼的可復(fù)用性太低的,所以寫了個(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()
}
使用說明
HtmlKit
htmlKit是創(chuàng)建html標(biāo)簽的工具
函數(shù)
| 函數(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 | |
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
深入了解JavaScript中的二進(jìn)制操作及位掩碼應(yīng)用
在JavaScript中,二進(jìn)制操作可以說是一項(xiàng)非常強(qiáng)大和有用的技能,尤其是在處理數(shù)據(jù)和位掩碼時(shí),它們是不可或缺的,本文將介紹JavaScript中的二進(jìn)制操作,包括什么是二進(jìn)制以及如何在JavaScript中進(jìn)行二進(jìn)制操作2023-06-06
JavaScript中的淺拷貝和深拷貝原理與實(shí)現(xiàn)淺析
這篇文章主要介紹了JavaScript中的淺拷貝和深拷貝原理與實(shí)現(xiàn),JavaScript 中的淺拷貝和深拷貝指的是在復(fù)制對(duì)象(包括對(duì)象、數(shù)組等)時(shí),是否只復(fù)制對(duì)象的引用地址或者在復(fù)制時(shí)創(chuàng)建一個(gè)新的對(duì)象2023-04-04
js實(shí)現(xiàn)拖動(dòng)模態(tài)框
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)拖動(dòng)模態(tài)框,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07
實(shí)現(xiàn)點(diǎn)擊列表彈出列表索引的兩種方式
使用利用事件冒泡委托給列表的父節(jié)點(diǎn)去處理的方式第二種方式就是使用閉包了,感興趣的你可以參考下本文,或許對(duì)你學(xué)習(xí)js有所幫助2013-03-03
js動(dòng)態(tài)添加刪除,后臺(tái)取數(shù)據(jù)(示例代碼)
這篇文章主要是對(duì)js動(dòng)態(tài)添加刪除,后臺(tái)取數(shù)據(jù)(示例代碼)進(jìn)行了詳細(xì)的分析介紹,需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-11-11

