user550738
user550738

Reputation:

PrimeFaces 2, how to use ajax with a h:selectOneBooleanCheckbox?

I have a JSF / PrimeFaces 2.x UI with a check box (h:selectOneBooleanCheckbox) whose value affects other widgets. Something like:

[X] checkbox1
  [____|V] combobox1
  [X] checkbox2

When checkbox1 is false, the selected value for combobox1 must be null, and checkbox2 must also be false.

I'd like to use ajax to set the values in the backing bean on check of checkbox1, but I don't know how to add ajax support for a h:selectOneBooleanCheckbox

Can anyone help? Thanks, Rob

Upvotes: 2

Views: 2950

Answers (1)

BalusC
BalusC

Reputation: 1109875

Nest <f:ajax> with a listener method in checkbox1 which does the desired job and renders the combobox1 and checkbox1.

Something like:

<h:selectBooleanCheckbox value="#{bean.checkbox1}">
    <f:ajax listener="#{bean.listener}" render="combobox1 checkbox2" />
<h:selectBooleanCheckbox>
<h:selectOneMenu id="combobox1" value="#{bean.combobox1}">
    <f:selectItems ... />
</h:selectOneMenu>
<h:selectBooleanCheckbox id="checkbox2" value="#{bean.checkbox2}" />

with

public void listener() {
    if (!checkbox1) {
        combobox1 = null;
        checkbox2 = false;
    }
}

PrimeFaces itself has a <p:ajax> which offers simlilar functionality. It only uses update attribute whereas JSF standard <f:ajax> uses render attribute.

Upvotes: 2

Related Questions