How to set defalult selected items in selectOneRadio/selectCheckboxMenu by beans in primefaces

萝らか妹 提交于 2021-02-08 07:55:45

问题


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.

default page display

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!