最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JavaScript根據(jù)json生成html表格的示例代碼

 更新時(shí)間:2018年10月24日 09:37:05   作者:DH鑌  
這篇文章主要介紹了JavaScript根據(jù)json生成html表格的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

之前公司有一個(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": ["語文", "數(shù)學(xué)", "英語"],
  "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>語文</th>
    <th bgcolor=AntiqueWhite>數(shù)學(xué)</th>
    <th bgcolor=AntiqueWhite>英語</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>

效果:

語文 數(shù)學(xué) 英語
80 89 90
合計(jì) 259

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

五寨县| 英山县| 平顺县| 彰化县| 遂溪县| 海原县| 时尚| 襄垣县| 太谷县| 青岛市| 虎林市| 瑞昌市| 成安县| 古蔺县| 同江市| 丰宁| 新晃| 十堰市| 连州市| 武平县| 思茅市| 偏关县| 当阳市| 密云县| 太湖县| 玉龙| 无锡市| 石屏县| 大港区| 廉江市| 德钦县| 扶风县| 罗城| 阳江市| 明水县| 莱州市| 广元市| 乌恰县| 通城县| 宁津县| 错那县|