gyrocode / jquery-datatables-checkboxes

Checkboxes is an extension for the jQuery DataTables library that provides universal solution for working with checkboxes in a table.
https://www.gyrocode.com/projects/jquery-datatables-checkboxes/
MIT License
150 stars 61 forks source link

Checkbox not selecting the row. #103

Closed Benjythebee closed 4 years ago

Benjythebee commented 4 years ago

I'm using serverside datatable with checkboxes in the first column. I have the following script based on https://jsfiddle.net/snqw56dw/

var table = $('#table-keys').DataTable({
  'processing': true,
  'serverSide': true,
  'columnDefs': [
      {
         'targets': 0,
         'data': null,
         'checkboxes': {
            'selectRow': true,
         }
      },
      {
        'targets': 3,
        'render': function ( data, type, row ) {
            return data.substr( 0, 70 );
        }
      }
      ],

   'ajax': 'post/dt_fingerprint.php',
   'select': {
      'style': 'multi',
   },
   'order': [[1, 'asc']]
});

$('#table-gen-key').on('submit', function(e){
      var form = this;

      var rows_selected = table.column(0).checkboxes.selected();

      console.log($rows_selected);
      // Iterate over all selected checkboxes
      $.each(rows_selected, function(index, rowId){
        console.log(index);
         // Create a hidden element 
         $(form).append(
             $('<input>')
                .attr('type', 'text')
                .attr('name', 'id[]')
                .val(rowId)
         );
      });
      $('#example-console-rows').text(rows_selected.join(","));
      e.preventDefault();
});

I'm using datatable dt-1.10.20 and the latest version of datatables checkboxes.

When I click "submit" the function returns [Object object] (not the id) and the row is not selected in the table. I have tried using "Object.values" but that gives " ["[","o","b","j","e","c","t"... While trying to solve this problem I also realised that clicking the checkbox doesn't select the row at all (the selected attribute doesn't show up), even though it should since 'selectRow': true,

Help...

mpryvkin commented 4 years ago

Your server-side script should produce unique values for the column containing checkboxes, for example 1, 2, 3 and you cannot use data': null, for that column.

Benjythebee commented 4 years ago

What would you have recommended? I ended up using :

 $("#table-gen-key input[type=checkbox]:checked").each(function () {

}
mpryvkin commented 4 years ago

Again I recommend to modify your server-side script post/dt_fingerprint.php and return unique data as the first element of the array for each row, for example 1, 2, 3. Then you can use the code you already have. Also remove 'data': null.