The selected ones are accumulated

1

I have the following table:

$(document).ready(function() {
var table = $('#tblClientes').DataTable({
    'select': true,
    'paging': true,
    'info': true,
    'filter': true,
    'stateSave': true,
    'processing': false,
    'serverSide': false,
    .....
});
});

And the following code also:

var mult_select = function(tbody, table){
$('#tblClientes tbody').on( 'click', 'tr', function () {

    table.on('select.dt', function() {
      var array = [];
      table.rows('.selected').every(function(rowIdx) {
         array.push(table.row(rowIdx).data())
      });   
      console.log(array);
    });
});
}

The goal is that when selecting a set of rows you can send by ajax the id of each of them to make an SQL query.

The problem is that the console.log (array) every time I select or deselect shows me more and more, it is as if they were accumulating. I want that every time I select one or two (or more) I show everything in the same object in the array as the second display of the image.

Thanks in advance !!

    
asked by Cifu 20.09.2017 в 11:08
source

1 answer

1

Use a counter and when adding the data to the array, instead of using the push function, indicate the id to be used to save it in the array.

var mult_select = function(tbody, table){
$('#tblClientes tbody').on( 'click', 'tr', function () {
    var id_array_seleccionados = 0;
    table.on('select.dt', function() {
      var array = [];
      table.rows('.selected').every(function(rowIdx) {
         array[id_array_seleccionados] = table.row(rowIdx).data();
         id_array_seleccionados++; //incrementas para el siguiente
      });   
      console.log(array);
    });
});
}
    
answered by 20.09.2017 / 11:27
source