簡體   English   中英

如何用json對象內容填充html表

[英]How to populate html table with json object contents

我在ajax調用中返回一個列表,成功函數寫在下面。 我的json的內容類似{“ 1”,“ ayesha”,“ cs”,“ Lahore”}。

 function ShowData(data) {
         var data = data.d;

        var html = "";
        if (data.length > 0) {
            JObject = data;
            console.log(JObject);
            html = "<table id='tbl' class='TableGrid'  width='100%' >";
            html += "<thead><tr><th>StdId</th><th>Name</th><th>feild</th><th>City</th></thead><tbody> ";
            for (var i = 0; i < JObject.length; i++) {
                html += '<tr>';
                html += ' <td align="left" >' + JObject[i] + '</td>';
                html += '</tr>';
            }
            html += "</tbody> </table>";

        }

我應該如何顯示內容?

使用波紋管腳本可以幫助創建表

  1. var table = document.createElement(“ table”);
  2. var thead = document.createElement(“ thead”);
  3. var tr = document.createElement(“ tr”);
  4. var th = document.createElement(“ th”);
  5. var td = document.createElement(“ td”);
  6. table.appendChild(線程).appendChild(TH);
  7. table.appendChild(TR).appendChild(TD)

使用.innerHTML將表格插入模板中。

DOCS: https//www.w3schools.com/jsref/prop_html_innerhtml.asp

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM