简体   繁体   English

当我在表中插入日期 object 时,JSON ajax 返回 undefined

[英]JSON ajax returns undefined when I insert a date object in the table

I am trying to insert the elements in a table, but when showing the data object, the undefined message is appearing我试图在表格中插入元素,但是在显示数据 object 时,出现未定义的消息

How to pass this date object to the table?如何将此日期 object 传递给表?

The other objects are ok, I just have a problem with the data object其他对象都还好,只是数据object有问题

JSON: JSON:

[
  {
    tipo: "O",
    numero: "001",
    data: { year: 2019, month: 4, day: 18 },
    prazo: 0,
    documento: "4600530888",
  },
];

桌子


$.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')

You don't need two loop to iterate through values you can use one loop and access all value inside your json using item.keyname and to access json object inside json array write item.data.keyname . You don't need two loop to iterate through values you can use one loop and access all value inside your json using item.keyname and to access json object inside json array write item.data.keyname .

Demo Code :演示代码

 //your response var data = [{ tipo: "O", numero: "001", data: { year: 2019, month: 4, day: 18 }, prazo: 1, documento: "4600530888" }, { tipo: "O", numero: "001", data: { year: 2009, month: 4, day: 18 }, prazo: 0, documento: "4600530588" } ] var indices = ['Tipo', 'Numero', 'Document', 'Prazo','Data'] var table = $("<table border='1'>") var thead = $('<thead>') for (const index of indices) { $('<th>' + index + '</th>').appendTo(thead) } var tbody = $('<tbody>') for (const item of data) { var tr = $('<tr>') //get datas from json $('<td>' + item.tipo + '</td>').appendTo(tr) $('<td>' + item.numero + '</td>').appendTo(tr) $('<td>' + item.prazo + '</td>').appendTo(tr) $('<td>' + item.documento + '</td>').appendTo(tr) $("<td>" + item.data.year + "/" + item.data.month + "/" + item.data.day + "</td>").appendTo(tr) tr.appendTo(tbody) } //apend data in thead 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM