JavaScript根據(jù)json生成html表格的示例代碼
更新時間:2018年10月24日 09:37:05 作者:DH鑌
這篇文章主要介紹了JavaScript根據(jù)json生成html表格的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
之前公司有一個需求是:通過js來生成html。而且大部分都是生成表格,直接通過字符串拼接的話,代碼的可復用性太低的,所以寫了個通用的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標簽的工具
函數(shù)
函數(shù)名 | 作用 | 例子 |
---|---|---|
start (tag, attrs) | 創(chuàng)建未封閉標簽頭 | start("table", {"cellpadding": "10", "border": "1"}),輸出<table cellpadding="10" border="1"> |
end () | 創(chuàng)建上一個start函數(shù)的標簽尾 | 上面執(zhí)行了start("table"),再執(zhí)行end(),輸出</table> |
tag (tag, attr, text) | 創(chuàng)建封閉標簽 | 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": ["語文", "數(shù)學", "英語"], "dataKeys": [["chinese", "mathematics", "english"], ["text","1:2:total"]], // rowspan:colspan:value "data": [data, [{"text": "合計","total": total}]] }; var html = json2Html(htmlMetadata); console.info(html); 輸出結果(結果為了好看,格式化了): <table cellpadding=10 border=1> <thead> <tr> <th bgcolor=AntiqueWhite>語文</th> <th bgcolor=AntiqueWhite>數(shù)學</th> <th bgcolor=AntiqueWhite>英語</th> </tr> </thead> <tbody> <tr> <td>80</td> <td>89</td> <td>90</td> </tr> <tr> <td>合計</td> <td rowspan=1 colspan=2>259</td> </tr> </tbody> </table>
效果:
語文 | 數(shù)學 | 英語 |
---|---|---|
80 | 89 | 90 |
合計 | 259 |
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
JavaScript中的淺拷貝和深拷貝原理與實現(xiàn)淺析
這篇文章主要介紹了JavaScript中的淺拷貝和深拷貝原理與實現(xiàn),JavaScript 中的淺拷貝和深拷貝指的是在復制對象(包括對象、數(shù)組等)時,是否只復制對象的引用地址或者在復制時創(chuàng)建一個新的對象2023-04-04js動態(tài)添加刪除,后臺取數(shù)據(jù)(示例代碼)
這篇文章主要是對js動態(tài)添加刪除,后臺取數(shù)據(jù)(示例代碼)進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11