简体   繁体   中英

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

How to pass this date object to the table?

The other objects are ok, I just have a problem with the data object

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 .

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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