1

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",
  },
];

Table


$.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')
5
  • That's not valid JSON. JSON requires quotes around property names. Commented Jul 21, 2020 at 14:49
  • [ { tipo: "O", numero: "001", data: { year: 2019, month: 4, day: 18 }, prazo: 0, documento: "4600530888" //fixed without comma (","). } //fixed without comma (","). ] Commented Jul 21, 2020 at 15:37
  • @D.Pardal fixed, but im still getting the undefied error when try to show the "Data" Commented Jul 21, 2020 at 15:39
  • @AllPower did you tested below code? Commented Jul 22, 2020 at 4:59
  • @Swati HISwati, Thanks for the help, it worked really well, I already accepted it as an answer! Commented Jul 23, 2020 at 21:44

1 Answer 1

1

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>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.