('input:checked').length not working across pages

('input:checked').length not working across pages

khaoskhaos Posts: 47Questions: 11Answers: 0
edited October 2011 in DataTables 1.8
desired: At least one checkbox must must be checked to enable the button.

This works fine on each individual page. I am using fnDrawCallback to restart the .click for all the checkboxes. But I don't know how to get the real ('input:checked').length.

[code] $(document).ready(function () {
SetButtonBasedOnChecks();

$('.addme').click(function () {
SetButtonBasedOnChecks();
});
});

function SetButtonBasedOnChecks() {
var $btn = $('.notempty');

if ($('input:checked').length > 0) {
$btn.removeAttr('disabled');
} else {
$btn.attr('disabled', 'disabled');
}
} [/code]

the DataTable code:
[code]
var oTable = $('#<%= gvTest.ClientID %>').dataTable({
"bJQueryUI": true
, "bStateSave": false
, "sPaginationType": "full_numbers"
, "aoColumns": [
{ "sSortDataType": "dom-checkbox" }
, { "sSortDataType": "dom-text", "sType": "numeric" }
, { "sSortDataType": "dom-text" }
, { "sSortDataType": "dom-text" }
, { "sSortDataType": "dom-text" }
, { "sSortDataType": "dom-text" }
, { "sSortDataType": "dom-text" }
, { "sSortDataType": "dom-text" }
, { "sSortDataType": "dom-text" }
]
, "fnDrawCallback": function() {
//alert( 'DataTables has redrawn the table' );
SetButtonBasedOnChecks();
$('.addme').click(function () {
SetButtonBasedOnChecks();
});
}
});[/code]

Replies

  • khaoskhaos Posts: 47Questions: 11Answers: 0
    Beuller?
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    You need to use fnGetNodes to get all TR elements in the table and then apply your filter to that. For example:

    [code]
    if ($('input:checked', oTable.fnGetNodes()).length > 0) {
    [/code]

    The reason for this is that the TR elements which are needed for a given page are removed from the DOM - hence the need to use the API to get an array of the full list :-)

    Allan
  • khaoskhaos Posts: 47Questions: 11Answers: 0
    fnSweet()!! I'll do this right away.
  • khaoskhaos Posts: 47Questions: 11Answers: 0
    Allen thanks a bunch. This works swimmingly! I wanted to follow up for the lurkers:

    In order to get it to work I had to parenthesize the selector. No big but it took me a few minutes to wake up and smell the coffee.

    [code] if (($('input:checked', oTable.fnGetNodes())).length > 0) { [/code]
  • allanallan Posts: 63,498Questions: 1Answers: 10,470 Site admin
    Good to hear that does the job :-)

    Allan
This discussion has been closed.