mbahaa mbahaa - 1 year ago 130
jQuery Question

Datatables 1.10 "Check all" via jquery

I know this might seem primitive, but I've been trying to implement it for a whole day, maybe because I can't fully comprehend how to use the API, I'm using DataTables 1.10.0, I have a table with pagination feature, each row has one checkbox in it, I need to have a "check all button" that would check all the checkboxes in all pages, the problem is that it only checks the checkboxes in the current page, and leaves the other pages unchecked, this is supposed to be easy, but I couldn't figure it out ! the answers I found use "fnGetNodes" which seems to be deprecated and not used by version 1.10

Edit: this is my markup

<table class="table table-striped table-bordered table-hover" id="numbers_table">
<th><input type="checkbox" id="checkall" title="Select all" onClick="toggle(this)"/></th>
<% _.each(array, function (value) { %>
<td><input type='checkbox' name='numbers[]' value='<>'/></td>
<td><%= value.number %></td>
<td><%= %></td>
<td><%= value.tags %></td>
<% }) %>

<button type="button" class="btn btn-primary" id="checkall2">SELECT ALL</button>


$(document).ready(function() {

//"bPaginate": false,
"aoColumnDefs": [
{ "bSortable": false, "aTargets": [ 0 ] }

$("#checkall2").click(function() { // a button with checkall2 as its id
$(':checkbox').prop('checked', true); // all checkboxes, you can narrow with a better selector

Answer Source

Try this code:

var table = $('#numbers_table').DataTable();

var cells = table
    .cells( ":checkbox" )

$( cells ).prop('checked', true);


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download