当我在表中插入日期对象时,JSON ajax 返回未定义

我试图在表中插入元素,但在显示数据对象时,出现未定义的消息


如何将这个日期对象传递给表?


其他对象没问题,我只是数据对象有问题


JSON:


[

  {

    tipo: "O",

    numero: "001",

    data: { year: 2019, month: 4, day: 18 },

    prazo: 0,

    documento: "4600530888",

  },

];

http://img2.mukewang.com/63832059000154b403800112.jpg

$.ajax({

      url: 'buscaTextoAditivos.action', // action to be perform

      type: 'POST',       //type of posting the data

      data: { linhaSelecionadaJson: jsonHide }, // data to set to Action Class

      dataType: 'json',

      success: function (data) {


      var indices = ['Tipo', 'Numero', 'Data', 'Prazo', 'Document']

      var table = $("<table>")

      var thead = $('<thead>')

        for (const index of indices) {

               $('<th>' + index + '</th>').appendTo(thead)

            }

               var tbody = $('<tbody>')

                for (const item of data) {

                     var tr = $('<tr>')

                        for (const index of indices) {

                            $('<td>' + item[index] + '</td>').appendTo(tr)

                        }

                        tr.appendTo(tbody)

                    }

                    tbody.appendTo(table)


                  $("#loaderMaiorDemandante").hide();

                  table.appendTo('#records_table')


慕桂英4014372
浏览 83回答 1
1回答

一只甜甜圈

您不需要两个循环来遍历值,您可以使用一个循环并访问jsonusing中的所有值item.keyname,并访问 json 数组 write 中的 json 对象item.data.keyname。演示代码://your responsevar data = [{&nbsp; &nbsp; tipo: "O",&nbsp; &nbsp; numero: "001",&nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; year: 2019,&nbsp; &nbsp; &nbsp; month: 4,&nbsp; &nbsp; &nbsp; day: 18&nbsp; &nbsp; },&nbsp; &nbsp; prazo: 1,&nbsp; &nbsp; documento: "4600530888"&nbsp; },&nbsp; {&nbsp; &nbsp; tipo: "O",&nbsp; &nbsp; numero: "001",&nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; year: 2009,&nbsp; &nbsp; &nbsp; month: 4,&nbsp; &nbsp; &nbsp; day: 18&nbsp; &nbsp; },&nbsp; &nbsp; prazo: 0,&nbsp; &nbsp; documento: "4600530588"&nbsp; }]var indices = ['Tipo', 'Numero', 'Document', 'Prazo','Data']var table = $("<table border='1'>")var thead = $('<thead>')for (const index of indices) {&nbsp; $('<th>' + index + '</th>').appendTo(thead)}var tbody = $('<tbody>')for (const item of data) {&nbsp; var tr = $('<tr>')&nbsp; //get datas from json&nbsp;&nbsp; $('<td>' + item.tipo + '</td>').appendTo(tr)&nbsp; $('<td>' + item.numero + '</td>').appendTo(tr)&nbsp; $('<td>' + item.prazo + '</td>').appendTo(tr)&nbsp; $('<td>' + item.documento + '</td>').appendTo(tr)&nbsp; &nbsp;$("<td>" + item.data.year + "/" + item.data.month + "/" + item.data.day + "</td>").appendTo(tr)&nbsp; &nbsp; tr.appendTo(tbody)}//apend data in thead&nbsp;thead.appendTo(table)tbody.appendTo(table)$("#loaderMaiorDemandante").hide();table.appendTo('#records_table')<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><div id="records_table"></div>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript