3

I know there are lot of examples here that shows how to count the checked checkboxes but for some reason I'm unable to make this work.

What I'm trying to do is that when at least one checkbox in my page is checked a button should be enable or disable if none of the checkboxes are checked.

The thing is even that I implemented the following code, the count of checked checkboxes is always 0, I'm not sure what I'm missing.

Primefaces component:

<p:column id="idSelectBox" selectionMode="multiple" style="width:68px"  />

JQuery Code:

function countChecked() {   
    var n = $("input:checkbox:checked").length;   
    alert('Count: ' + n);
} 

$(":checkbox").click(countChecked);

Hope you can help me out!

UPDATE 1:

I did a little more research and the Primefaces component does not render to HTML checkboxes elements, its outputs is a set of divs and classes:

<td class="ui-selection-column">
 <div class="ui-dt-c">
  <div class="ui-radiobutton ui-widget">
   <div class="ui-radiobutton-box ui-widget ui-corner-all ui-radiobutton-relative ui-state-default">
    <span class="ui-radiobutton-icon"></span>
   </div>
  </div>
 </div>
</td>

So I'm still trying to figure out how I can detect whether the checkbox was checked or not.

Night Elve
  • 217
  • 1
  • 3
  • 11

2 Answers2

3

PrimeFaces 3.1 datatable client side api has getSelectedRowsCount() method you can use to see if there are any selected rows.

Cagatay Civici
  • 6,406
  • 1
  • 29
  • 34
  • Hello, I went to PrimeFaces site & downloaded **primefaces-3.1-SNAPSHOT.jar**, but I didn't find any documentation regarding how to call this method, I just need to call it in a JS script to do some validations. Could you point me in the right direction please? Thank you man! – Night Elve Jan 26 '12 at 15:53
  • I'm trying the following code where dataTable is the widgetVar name: `function countCheckboxMarked(dataTable) { var count = dataTable.getSelectedRowsCount; alert(count); }` .. The alert output is: `function(){return this.isSelectionEnabled()?this.selection.length:0}` .. Which I guess is just printing the function body. What am I missing? – Night Elve Jan 26 '12 at 17:50
  • **UPDATE:** Totally missed the lack of constructor in my call, it should be: `dataTable.getSelectedRowsCount();` .. I'm not sure why overlooked that. Thanks man! – Night Elve Jan 26 '12 at 18:10
-1

you have to use the attribute selector see http://api.jquery.com/category/selectors/attribute-selectors/

var n = $("input[type=checkbox][checked]").length;
Ingemar
  • 1,638
  • 2
  • 12
  • 15
  • 1
    This doesn't apply, as PrimeFaces does not render checkbox element for column selection. Checkbox UI has two divs and span with image for themed look. – Cagatay Civici Jan 26 '12 at 09:42