将json对象转换为htmlTable typescript

时间:2016-04-14 09:35:35

标签: typescript html-table jsonobject

我想将我的json对象转换为表格,在html页面中显示。我使用typescript来做到这一点。我有关于这个主题的任何信息,我刚刚在javascript中找到了一个解决方案:http://www.kodingmadesimple.com/2015/07/convert-json-data-html-table-jquery-datatables-plugin.html 它是一个例子还是一个解决方案? 谢谢你提前

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试学习如何使用打字稿进行编码?如果是这样,这可能会让你开始:

let input = {
  "data": [
    {
      "name": "Garrett Winters",
      "designation": "Accountant",
      "salary": "$170,750",
      "joining_date": "2011/07/25",
      "office": "Tokyo",
      "extension": "8422"
    }
 ]
}

function convert(data: typeof input) {
    let rows = data.data.map(d => `<tr><td>${d.name}</td></tr>`);
    return `<table>${rows}</table>`;
}

console.log(convert(input));