从Backing bean动态地向JSF页面添加一个新的p:SelectOneMenu

时间:2016-05-10 10:24:03

标签: jsf primefaces javabeans selectonemenu

我想在这里动态添加一个新的p:SelectOneMenu到我的panelGrid

<h:panelGrid id="panel_grid" columns="1" style="margin-bottom:10px"cellpadding="5" >

   <p:outputLabel for="firstQuestionDropDown" value={InsererAffaireController.firstQuestion.libeleQuestion}"
   <p:selectOneMenu id="firstQuestionDropDown" value="#{InsererAffaireController.firstQuestion.libeleQuestion}" style="width:150px">
      <p:ajax listener="#{InsererAffaireController.displayNextQuestion()}" update="panel_grid" />
      <f:selectItems value="#{InsererAffaireController.firstQuestion.listLibeleReponses}" />
    </p:selectOneMenu>

            <!-- Here i want to add my new SelectOneMenu -->    
</h:panelGrid>

我设法从托管bean那里做到了,但问题是我无法填充新的SelectOneMenu而且我不知道如何添加f:selectItems到它 这是将新的p:SelectOneMenu添加到面板网格但仍然为空的方法

    public void displayNextQuestion() {
       nextQuestion = getNextQuestion();
       OutputLabel nextQuestionLabelUI = new OutputLabel();
       nextQuestionLabelUI.setValue(nextQuestion.getLibeleQuestion());

       SelectOneMenu nextQuestionDropDown = new SelectOneMenu() ;

       List<SelectItem> selectItems = new ArrayList<>();

       for (String s : nextQuestion.getListLibeleReponses()) {

        selectItems.add(new SelectItem(s, s));
        System.out.println(selectItems.get(0).getLabel());
       }
       // How can i add the selectitems to the SelectOneMenu
       UIComponent panelGrid = findComponent("panel_grid");
       panelGrid.getChildren().add(nextQuestionLabelUI);
       panelGrid.getChildren().add(nextQuestionDropDown) ;
       RequestContext.getCurrentInstance().update("panel_grid");

}

1 个答案:

答案 0 :(得分:1)

您可以像这样使用public T GetValue<T>(object value) { if (value == null || value == DBNull.Value) { return default(T); } else { //return (T)value; return (T)Convert.ChangeType(value, typeof(T)); } } 类:

UISelectItems