Is it possible to create multiple selectOneMenu using JSF at run time?
my problem is that i want to create another selectOneMenu each time the user select an item in a previous selectOneMenu.
the first list is loaded when the page loads, here what i've tried:
<h:form id="form">
<p:panel id="panel">
<p:selectOneMenu id="selCaterogy" value="#{connaissance.category}" filter="true" filterMatchMode="startsWith">
<f:selectItem itemLabel="Select une catégorie..." itemValue="" />
<f:selectItems value="#{connaissanceDAO.category}"/>
<p:ajax listener="#{connaissance.addComponent()}"/>
</p:selectOneMenu>
</p:panel>
</h:form>
public void addComponent(){
UIComponent parent = FacesContext.getCurrentInstance().getViewRoot().findComponent("form:panel");
includeCompositeComponent(parent, "http://primefaces.org/ui", "selectOneMenu", "randomID");
System.out.println("Added");
}
the includeCompositeComponent
is gotten from this question
public static void includeCompositeComponent(UIComponent parent, String taglibURI, String tagName, String id) {
FacesContext context = FacesContext.getCurrentInstance();
UIComponent composite = context.getApplication().getViewHandler()
.getViewDeclarationLanguage(context, context.getViewRoot().getViewId())
.createComponent(context, taglibURI, tagName, null);
composite.setId(id);
parent.getChildren().add(composite);
}
but the component isnt getting added. Another thing, if adding components at runtime is possible, can i fill the select menu before i add it to the page?
Using Java EE7, tomcat 8 and primefaces 5.0.