问题
I want to create selectOneRadio
list and selectCheckboxMenu
which items get from related bean objects.
For non selected list it is working well.But how can i provide these lists with some item(s) selected by default.
My current selectCheckboxMenu
code is like this.
<p:selectCheckboxMenu id="trdays"
value="#{mybean.selectedDay}"
label="Select Days">
<f:selectItems value="#{mybean.dayList}" var="day"
itemValue="#{day.value}" itemLabel="#{day.name}"/>
</p:selectCheckboxMenu>
it should look like this when page loaded and user haven't done anything yet.
EDIT Day Class :-
public class Day{
private String name;
private String value;
//getters and setters
}
回答1:
Value attribute of selectCheckboxMenu component should get the your default values. On bean side you should write a getter for selectedDayList and all daylist.
Example:
@PostConstruct
public void init() {
dayList= new ArrayList<String>();
dayList.add("Mon");
dayList.add("Tue");
dayList.add("Wed");
dayList.add("Thu");
dayList.add("Fri");
dayList.add("Sat");
dayList.add("Sun");
selectedDayList= new ArrayList<String>();
selectedDayList.add("Tue");
selectedDayList.add("Wed");
}
public List<String> getDayList()
{
return dayList;
}
public List<String> getSelectedDaylist()
{
return selectedDayList;
}
.xhtml page should be like this.
<p:selectCheckboxMenu id="trdays"
value="#{mybean.selectedDaylist}"
label="Select Days">
<f:selectItems value="#{mybean.dayList}" var="day"
itemValue="#{day.value}" itemLabel="#{day.name}"/>
</p:selectCheckboxMenu>
Good Luck!
回答2:
Just add default values in the selectedDay
list (or array) in init method (with @PostConstruct
annotation). These values should have same value as corresponding itemValue
attribute (in your case this is day.value
).
来源:https://stackoverflow.com/questions/14827677/how-to-set-defalult-selected-items-in-selectoneradio-selectcheckboxmenu-by-beans