javascriptjquerycheckboxpaginationdisplaytag

displaytag pagination javascript counting checkboxes


I have a displaytag table which uses the checkbox decorator to keep track of selected values in the table. I'm trying to use Jquery to keep track of the # of selected values in the table for validation purposes.

What would be the best way to count the # of selected values in the table when paging through the table?

Solution

I added a listener to each checkbox so that it completes a count when the value changes:

$(document).ready(function() {          
$('.ids').find("input:checkbox").each(function() {
    $(this).click( function() {
        count = recount();
            });
    });
});

function recount() {
    var c = 0;
    var hiddenCount = $('input[type=hidden][name=_chk]');
    if (hiddenCount != null) 
        c = hiddenCount.length;
    var visibleCount = $("[type='checkbox']:checked[name=_chk]");
    if (visibleCount != null) 
        c += visibleCount.length;
    return c;
}

Solution

  • you can select all the items for example

    $("input[type='checkbox']:checked")
    

    and count them:

    $("input[type='checkbox']:checked").length