[英]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 我有以下jsf代码
<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: 我的控制器名称是newUser,在我的控制器中,我得到了选中复选框的值,代码如下:
for(String selectedCheckBox: newUserModel.getSelectedCheckBox()){
System.out.println(value of selectckeckBox"+selectedCheckBox);
}
My Bean name is newUserModel ad the declaration is as followas: 我的Bean名称为newUserModel,声明如下:
private List<SelectItem> userRoleComboBox;
private List<String> selectedCheckBox = new ArrayList<String>();
and the getter & setter. 和getter和setter。
You simply have to initialize your selectCheckBox
properly by adding value of each SelectItem
you want to select. 您只需通过添加要选择的每个SelectItem
值来正确初始化selectCheckBox
即可。
Example : 范例:
// Assuming your userRoleComboBox is already initialized
this.selectedCheckBox.add((String)this.userRoleComboBox.get(0).getValue());
This will pre-select the first choice. 这将预选第一个选择。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.