jQuery DataTables: Row selection using checkboxes

Update
January 15, 2016
Check out our extension jQuery DataTables Checkboxes which offers much easier way to add checkboxes and multiple row selection to a table powered by jQuery DataTables.

This is a follow-up article to jQuery DataTables – How to add a checkbox column describing a simple solution to add checkboxes to a table. However proposed solution worked for a table using client-side processing mode only. This article offers universal solution that would work both in client-side and server-side processing modes.

It is loosely based on DataTables example – Row selection but adds extra functionality such as ability to use checkboxes for row selection and other minor improvements.

Example

Example below shows a data table using client-side processing mode where data is received from the server using Ajax. However the same code could be used if data table is switched into server-side processing mode with 'serverSide': true initialization option.

Name Position Office Extn. Start date Salary
Name Position Office Extn. Start date Salary

Data submitted to the server:


//
// Updates "Select all" control in a data table
//
function updateDataTableSelectAllCtrl(table){
   var $table             = table.table().node();
   var $chkbox_all        = $('tbody input[type="checkbox"]', $table);
   var $chkbox_checked    = $('tbody input[type="checkbox"]:checked', $table);
   var chkbox_select_all  = $('thead input[name="select_all"]', $table).get(0);

   // If none of the checkboxes are checked
   if($chkbox_checked.length === 0){
      chkbox_select_all.checked = false;
      if('indeterminate' in chkbox_select_all){
         chkbox_select_all.indeterminate = false;
      }

   // If all of the checkboxes are checked
   } else if ($chkbox_checked.length === $chkbox_all.length){
      chkbox_select_all.checked = true;
      if('indeterminate' in chkbox_select_all){
         chkbox_select_all.indeterminate = false;
      }

   // If some of the checkboxes are checked
   } else {
      chkbox_select_all.checked = true;
      if('indeterminate' in chkbox_select_all){
         chkbox_select_all.indeterminate = true;
      }
   }
}

$(document).ready(function (){
   // Array holding selected row IDs
   var rows_selected = [];
   var table = $('#example').DataTable({
      'ajax': {
         'url': '/lab/articles/jquery-datatables-checkboxes/ids-arrays.txt' 
      },
      'columnDefs': [{
         'targets': 0,
         'searchable': false,
         'orderable': false,
         'width': '1%',
         'className': 'dt-body-center',
         'render': function (data, type, full, meta){
             return '<input type="checkbox">';
         }
      }],
      'order': [[1, 'asc']],
      'rowCallback': function(row, data, dataIndex){
         // Get row ID
         var rowId = data[0];

         // If row ID is in the list of selected row IDs
         if($.inArray(rowId, rows_selected) !== -1){
            $(row).find('input[type="checkbox"]').prop('checked', true);
            $(row).addClass('selected');
         }
      }
   });

   // Handle click on checkbox
   $('#example tbody').on('click', 'input[type="checkbox"]', function(e){
      var $row = $(this).closest('tr');

      // Get row data
      var data = table.row($row).data();

      // Get row ID
      var rowId = data[0];

      // Determine whether row ID is in the list of selected row IDs 
      var index = $.inArray(rowId, rows_selected);

      // If checkbox is checked and row ID is not in list of selected row IDs
      if(this.checked && index === -1){
         rows_selected.push(rowId);

      // Otherwise, if checkbox is not checked and row ID is in list of selected row IDs
      } else if (!this.checked && index !== -1){
         rows_selected.splice(index, 1);
      }

      if(this.checked){
         $row.addClass('selected');
      } else {
         $row.removeClass('selected');
      }

      // Update state of "Select all" control
      updateDataTableSelectAllCtrl(table);

      // Prevent click event from propagating to parent
      e.stopPropagation();
   });

   // Handle click on table cells with checkboxes
   $('#example').on('click', 'tbody td, thead th:first-child', function(e){
      $(this).parent().find('input[type="checkbox"]').trigger('click');
   });

   // Handle click on "Select all" control
   $('thead input[name="select_all"]', table.table().container()).on('click', function(e){
      if(this.checked){
         $('#example tbody input[type="checkbox"]:not(:checked)').trigger('click');
      } else {
         $('#example tbody input[type="checkbox"]:checked').trigger('click');
      }

      // Prevent click event from propagating to parent
      e.stopPropagation();
   });

   // Handle table draw event
   table.on('draw', function(){
      // Update state of "Select all" control
      updateDataTableSelectAllCtrl(table);
   });

   // Handle form submission event 
   $('#frm-example').on('submit', function(e){
      var form = this;
      
      // 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)
         );
      });
   });

});

In addition to the above code, the following Javascript library files are loaded for use in this example:

//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js
//cdn.datatables.net/1.10.7/js/jquery.dataTables.min.js
table.dataTable.select tbody tr,
table.dataTable thead th:first-child {
  cursor: pointer;
}

The following CSS library files are loaded for use in this example to provide the styling of the table:

//cdn.datatables.net/1.10.7/css/jquery.dataTables.min.css

Edit on jsFiddle

Other examples

Problem

The problem with handling checkboxes varies based on DataTables initialization settings. In server-side processing mode ('serverSide':true) elements <input type="checkbox"> would exist for current page only. Once page is changed, the checked state of the checkboxes would not be preserved. In client-side processing mode, the checked state of checkbox is preserved, but only current page is accessible in DOM, all other pages has to be accessible through DataTables API.

Solution

The solution is to create a global variable (rows_selected in our example) to store a list of selected row IDs and use it to display checkbox state and highlight selected rows.

Highlights

Javascript

  • Storing selected row IDs

       // Array holding selected row IDs
       var rows_selected = [];

    Define array holding selected row IDs.

  • Columns definition

          'columnDefs': [{
             'targets': 0,
             'searchable': false,
             'orderable': false,
             'width': '1%',
             'className': 'dt-body-center',
             'render': function (data, type, full, meta){
                 return '<input type="checkbox">';
             }
          }],
    

    Option columnsDef is used to define appearance and behavior of the first column ('targets': 0).

    Searching and ordering of the column is not needed so this functionality is disabled with searchable and orderable options.

    To center checkbox in the cell, internal DataTables CSS class dt-body-center is used.

    Option render is used to prepare the checkbox control for being displayed in each cell of the first column.

  • Initial sorting order

          'order': [[1, 'asc']],
    

    By default, DataTables sorts table by first column in ascending order. By using order option we select another column to perform initial sort.

  • Row draw callback

          'rowCallback': function(row, data, dataIndex){
             // Get row ID
             var rowId = data[0];
    
             // If row ID is in the list of selected row IDs
             if($.inArray(rowId, rows_selected) !== -1){
                $(row).find('input[type="checkbox"]').prop('checked', true);
                $(row).addClass('selected');
             }
    

    Callback function rowCallback will be called before each row draw and is useful to indicate the state of the checkbox and row selection. We use internal DataTables CSS class selected.

    Important

    Please note that in the example above row ID is stored as first element of the row data array and is being retrieved by using the following code.

             // Get row ID
             var rowId = data[0];

    If you’re using data structure other than described in the article, adjust this and other similar lines accordingly.

  • Form submission

       // Handle form submission event 
       $('#frm-example').on('submit', function(e){
          var form = this;
          
          // 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)
             );
          });
       });
    

    When table is enhanced by jQuery DataTables plug-in, only visible elements exist in DOM. That is why by default form submits checkboxes from current page only.

    To submit selected checkboxes from all pages we need to iterate over rows_selected array and add a hidden <input> element to the form with some name (id[] in our example) and row ID as a value. This will allow all the data to be submitted.

HTML

<table id="example" class="display select" cellspacing="0" width="100%">

Additional CSS class select is used to change cursor when hovering over table rows for specific tables only.

CSS

table.dataTable.select tbody tr,
table.dataTable thead th:first-child {
  cursor: pointer;
}

Display cursor in the form of a hand for table rows and first cell in the table heading where “Select all” control is located.

Related posts

Comments

  1. This is great article! Just wondering from something that I work on. How could a user select all data in the server-side example?
    As I click the check all checkbox, user might expect they select all data instead all the displaying page record.

  2. Nice article! Help me a lot. I was working with a large amount of data (more than 10 000 rows) and the method to manage the ‘Select All’ was slow because we the triggered the click event several times. My workaround was this:

       // Handle click on "Select all" control
       $('thead input[name="select_all"]', table.table().container()).on('click', function(e){
          $('tbody input[type="checkbox"]', '#example').prop('checked', this.checked);
          rows_selected = [];
    			
          if (this.checked) {
         	  $('tbody tr', '#example').addClass('selected');
            var data = table.rows().data();
            var len = data.length;
            var i;
    			 
            for (i = 0; i < len; i++) {
              rows_selected.push(data[i][0]);
            }       
          } else {
         	  $('tbody tr', '#example').removeClass('selected');
          }
         
          // Prevent click event from propagating to parent
          e.stopPropagation();
       });
    
    1. This is an example of poor UX. Selecting a row should not select the checkbox automatically. It is considered an unexpected behavior. What happens if you want to support multiple links on a row? Or if you want to support inline editing on a row. While the solution is attractive an elegant, it is not considered standard behavior in user experience. I’d be happy to discuss this further.

      1. John, thank you for your detailed comment. I can partially agree with you. My example shows one particular use case. Links would need to be handled accordingly so that click on the link doesn’t select a row. In case of inline editing, row selection will be distracting, it’s true. But my article tries to solve the problem of handling checkboxes in the table powered by jQuery DataTables, row highlighting is an extra bonus and can be removed from the code with some effort.

  3. After selecting one or more rows I needed to apply some functionality on them and this involved ajax call. After completion of ajax call JQuery fires callbacks and within these callbacks it calls a JQuery method jQuery.ajaxTransport( option ) which has a function that gets ‘type’ parameter from i don’t know where and this parameter is undefined

    callback = function (type) {
        return function () {
            if (callback) {
                delete xhrCallbacks[id];
                callback = xhr.onload = xhr.onerror = null;
    
                if (type === "abort") {
                    xhr.abort();
                } else if (type === "error") {
                    complete(
                        // file: protocol always yields status 0; see #8605, #14207
                        xhr.status,
                        xhr.statusText
                    );
                } else {
                    complete(
                        xhrSuccessStatus[xhr.status] || xhr.status,
                        xhr.statusText,
                        // Support: IE9
                        // Accessing binary-data responseText throws an exception
                        // (#11426)
                        typeof xhr.responseText === "string" ? {
                            text: xhr.responseText
                        } : undefined,
                        xhr.getAllResponseHeaders()
                    );
                }
            }
        };
    };
    

    So after this function ALL REMAINING CHECKBOXS AND ROWS ARE SELECTED automatically and checkbox in header is selected too

  4. Hi , Thanks for this. I tried implementing the same, but when i alert the rowid.value on onSubmit, it is working only for the page where i am in. for example if i am in first page i can get the value in alert on only that page, not the second one. Any Help ?

    1. I have textboxes in my rows and want to get the values of text boxes when i submit, am able to get values for only the first page records, not for the second one. Similarly if i navigate to second page i am able to get the text box values for second page only not the first page.
      May i know the resolution for this ?

  5. First, thank you very much for the tutorial as it helped me a lot.

    I’m having some problems which I have documented on Stackoverflow: http://stackoverflow.com/questions/41130976/submitting-form-resubmits-the-same-data

    Basically I set it up to post back using ajax so I didn’t have to do a page refresh when submitting. But running into a problem where the form variable still holds on to previously selected values even though they won’t appear in the rows_selected variable.

    If you have a few minutes could you take a look please?

    1. Nevermind I figured it out. I ended up iterating back over the selected rows and removing the elements from the form variable after I get a success back from ajax.

  6. Hello . I have a problem. I can manage to select all the values. I have the pagination on my table and when i select all , i just get checked the fields that i have in the actual page.

    After debugin i sow that in data object just have the rows in selected page, not al the pages.
    Is possible … when i build the table to assign all data , not only the first page ?¿
    I sow that you have a answer at this problem. But is just not working for me.
    Please someone help .

  7. How would you handle selecting all paginated records (including the pages not yet fetched) in a server side processing table?

    I have a select all column checkbox for the current page that, when clicked, shows a link to select all paginated rows. I set it up that when you visit each page, the checkboxes are added to the array and set to true. It is a small quandary because It will only retain the data on the pages you visited. What about the scenario where the user modifies some checkboxes (or worse, checks the page’s select all to false) and leaves that page and comes back? You would not know the difference.

    Maybe I can track what the user deselects and overlay that against the default select all. That makes more sense to me.

    1. We’re considering adding this feature to jQuery DataTables Checkboxes plugin, please see issue #19 on GitHub.

      Basically, you would need to track three things: last state of “Select all” control, list of checked checkboxes and list of unchecked checkboxes.

      • Checking/unchecking “Select all” control should clear both lists.
      • When checkbox is checked, add it to the list of checked checkboxes and remove from the list of unchecked checkboxes.
      • When checkbox is unchecked, add it to the list of unchecked checkboxes and remove from the list of checked checkboxes.

      You need to submit all this information to the server-side script which then should be able to tell the state of all checkboxes.

      • If last state of “Select all” is checked and list of unchecked checkboxes is empty, consider that all checkboxes are checked
      • If last state of “Select all” is checked and list of unchecked checkboxes is not empty, consider that all checkboxes are checked except those in the list of unchecked checkboxes
      • If last state of “Select all” is unchecked and list of checked checkboxes is empty, consider that all checkboxes are unchecked
      • If last state of “Select all” is unchecked and list of checked checkboxes is not empty, consider that all checkboxes are unchecked except those in the list of checked checkboxes

      Hope that makes sense.

      1. Initially I thought I could just get away with a deselected list, but the pagination would still save the checkbox state on non-current pages after I clear all table checkboxes. So yes, your reply makes sense. It would be based on the state of the select all whether you take all records minus the unchecked list or just the checked list, if not empty.

        It is not very eloquent how I wrote it using jQuery, but it gets the job done for now and it can be improved later. Thanks.

  8. I have custom check-boxes like above and also have edit button, but clicking edit button it shows blank form as rowId is not updated.
    How can we pragmatically set rowId to table? I have to set it inside checkbox checked event. Please help

  9. Thanks for the perfect script. But what is to do when the click event should just be on the checkbox and not on one of the other elements in the data row?

  10. it worked for me by adding a line

    selector: ‘input[type=”checkbox”]’

    
    'select': {
             'style': 'multi',
             selector: 'input[type="checkbox"]'
          },
    
  11. Nice tutorial! I have Error Message “Illegal mix of collations for operation ‘like’” when search about something. My database Collation Is utf8_general_ci. How to fix that? Thanks!

Leave a Reply

You may use simple HTML to add links or lists to your comment. Also use <pre><code class="language-*">...</code></pre> to mark up code snippets. We support language-js, language-markup and language-css for comments.
(Optional)