html - 如何将json值转换为html表
解决方案
查看https://jsfiddle.net/nt56wuox/3/以获取纯 javascript 和 HTML 的基本示例。
html
<html>
<body>
<input id='inAttr' type='text' placeholder='Enter attr' >
<button onClick='calculate(document.getElementById("inAttr").value)' > Show table </button>
<div id='result'> </div>
</body>
</html>
Javascript
data = [
{code:'101' , attr:'Body' , attr_value: 'SS04'},
{code:'102' , attr:'Body' , attr_value: 'SS04'},
{code:'103' , attr:'Float' , attr_value: 'SS0X'},
{code:'104' , attr:'Spring' , attr_value: 'SS0X'},
]
function calculate(attr){
const tableData = data
.filter(row=>row.attr === attr)
console.log({attr, tableData});
var html = '<table>';
html+=`<tr><th>Code</th><th>${attr}</th></tr>`
tableData.forEach(row=>{
html+=`<tr><td>${row.code}</td><td>${row.attr_value}</td></tr>`
})
html += '<table>';
document.getElementById('result').innerHTML = html;
return tableData;
}
您可能需要考虑阅读有关 HTML、JS 和 JSON 以及可能的常见框架之一,例如 Angular / React
推荐阅读
- excel - 使用 VBA 间接引用其他工作表中的范围
- dynamics-crm - Dynamics 365 - 从全局选项集中获取选定选项的值
- sql - SQL 从另一个表中向下日期值
- css - CSS - 固定按钮展开
- c - C文件处理-加载函数不断从文件中获取相同的数据
- typescript - tsc:1:达到最大嵌套函数级别;增加 FUNCNEST?
- scala - Scala Spark/Databricks:.cache() 不会阻止重新计算
- php - 如何使用 PHP 提取 JSON 数据
- sql - SQL Server 插入行:为每个“组”插入一行
- valgrind - Massif 可视化工具无法在图例中显示文本