Go through Json that is inside an array with each of jquery

0

Help please as I walk this array with each of jquery (what I print is the console.log of the data input variable):

this is my code:

function crearTabla(datos){
$.each(datos, function(i,item){
                    console.log(item);
                    var index=parseInt(i);
                    index++;
                    var tr= $('<tr>').append(
                                $('<td>').text(index),
                                $('<td class="codigo">').html(item['id']),
                                $('<td class="descripcion">').html(item['descripcion']),
                                $('<td>').append('<input type="button" class="abrirModal btnVer" value="Edit">'));
                            $('tbody').append(tr);

            });

}
    
asked by Felix 19.06.2018 в 01:25
source

2 answers

0

I do not see anything wrong with your code, if you want to show the data as it indicates in your jquery function you need to add the <tr></tr> as I show you so that the append is done and the data is displayed.

function crearTabla(datos){
$.each(datos, function(i,item){
                    console.log(item);
                    var index=parseInt(i);
                    index++;
                    var tr= $('<tr>').append(
                                $('<td>').text(index),
                                $('<td class="codigo">').html(item['id']),
                                $('<td class="descripcion">').html(item['descripcion']),
                                $('<td>').append('<input type="button" class="abrirModal btnVer" value="Edit">'));
                            $('tbody').append(tr);

            });

}

var datos = [
       {
        "id": "9",
        "descripcion": "computadoras",
        "stock": "36.00"
      },
      {
        "id": "14",
        "descripcion": "galleta",
        "stock": "1120.00"
      },
      {
        "id": "5",
        "descripcion": "caramelos",
        "stock": "20.00"
      },
      {
        "id": "15",
        "descripcion": "honorarios",
        "stock": "0.00"
      }
     
    ];
    
crearTabla(datos);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<table>
	<tr></tr>
</table>
    
answered by 19.06.2018 в 01:38
0

It seems that the variable that your function receives has the following structure:

  • datos is an object that has a property called data
  • data is a data property, which is an array (Array)
  • It seems to me that the correct way to execute your function would be

    $.each(datos.data, function(idx, el) { console.log(el); });
    

    Example:

    var datos = {
     data: [
      {id: 0, descripcion: 'arroz'},
      {id: 1, descripcion: 'maiz'},
      {id: 2, descripcion: 'lechuga'},
      {id: 3, descripcion: 'tomate'},
     ]
    };
    
    $.each(datos.data, function(idx, el) {
      console.log(el);
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
        
    answered by 19.06.2018 в 06:42