Proper way to get all rows from data table?

Proper way to get all rows from data table?

mawi5mawi5 Posts: 1Questions: 1Answers: 0

I have this dataTable:

$('#dtTecnologia').dataTable({
        paging: false,
        searching: false,
        ordering: false,
        scrollY: 177,
        info: false,
        "data": null,
        "columns": [
            { "data": "Tecnologia" }
        ]
    });
}


dtTecnologia = $('#dtTecnologia').DataTable();

And I have a function to add rows:


function addRow(oTecnologia) { oRow = dtTecnologia.row.add(oTecnologia).draw().node(); console.log("New data added:"); console.log(dtTecnologia.row(oRow).data()); console.log("At index:"); console.log(dtTecnologia.row(oRow).index()); }

After that, I add some rows:


addRow({TecnologiaId: 1, Tecnologia: "Test 1"}); addRow({TecnologiaId: 2, Tecnologia: "Test 2"});

At this point, I have no problem, but when I try to get all the data from the data table, using either of this ways:

var oData = dtTecnologia .rows().data();
var oData = dtTecnologia.data();

The oData object contains additional data, not just the rows, and I just want the two objects added:

How can I get only the two objects added?

What's the proper way to get the data from dataTable?

Is there a direct way to get it or I have to iterate the dataTable?

Thanks guys

*This is the data in the object oData:

//Result of the object oData

{
   "0":{
      "TecnologiaId":"1",
      "Tecnologia":"Test 1"
   },
   "1":{
      "TecnologiaId":"2",
      "Tecnologia":"Test 2"
   },
   "context":[
      {
         "oFeatures":{
            "bAutoWidth":true,
            "bDeferRender":false,
            "bFilter":false,
            "bInfo":false,
            "bLengthChange":true,
            "bPaginate":false,
            "bProcessing":false,
            "bServerSide":false,
            "bSort":false,
            "bSortMulti":true,
            "bSortClasses":true,
            "bStateSave":null
         },
         "oScroll":{
            "bCollapse":false,
            "iBarWidth":21,
            "sX":"",
            "sXInner":"",
            "sY":177
         },
         "oLanguage":{
            "fnInfoCallback":null,
            "oAria":{
               "sSortAscending":": activate to sort column ascending",
               "sSortDescending":": activate to sort column descending",
               "_hungarianMap":{
                  "sortAscending":"sSortAscending",
                  "sortDescending":"sSortDescending"
               }
            },
            "oPaginate":{
               "sFirst":"First",
               "sLast":"Last",
               "sNext":"Next",
               "sPrevious":"Previous",
               "_hungarianMap":{
                  "first":"sFirst",
                  "last":"sLast",
                  "next":"sNext",
                  "previous":"sPrevious"
               }
            },
            "sEmptyTable":"No data available in table",
            "sInfo":"Showing _START_ to _END_ of _TOTAL_ entries",
            "sInfoEmpty":"Showing 0 to 0 of 0 entries",
            "sInfoFiltered":"(filtered from _MAX_ total entries)",
            "sInfoPostFix":"",
            "sDecimal":"",
            "sThousands":",",
            "sLengthMenu":"Show _MENU_ entries",
            "sLoadingRecords":"Loading...",
            "sProcessing":"Processing...",
            "sSearch":"Search:",
            "sSearchPlaceholder":"",
            "sUrl":"",
            "sZeroRecords":"No matching records found",
            "_hungarianMap":{
               "aria":"oAria",
               "paginate":"oPaginate",
               "emptyTable":"sEmptyTable",
               "info":"sInfo",
               "infoEmpty":"sInfoEmpty",
               "infoFiltered":"sInfoFiltered",
               "infoPostFix":"sInfoPostFix",
               "decimal":"sDecimal",
               "thousands":"sThousands",
               "lengthMenu":"sLengthMenu",
               "loadingRecords":"sLoadingRecords",
               "processing":"sProcessing",
               "search":"sSearch",
               "searchPlaceholder":"sSearchPlaceholder",
               "url":"sUrl",
               "zeroRecords":"sZeroRecords"
            }
         },
         "oBrowser":{
            "bScrollOversize":false,
            "bScrollbarLeft":false
         },
         "ajax":null,
         "aanFeatures":[

         ],
         "aoData":[
            {
               "nTr":{
                  "_DT_RowIndex":0
               },
               "anCells":[
                  {

                  }
               ],
               "_aData":{
                  "TecnologiaId":"1",
                  "Tecnologia":"Ciclo Combinado"
               },
               "_aSortData":null,
               "_aFilterData":null,
               "_sFilterRow":null,
               "_sRowStripe":"odd",
               "src":"data"
            },
            {
               "nTr":{
                  "_DT_RowIndex":1
               },
               "anCells":[
                  {

                  }
               ],
               "_aData":{
                  "TecnologiaId":"2",
                  "Tecnologia":"Combustión interna"
               },
               "_aSortData":null,
               "_aFilterData":null,
               "_sFilterRow":null,
               "_sRowStripe":"even",
               "src":"data"
            }
         ],
         "aiDisplay":[
            0,
            1
         ],
         "aiDisplayMaster":[
            0,
            1
         ], 
   ],
   "length":2,
   "selector":{
      "rows":"",
      "cols":null,
      "opts":{
         "search":"none",
         "order":"current",
         "page":"all"
      }
   },
   "ajax":{
      "__dt_wrapper":true
   }
}

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,214Questions: 1Answers: 10,415 Site admin
    Answer ✓

    How can I get only the two objects added?

    Use dtTecnologia.row.add(oTecnologia).data() to get the data from the new row (although that seems a little pointless since you already have it in oTecnologia).

    *This is the data in the object oData:

    That is the API instance properties as well as the contents of the instance. Use the toArray() method to convert the API instance to just a plain old array.

    Allan

This discussion has been closed.