1

I want to set checkboxes of a selectManyCheckbox to checked if a condition is true "#{myBean.isCondition}":

MyBean.java

public class MyBean{
  private boolean isCondition;
  ...

  public boolean isCondition{
      return isCondition;
  }
  ...
}

my_page.jspx :

<h:selectManyCheckbox 
    layout="pageDirection"
    label="#{messages['numsTelephone.label']}"
    value="#{listSelected}">
    <s:selectItems var="clientTelephone" 
                value="#{list}"
                label="#{clientTelephone}" />
</h:selectManyCheckbox>

I need to know if it exist something like

checked = "#{myBean.isCondition}"

that I can add in my selectManyCheckbox bloc ? Or should I use a javascript ?

Any other solution is welcome too.

My other question is how to set myBean.isCondition to true if a checkbox is selected ?

Thanks,

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Nabil Salah
  • 300
  • 3
  • 7
  • 19

1 Answers1

2

Use <f:ajax> in the "Select all" checkbox and do the selection job in the listener method and then re-render the checkboxes.

Kickoff example:

<h:form>
    <h:selectBooleanCheckbox value="#{bean.selectAll}">
        <f:ajax listener="#{bean.onSelectAll}" render="items" />
    </h:selectBooleanCheckbox>
    <h:selectManyCheckbox id="items" value="#{bean.selectedItems}">
        <f:selectItems value="#{bean.availableItems}" />
    </h:selectManyCheckbox>
</h:form>

@Named
@ViewScoped
public class Bean implements Serializable {

    private boolean selectAll; // +getter +setter
    private List<String> selectedItems; // +getter +setter
    private Map<String, String> availableItems; // +getter (no setter necessary)

    @PostConstruct
    public void init() {
        availableItems = new LinkedHashMap<String, String>();
        availableItems.put("Foo label", "foo");
        availableItems.put("Bar label", "bar");
        availableItems.put("Baz label", "baz");
    }

    public void onSelectAll() {
        selectedItems = selectAll ? new ArrayList<>(availableItems.values()) : null;
    }

    // ...
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555