0

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;
}
rcheuk
  • 1,140
  • 1
  • 12
  • 32

1 Answers1

0

you can select all the items for example

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

and count them:

$("input[type='checkbox']:checked").length
carlituxman
  • 1,201
  • 12
  • 22
  • how would i do this when the user selects a new page? is there a paging event I have to listen to? – rcheuk Jul 08 '13 at 15:58
  • you can use different selectors for every page for example, or try using .is(":visible") – carlituxman Jul 08 '13 at 16:06
  • will this be able to keep the count across all pages? – rcheuk Jul 08 '13 at 16:21
  • Thanks for the pointer. It helped me solve my problem. :) Will post solution in my post. – rcheuk Jul 08 '13 at 19:07
  • are you using jquery datatables? – carlituxman Jul 09 '13 at 17:20
  • I am getting data through a request with the displaytable library. – rcheuk Jul 09 '13 at 17:23
  • your post above helped quite a lot. :) I don't plan to introduce new libraries though, since the rest of the application currently uses display tables for less complicated functions, and what I have functioning now will suffice. the main feature im trying to add would be nice to have but not required - updating # of selected checkboxes with each page change. – rcheuk Jul 09 '13 at 17:56