Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
303 views
in Technique[技术] by (71.8m points)

How to covert json value in to html table

I want json code and also how to convert that json data to html table. Importantly how to covert the json values as a th(header) in html. enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have a look at https://jsfiddle.net/nt56wuox/3/ for a basic example in pure javascript and 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;
}

You might want to consider reading about HTML, JS and JSON and perhaps one of the common frameworks such as Angular / React


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...