国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

JavaScript根據json生成html表格的示例代碼

2019-11-19 12:38:27
字體:
來源:轉載
供稿:網友

之前公司有一個需求是:通過js來生成html。而且大部分都是生成表格,直接通過字符串拼接的話,代碼的可復用性太低的,所以寫了個通用的json轉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是創建html標簽的工具

函數

函數名 作用 例子
start (tag, attrs) 創建未封閉標簽頭 start("table", {"cellpadding": "10", "border": "1"}),輸出<table cellpadding="10" border="1">
end () 創建上一個start函數的標簽尾 上面執行了start("table"),再執行end(),輸出</table>
tag (tag, attr, text) 創建封閉標簽 tag("th", {"bgcolor": "AntiqueWhite"}, "hello"),輸出<th bgcolor="AntiqueWhite">hello</th>

json2Html

json轉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": ["語文", "數學", "英語"],  "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>數學</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>

效果:

語文 數學 英語
80 89 90
合計 259

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 佛冈县| 嘉鱼县| 安阳县| 南丰县| 额敏县| 伊春市| 竹北市| 岐山县| 遂宁市| 滨州市| 东方市| 敖汉旗| 西乌珠穆沁旗| 如东县| 南康市| 根河市| 保靖县| 正镶白旗| 兴宁市| 江达县| 贵阳市| 建德市| 泗水县| 高陵县| 镇远县| 桦川县| 顺昌县| 贵阳市| 抚州市| 广平县| 历史| 蓝田县| 乌拉特中旗| 旅游| 济宁市| 资源县| 霸州市| 观塘区| 太原市| 定州市| 两当县|