为了账号安全,请及时绑定邮箱和手机立即绑定

创建动态 JSON 表 HTML JS

创建动态 JSON 表 HTML JS

PHP
慕的地8271018 2023-11-06 15:36:52
我的示例 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/blank2020-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>我如何获得预期的输出?
查看完整描述

1 回答

?
弑天下

TA贡献1818条经验 获得超8个赞

如果您有嵌套数据结构,则还必须遍历嵌套元素


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 = getCellValue(myList[i], columns[colIndex]);

            if (cellValue == null)

                cellValue = "";

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

        }

        $(selector).append(row$);

    }

}



function addAllColumnHeaders(myList, selector) {

    var columnSet = [];

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


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

        var rowHash = myList[i];


        recursiveHeaderCheck(rowHash, selector, "", columnSet, headerTr$);

    }


    $(selector).append(headerTr$);


    return columnSet;

}



function recursiveHeaderCheck(ListElement, selector, parentKeyname, columnSet, headerTr$) {


    if (typeof ListElement === 'object') {

        if (parentKeyname != '')

            parentKeyname = parentKeyname + '-';

        for (var key in ListElement) {

            var keyname = parentKeyname + key;

            recursiveHeaderCheck(ListElement[key], selector, keyname, columnSet, headerTr$);

        }

    } else {

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

            columnSet.push(parentKeyname);

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

        }

    }


}


function getCellValue(nestedStructure, columnHeader) {

    var subElement = nestedStructure;

    var subHeaders = columnHeader.split("-");

    for (var k = 0; k < subHeaders.length; k++) {

        if (typeof subElement === 'object' && subElement != null)

            subElement = subElement[subHeaders[k]];

    }

    return subElement;

}



查看完整回答
反对 回复 2023-11-06
  • 1 回答
  • 0 关注
  • 106 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信