0

I have created jQuery DataTables Checkboxes, now my problem is I cannot show the multiple checkboxes to select in the data table. I have tried to add a Checkboxes extension for the jQuery DataTables library that provides a universal solution for working with checkboxes in a table. But the error shows me the message is Script error Hope someone can point out which one I am getting error. Thanks.

Below is my sample coding:

$(document).ready(function() {
   var table = $('#example').DataTable({     
      'columnDefs': [
         {
            'targets': 0,
            'checkboxes': {
               'selectRow': true
            }
         }
      ],
      'select': {
         'style': 'multi'
      },
      'order': [[1, 'asc']]
   });
   
   // Handle form submission event 
   $('#frm-example').on('submit', function(e){
      var form = this;
      
      var rows_selected = table.column(0).checkboxes.selected();

      // Iterate over all selected checkboxes
      $.each(rows_selected, function(index, rowId){
         // Create a hidden element 
         $(form).append(
             $('<input>')
                .attr('type', 'hidden')
                .attr('name', 'id[]')
                .val(rowId)
         );
      });

     
      
      // Output form data to a console     
      $('#example-console-rows').text(rows_selected.join(","));
      
      // Output form data to a console     
      $('#example-console-form').text($(form).serialize());
       
      // Remove added elements
      $('input[name="id\[\]"]', form).remove();
       
      // Prevent actual form submission
      e.preventDefault();
   });   
});
 <script src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.21/js/jquery.dataTables.min.js"></script>
<link rel="stylesheet" href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/css/dataTables.checkboxes.css" />
 <script src="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/js/dataTables.checkboxes.min.js"></script>
 
 
<table id="example" class="display" cellspacing="0" width="100%">
   <thead>
      <tr>
         <th></th>
         <th>Name</th>
         <th>Position</th>
         <th>Office</th>
         <th>Extn</th>
         <th>Start date</th>
         <th>Salary</th>
      </tr>
   </thead>
   <tbody>
     <tr>
         <td>1</td>
         <td>Name</td>
         <td>Position</td>
         <td>Office</td>
         <td>Extn</td>
         <td>Start date</td>
         <td>Salary</td>
      </tr>
      <tr>
         <td>2</td>
         <td>Name</td>
         <td>Position</td>
         <td>Office</td>
         <td>Extn</td>
         <td>Start date</td>
         <td>Salary</td>
      </tr>
      <tr>
         <td>3</td>
         <td>Name</td>
         <td>Position</td>
         <td>Office</td>
         <td>Extn</td>
         <td>Start date</td>
         <td>Salary</td>
      </tr>
      
     
   </tbody>
   <tfoot>
      <tr>
         <th></th>
         <th>Name</th>
         <th>Position</th>
         <th>Office</th>
         <th>Age</th>
         <th>Start date</th>
         <th>Salary</th>
      </tr>
   </tfoot>
</table>

This is my expected result:

output

  • 1
    Your snippet is missing jquery. [edit] the snippet and select jquery from the drop down on the left then run your snippet and it works fine - I've not made this change for you incase that is the only issue you have as it would solve it in the question. – freedomn-m May 24 '21 at 07:54

1 Answers1

1

This code works.

$(document).ready(function() {
   var table = $('#example').DataTable({     
      'columnDefs': [
         {
            'targets': 0,
            'checkboxes': {
               'selectRow': true
            }
         }
      ],
      'select': {
         'style': 'multi'
      },
      'order': [[1, 'asc']]
   });
   
   // Handle form submission event 
   $('#frm-example').on('submit', function(e){
      var form = this;
      
      var rows_selected = table.column(0).checkboxes.selected();

      // Iterate over all selected checkboxes
      $.each(rows_selected, function(index, rowId){
         // Create a hidden element 
         $(form).append(
             $('<input>')
                .attr('type', 'hidden')
                .attr('name', 'id[]')
                .val(rowId)
         );
      });

     
      
      // Output form data to a console     
      $('#example-console-rows').text(rows_selected.join(","));
      
      // Output form data to a console     
      $('#example-console-form').text($(form).serialize());
       
      // Remove added elements
      $('input[name="id\[\]"]', form).remove();
       
      // Prevent actual form submission
      e.preventDefault();
   });   
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdn.datatables.net/1.10.24/css/jquery.dataTables.min.css" rel="stylesheet" />
<script src="https://cdn.datatables.net/1.10.24/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/jquery-datatables-checkboxes@1.2.12/js/dataTables.checkboxes.min.js"></script>
<table id="example" class="display" cellspacing="0" width="100%">
  <thead>
    <tr>
      <th></th>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Extn</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Name</td>
      <td>Position</td>
      <td>Office</td>
      <td>Extn</td>
      <td>Start date</td>
      <td>Salary</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Name</td>
      <td>Position</td>
      <td>Office</td>
      <td>Extn</td>
      <td>Start date</td>
      <td>Salary</td>
    </tr>
    <tr>
      <td>3</td>
      <td>Name</td>
      <td>Position</td>
      <td>Office</td>
      <td>Extn</td>
      <td>Start date</td>
      <td>Salary</td>
    </tr>


  </tbody>
  <tfoot>
    <tr>
      <th></th>
      <th>Name</th>
      <th>Position</th>
      <th>Office</th>
      <th>Age</th>
      <th>Start date</th>
      <th>Salary</th>
    </tr>
  </tfoot>
</table>
Bernhard Beatus
  • 1,191
  • 1
  • 5
  • 6