How i get all data in the table help

How i get all data in the table help

alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1

Hello, I'm trying to get all the data in a table,
but so monent I have not been able

for example i have this table

 <thead>
            <tr>
                <th>Name</th>
                <th>Position</th>
                <th>Office</th>
                <th>Age</th>
                <th>Start date</th>
                <th>Salary</th>
            </tr>
        </thead>
<tbody>
            <tr>
                <td>Tiger Nixon</td>
                <td>System Architect</td>
                <td>Edinburgh</td>
                <td>61</td>
                <td>2011/04/25</td>
                <td>$320,800</td>
            </tr>
<tr>
                <td>Garrett Winters</td>
                <td>Accountant</td>
                <td>Tokyo</td>
                <td>63</td>
                <td>2011/07/25</td>
                <td>$170,750</td>
            </tr>
</tbody>

i need get all data foreach row

1 row ->{Tiger Nixon,System Architect,Edinburgh,61,2011/04/25,$320,800}
2 row->{Garrett Winters,Accountant....................etc etc}

I'm testing with this code

$("#tblCatalogoActualModal td").each(function(index) {
                $(this).parents("tr").find("td").each(function(){/*metodo para recorrer la tabla*/
                      alert($(this).html());
                    switch($(this).parent().children().index($(this))) {//obtengo el index de la columna EKISDE
                        case 0:
                            codigo = $(this).html();
                            break;
                        case 1:
                            articulo = $(this).html();
                            break;
                        case 2:
                            break;
                        case 3:
                            puntos = $(this).html();
                            break;
                        case 5:
                            break;
                        default: 
                        //alert($(this).html());
                    }
                });
                alert("out");
            /*var form_data = {
                codigo: codigo,
                puntos: puntos,
                articulo: articulo,
                IdCatalogo: IdCatalogo
                };
             $.ajax({
                url: "actualizarCatalogo",
                type: "post",
                async:true,
                data: form_data,
                success:
                    function(json){
                        Materialize.toast('SE GUARDARON LOS CAMBIOS EN EL CATALOGO, ESPERE..', 3000);
                        var myVar = setInterval(myTimer, 2000);
                    }
                });*/
            });

This question has accepted answers - jump to:

Answers

  • DirceuNazarethDirceuNazareth Posts: 44Questions: 1Answers: 12
    Answer ✓

    So... when you initialize the datatable do something like:

    var myTable = $("#tblCatalogoActualModal").DataTable(//your properties...);
    
    //So, to get all data do:
    var form_data  = myTable.rows().data();
    

    and now you ajax you form_data;

  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1
    edited September 2016

    @DirceuNazareth thanks men!!!!
    I guess the form_data is an array and I need to cross it to get value for value.?
    for example

    myTable = $('#tblCatalogoActualModal').DataTable();
                var form_data  = myTable.rows().data();
    
                $.each( form_data, function( key, value ) {
                  alert( key + ": " + value );
                });
    

    ist that ok?

  • DirceuNazarethDirceuNazareth Posts: 44Questions: 1Answers: 12

    That depends how you initialized that data. It can be a 2d array, or array of objects:

    //This represent 3 rows with 4 columns
    var data= [
      ["a", "b","c", "y"],
     ["a", "h", "d", "z"],
     ["a", "h", "d", "a"]
    ]
    
    //or can be array of objects, like:
    
    var data= [
      {
         codigo: 1,
         puntos: 23,
         articulo: "abc",
         IdCatalogo: "shoes"
      },
      {
         codigo: 1,
         puntos: 3,
         articulo: "xuz",
         IdCatalogo: "shoes"
      },
      {
         codigo: 3451,
         puntos: 10,
         articulo: "x-large",
         IdCatalogo: "shorts"
      }
    ]
    
  • DirceuNazarethDirceuNazareth Posts: 44Questions: 1Answers: 12

    But, yes will be an array.

  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1

    hi @DirceuNazareth thanks for the help, her is mi initialization, i am draw the table with php

    $('#tblCatalogoActualModal').DataTable( {
                "info":    false,
                "bPaginate": true,
                "lengthMenu": [[5,10,50,100,-1], [5,10,50,100,"Todo"]],
                "language": {
                    "paginate": {
                        "first":      "Primera",
                        "last":       "Última ",
                        "next":       "Siguiente",
                        "previous":   "Anterior"
                    },
                    "lengthMenu": "MOSTRAR _MENU_ REGISTROS",
                    "search":     "BUSCAR"
                }
            });
    

    the data was create with php "echo" ....

    how I can get each of the array elements?
    for example

    var form_data  = myTable.rows().data();
    
    alert(form_data[row0][col0]);
    alert(form_data[row0][col1]);
    alert(form_data[row0][col2]);
    

    plis.. tthanks for the help

  • DirceuNazarethDirceuNazareth Posts: 44Questions: 1Answers: 12

    If that is the case, you have 2d array:

    taking your initial post HTML as example:

    var form_data  = myTable.rows().data();
     
    alert(form_data[0][0]);
    //will alert "Garrett Winters"
    
    alert(form_data[0][1]);
    //will alert "Accountant"
    
    alert(form_data[1][3]);
    //will alert "61"
    

    To iterate over it:

    var f = form_data;
    for(var i=0 ; f.length>i;i++){
      var n = f[i].length;
      for(var j = 0 ; f.length>j;j++){
        alert(f[i][j])
      }
    }
    
  • DirceuNazarethDirceuNazareth Posts: 44Questions: 1Answers: 12

    BTW... do you speak spanish or portuguese?

  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1
    edited September 2016

    @DirceuNazareth thanks men, you know why only works in the first row? in the others show me "undefined". i speak spanish... sorry my bad english

  • DirceuNazarethDirceuNazareth Posts: 44Questions: 1Answers: 12
    Answer ✓

    Oops my fault

    var f = form_data;
    for(var i=0 ; f.length>i;i++){
      var n = f[i].length;
      for(var j = 0 ; n>j;j++){
        alert(f[i][j])
      }
    }
    
  • alderhernandezalderhernandez Posts: 33Questions: 11Answers: 1

    thanks @DirceuNazareth .. lately but works. thanks man

This discussion has been closed.