Reputation: 1887
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.
Upvotes: 0
Views: 1018
Reputation: 1749
If your goal is to only create multiple SelectOneMenue then just use <ui:repeat>
and include your components inside it.
This way you can just items to the list binded to <ui:repeat>
and set the values you want to those items and it will render to the page.
<ui:repeat value="#{bean.list}" var="item">
<h:selectOneMenu value="#{item.value}" >
<h:selectItem itemLabel="..." itemValue="..."></h:selectItem>
</h:selectOneMenu>
</ui:repeat>
In your bean:
public void change(ValueChangeEvent e){
theList.add(new SomeObject("value"))
}
Upvotes: 1
Reputation: 2052
With the Use of Datatable you can add as many row(selectOneMenu) as you want on page.
Out side the table add one button which can add/remove row.
<p:dataTable resizableColumns="true"
var="sampleDesc" id="SampleDescTable" rowIndexVar="rowIndex"
value="#{sampleBean.sampleDescList.list}"
rendered="#{not empty sampleBean.sampleDescList.list}">
<p:column>
<h:outputLabel value="#{sampleDesc.sampleDescText}"/>
</p:column>
<p:column>
<h:selectOneMenu required="#{not empty sampleBean.sampleDescList.list}" converter="#{sampleDescValueConverter}"
id="SampleDescValue" value="#{sampleBean.selectedSampleDescList[rowIndex]}">
<f:selectItem itemLabel="Select One" itemValue="#{null}"/>
<f:selectItems value="#{sampleDesc.sampleDescValues}" var="sdv"
itemLabel="#{sdv.sampleDescValuesText}" itemValue="#{sdv}" />
</h:selectOneMenu>
</p:column>
</p:dataTable>
related Link on SO
Upvotes: 0