Add field when clicking button

3

I am developing a customer registration project, in a form using PrimeFaces I would like to click on a button and with this make an inputText appear, if I click again, another appears and so on. Is this possible with jsf and primefaces?

The idea is that this field added corresponds to an object of type Login that is related to @OneToMany of the Client object

    
asked by anonymous 21.09.2015 / 21:41

2 answers

3

There is no specific component of Primefaces for this.

One way to do this is to use the <p:dataTable> component:

 <p:dataTable id="tabelaLogin" value="#{seu_bean.listaDeLogins}" var="login">
        <p:column headerText="Login">
               <h:inputText value="#{login.username}"/>
         </p:column>
   </p:dataTable>
<p:commandButton value="Add Login" actionListener="#{seu_bean.addNaLista()}" process="@this" update="tabelaLogin"/>

In your Bean (it is recommended to use the @ViewScoped scope in the bean):

public void addNaLista() {
    listaDeLogins.add(new Login());
}
    
21.09.2015 / 22:35
1

Let's suppose you have 5 fields and want to display them when you click a button or enter a shortcut.

No Bean you make a counter, every click on the button increments + 1. Then on inputText you put a rendered = "bean.contador == 1" and so on. Then it makes a button that when clicked takes the counter and decreases 1.

It would look like this:

<p:inputText  size="40" rendered="ClienteBean.contador == 1" />
<p:inputText  size="40" rendered="ClienteBean.contador == 2" />
<p:inputText  size="40" rendered="ClienteBean.contador == 3" />

ClientBean:

private Integer contador=0;

//Get and Set's

public void adicionaCampo(){
    contador++;
    //Adiciona elemento na lista
}

public void removeCampo(){
    contador--;
    //Remove elemento da lista
}

Then just call these methods on a button or a <p:hotkey> .

    
21.09.2015 / 22:49