I have two kinds of check boxes. One is a simple check box e.g t:selectBooleanCheckbox
and another is a dynamically generated list of t:selectBooleanCheckbox
. I want to control this list with the single check box. E.g. when it is selected or deselected, similar action should take place for the list as well.

- 1,082,665
- 372
- 3,610
- 3,555

- 11
- 1
- 1
- 2
1 Answers
With JSF 1.x there are two ways to achieve this.
Submit the form to the server onclick of the checkbox.
<t:selectBooleanCheckbox value="#{bean.selectAll}" onclick="submit()" />
And just do the logic to select/unselect all checkboxes.
public void submit() { for (CheckboxItem item : checkboxItems) { item.setSelected(selectAll); } }
Caveat: not user and developer friendly. User sees flash of content and might have to wait some time before page returns. Developer has to retain all inputs of the same form and hassle with JSF validation, if any.
Use JavaScript to do the magic entirely at the client side.
<t:selectBooleanCheckbox value="#{bean.selectAll}" onclick="selectAll(this)" />
with
function selectAll(checkbox) { var elements = checkbox.form.elements; for (var i = 0; i < elements.length; i++) { var element = elements[i]; if (/checkboxId$/.test(element.id)) { element.checked = checkbox.checked; } } }
This however assumes that all checkbox elements generated by your dynamic list have an ID ending with
checkboxId
. Just check the generated HTML source to spot the pattern. You at least know in which direction you should look now.
Oh, don't forget to write similar logic to unselect the "select all" checkbox whenever one of the checkboxes in the list is unchecked.
With JSF 2.x, you'd of course use <f:ajax>
instead of onclick
.

- 1,082,665
- 372
- 3,610
- 3,555
-
how to get rid of this error The content of elements must consist of well-formed character data or markup it appears as my .seam page is not working with java script's logical operators – user352894 Jun 17 '10 at 12:22
-
Put JavaScript in its own `file.js` file and import it by ``. Don't clutter XML/XHTML page with raw JavaScript code, you'll get this kind of errors. You can solve it by putting JS in `<![CDATA[` block, but that's plain nasty. – BalusC Jul 01 '10 at 12:55
-
The first method given in your answer doesn't work without including the code given in this answer by Adam: http://stackoverflow.com/questions/3942060/select-all-checkbox-in-jsf-without-using-javascript?rq=1 – A_J Nov 21 '15 at 03:22
-
@A_J: Then you're doing it in the wrong place (a value change listener instead of an action listener, apparently). That answer basically turns the value change listener to an action listener. This doesn't make sense. Just use an action listener in first place. – BalusC Nov 21 '15 at 09:33
-
How to use action Listener with a selectBooleancheckbox ? The attribute is not available in that tag in JSF. May be you are suggesting to use richfaces or another third party framework. – A_J Nov 21 '15 at 13:18
-
Thanks for the clarification :) – A_J Nov 22 '15 at 13:19