3

How can I add/ remove the primefaces inputText dynamically?

Nidheesh
  • 4,390
  • 29
  • 87
  • 150
  • 1
    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 Nov 08 '11 at 12:52
  • 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 Nov 09 '11 at 04:36
  • 1
    Then you should repost it as an answer in detail and accept yourself. – BalusC Nov 09 '11 at 11:26

2 Answers2

2

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>
Nidheesh
  • 4,390
  • 29
  • 87
  • 150
  • I'm using `p:dataGrid` to solve similar problem, but components inside it have the same ids. Did you have the same problems? What is the reason for using `p:dataTable`? Thanks in advance – nikagra Nov 21 '11 at 19:11
  • I didn't have that problem. Currently all the values get posted to the list.I haven't tried with `p:dataGrid`,anyway. – Nidheesh Nov 22 '11 at 11:45
  • When using the dataTable, all the data are processed by a list. And because it's easier to update the dataTable component of primeFaces, which offer the possibility of some dynamic component. – bilelovitch Jan 06 '16 at 11:30
-1

This is the easy mode:

<h:inputText rendered="#{object.visibile}" /> 

if object.visibile == true the inputText is visible.

Michel Foucault
  • 1,724
  • 3
  • 25
  • 48