How can I add/ remove the primefaces inputText dynamically?
3
votes
It surprised me that you accepted the answer of Michel, I had a completely different functional requirement in mind. Michel's solution would require a fixed amount of prepared inputs in the view, while you seem to want let the user add/remove an undetermined amount dynamically.
- BalusC
Agreeing with you.Since I was new to this , I thought it would work. But it will work only if we have a fixed amount of prepared inputs as you said..But I made some workaround and , prepared it working without using rendered.
- Nidheesh
Then you should repost it as an answer in detail and accept yourself.
- BalusC
2 Answers
2
votes
To add/remove textboxes, try the following snippets.
<h:panelGrid columns="1" cellpadding="10">
<h:commandButton value="+" action="#{contactBean.addPhone}"
image="../images/addbtn.png" />
<p:dataTable border="0" value="#{contactBean.phoneNos}" var="p"
rowIndexVar="rowIndex" emptyMessage="No phone numbers entered">
<p:column>
<h:selectOneMenu id="extraTask1" value="#{p.phoneType}">
<f:selectItem itemLabel="Select" itemValue="" />
<f:selectItem itemLabel="Mobile" itemValue="Mobile" />
<f:selectItem itemLabel="Work" itemValue="Work" />
<f:selectItem itemLabel="Others" itemValue="Others" />
</h:selectOneMenu>
</p:column>
<p:column>
<p:inputText value="#{p.phoneNo}" />
</p:column>
<p:column>
<h:commandButton value="remove" image="../images/button_remove.gif"
actionListener="#{contactBean.removePhone}">
<f:param name="columnToRemove" value="#{rowIndex}" />
</h:commandButton>
</p:column>
</p:dataTable>
</h:panelGrid>