猿问

创建动态 JSON 表 HTML JS

我的示例 JSON 数据类似于:数据字段不是所有行的同一组字段。


[{"date":"2020-04-05 18:26:01", "mydata":{"city":{"name":"paris"},"travel":{"frequency":"5","climate":"cold"}}},

{"date":"2020-04-05 18:26:17", "mydata":{"city":{"name":"amsterdam"},"fly":{"airports":"1","type":"international"}}}]

这被保存到我的 MariaDB 的一列中。


我想以表格格式检索它,例如:


Date                 mydata-city-name   travel-frequency  travel-climate   fly-airports   fly-type 

2020-04-05 18:26:01  paris              5                 cold             null/blank     null/blank

2020-04-05 18:26:17  amsterdam          null/blank        null/blank       1              international

到目前为止,我尝试了以下 js 和 html 但我无法按预期获取数据:


JavaScript:


var myList

$.ajax({

  type: "GET",

  url: "symptom_list.php",

  success: function (data) {

    console.log(data)

    myList = JSON.parse(data)

    console.log(JSON.parse(

      data)

    );

    /* alert(data); */ // show response from the php script.

  },

});


function buildHtmlTable(selector) {

  var columns = addAllColumnHeaders(myList, selector);


  for (var i = 0; i < myList.length; i++) {

    var row$ = $('<tr/>');

    for (var colIndex = 0; colIndex < columns.length; colIndex++) {

      var cellValue = myList[i][columns[colIndex]];

      if (cellValue == null) cellValue = "";

      row$.append($('<td/>').html(cellValue));

    }

    $(selector).append(row$);

  }

}


// Adds a header row to the table and returns the set of columns.

// Need to do union of keys from all records as some records may not contain

// all records.

function addAllColumnHeaders(myList, selector) {

  var columnSet = [];

  var headerTr$ = $('<tr/>');


  for (var i = 0; i < myList.length; i++) {

    var rowHash = myList[i];

    for (var key in rowHash) {

      if ($.inArray(key, columnSet) == -1) {

        columnSet.push(key);

        headerTr$.append($('<th/>').html(key));

      }

    }

  }

  $(selector).append(headerTr$);


  return columnSet;

}

HTML:


<body onLoad="buildHtmlTable('#excelDataTable')">

<table id="excelDataTable" border="1">

</table>

</body>


我如何获得预期的输出?


慕的地8271018
浏览 107回答 1
1回答

弑天下

如果您有嵌套数据结构,则还必须遍历嵌套元素function buildHtmlTable(selector) {&nbsp; &nbsp; var columns = addAllColumnHeaders(myList, selector);&nbsp; &nbsp; for (var i = 0; i < myList.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var row$ = $('<tr/>');&nbsp; &nbsp; &nbsp; &nbsp; for (var colIndex = 0; colIndex < columns.length; colIndex++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var cellValue = getCellValue(myList[i], columns[colIndex]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (cellValue == null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; cellValue = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; row$.append($('<td/>').html(cellValue));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $(selector).append(row$);&nbsp; &nbsp; }}function addAllColumnHeaders(myList, selector) {&nbsp; &nbsp; var columnSet = [];&nbsp; &nbsp; var headerTr$ = $('<tr/>');&nbsp; &nbsp; for (var i = 0; i < myList.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; var rowHash = myList[i];&nbsp; &nbsp; &nbsp; &nbsp; recursiveHeaderCheck(rowHash, selector, "", columnSet, headerTr$);&nbsp; &nbsp; }&nbsp; &nbsp; $(selector).append(headerTr$);&nbsp; &nbsp; return columnSet;}function recursiveHeaderCheck(ListElement, selector, parentKeyname, columnSet, headerTr$) {&nbsp; &nbsp; if (typeof ListElement === 'object') {&nbsp; &nbsp; &nbsp; &nbsp; if (parentKeyname != '')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parentKeyname = parentKeyname + '-';&nbsp; &nbsp; &nbsp; &nbsp; for (var key in ListElement) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; var keyname = parentKeyname + key;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; recursiveHeaderCheck(ListElement[key], selector, keyname, columnSet, headerTr$);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; if ($.inArray(parentKeyname, columnSet) == -1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; columnSet.push(parentKeyname);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; headerTr$.append($('<th/>').html(parentKeyname));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}function getCellValue(nestedStructure, columnHeader) {&nbsp; &nbsp; var subElement = nestedStructure;&nbsp; &nbsp; var subHeaders = columnHeader.split("-");&nbsp; &nbsp; for (var k = 0; k < subHeaders.length; k++) {&nbsp; &nbsp; &nbsp; &nbsp; if (typeof subElement === 'object' && subElement != null)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subElement = subElement[subHeaders[k]];&nbsp; &nbsp; }&nbsp; &nbsp; return subElement;}
随时随地看视频慕课网APP
我要回答