vijayk
vijayk

Reputation: 2753

How to set selectManyCheckbox value?

My problem is I can't set the default values when I visit the page first time.
My code is run very well.But my requirement is when I am visit the page first time the first checkBox is default selected.
I have the following jsf code

<h:selectManyCheckbox value="#{newUser.newUserModel.selectedCheckBox}" layout="lineDirection"
                      onclick="document.getElementById('addNewUserForm:checkBox').click();">
    <f:selectItems value="#{newUser.newUserModel.userRoleComboBox}" />
    <f:ajax />
</h:selectManyCheckbox>

My Controller name is newUser and in my controller I got the values of selected checkBox.And the code is follows:

for(String selectedCheckBox: newUserModel.getSelectedCheckBox()){
    System.out.println(value of selectckeckBox"+selectedCheckBox);
} 

My Bean name is newUserModel ad the declaration is as followas:

private List<SelectItem> userRoleComboBox;
private List<String> selectedCheckBox = new ArrayList<String>();

and the getter & setter.

Upvotes: 0

Views: 1738

Answers (1)

Alexandre Lavoie
Alexandre Lavoie

Reputation: 8771

You simply have to initialize your selectCheckBox properly by adding value of each SelectItem you want to select.

Example :

// Assuming your userRoleComboBox is already initialized
this.selectedCheckBox.add((String)this.userRoleComboBox.get(0).getValue());

This will pre-select the first choice.

Upvotes: 1

Related Questions