Problem with DataTables ID

0

I'm doing a project and I'm having a question / problem ... I have a DataTables with the following content:

"aoColumns": [
  {"sTitle": "<input type='checkbox' class='pull-left' id='checkall'>","mDataProp": null, "sWidth": "20px", "sDefaultContent": "<input type='checkbox' class='pull-left' id='chbItem'>", "bSortable": false},
  { "mDataProp": "itemId", "sDefaultContent" : ""},
  { "mDataProp": "codBarra", "sDefaultContent" : ""},
  { "mDataProp": "descricao", "sDefaultContent" : ""},
  { "mDataProp": "grupo.descricao", "sDefaultContent" : ""},
  { "mDataProp": "cardapio.nome", "sDefaultContent" : ""},
],

How do I retrieve itemId of each checkbox I checked?

I tried the following (using an old method):

var table = $('#tabela-cardapios2').dataTable();

$(document).on('click', '#btnRemover', function () {
  $('input:checked', tabelaVinculados.fnGetNodes()).each(function(index){
    console.log(tabelaVinculados.fnGetData(index).itemId);
  });
});

And it even returns itemId , but always the first items, not the ones I set. For example, I marked item 3 and it displays the id of item 1, if it marks 3 and 4, it displays the id of item 1 and 2.

Anyway, can anyone tell me how to retrieve this ID using the current version of DataTables? If nobody knows, it may be using the old version itself, as I tried to do there ...

-EDIT -

In my JSP there is nothing in the creation of the table, only the body of it:

                                       Internal Code             Barcode             description             Group             Menu                                                                internal code             bar code             description             Group             Menu              

Here is where I create the fact table

function visualizaProdutos(cardapio) {
    var table = $('#tabela-vinculados').DataTable( {
    "aLengthMenu": [[5, 10, 15, 20, 25, 30, 35, 100, -1], [5, 10, 15, 20, 25, 30, 35, 100, "Todos"]],
    "pageLength": 10,
    "paging": true,
    "lengthChange": true,
    "searching": true,
    "ordering": true,
    "bStateSave": true,
    "info": true,
    "autoWidth": true,
    "responsive": true,
    "cursor": "pointer",
    destroy: true,
    "sAjaxSource": "visualizaProdutos/" + cardapio,
    "fnServerData": function ( sSource, aoData, fnCallback ) {
            aoData.push( { "name": "widget", "value": "token" } );
            request = $.ajax({
              "dataType": 'json', 
              "type": "GET", 
              "url": sSource, 
              "data": aoData, 
              "success": fnCallback
            });
      },
      "aoColumns": [


            { "mDataProp": null, "sWidth": "20px", "sDefaultContent": "<label class='switch'><input value='itemId' id='chbRemover' type='checkbox'><div class='slider round'></div></label>", "bSortable": false},
            { "mDataProp": "itemId", "sDefaultContent" : ""},
            { "mDataProp": "codBarra", "sDefaultContent" : ""},
            { "mDataProp": "descricao", "sDefaultContent" : ""},
            { "mDataProp": "grupo.descricao", "sDefaultContent" : ""},
            { "mDataProp": "cardapio.nome", "sDefaultContent" : ""},
        ],
    });

table.ajax.reload(null, false);
}

What's happening is that I can even get my itemId , only it's coming from random products, here's an example:

Iselectedtheproduct105023anditreturnedthe837032

Thankyouinadvance.

-Solution-

I'vechangedthetypeofselectingthetableitems,using:

"select": {
         style: 'multi'
    },

In the click of the button, I did the following to get the ID's:

    for (var i = 0; i < tabela2.rows('.selected').data().length; i++) { 
        console.log( tabela2.rows('.selected').data()[i].itemId);
    }
    
asked by anonymous 04.01.2017 / 13:12

1 answer

0

Solution found:

I've changed the type of selecting the table items, using:

    "select": {
     style: 'multi'
},

In the click of the button, I did the following to get the ID's:

for (var i = 0; i < tabela2.rows('.selected').data().length; i++) { 
    console.log( tabela2.rows('.selected').data()[i].itemId);
}
    
05.01.2017 / 13:15