Add field by clicking button

Asked

Viewed 949 times

3

I am developing a client registration project, in a form using Primefaces I would like to click on a button and with this appear an inputText, if you click again, another appears and so on. This is possible with jsf and primefaces?

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

  • You want to do this dynamically or with a set amount of fields?

  • Whatever, the field limit can be predefined.

2 answers

3


There is no component of Primefaces specific to that.

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

 <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 scope @ViewScoped in the bean):

public void addNaLista() {
    listaDeLogins.add(new Login());
}
  • Exactly what I want. But a nullpointexception is launched on clicking. java.lang.NullPointerException&#xA; at net.youconnection.bean.ClienteBean.addNaLista(ClienteBean.java:61)

  • Did you start the list? List<Login> listDeLogins = new Arraylist<>();

  • I always forget that detail. Thanks @Rafael

1

Let’s assume you have 5 fields and want to display them when you click a button or enter with a shortcut.

In the Bean you make a counter, every click on the increment button + 1. Then on inputText you put a rendered = "bean.contador == 1" and so on. Then make a button that when clicked picks up the counter and decreases 1.

I’d be 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" />

Clientebean:

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 else on a <p:hotkey>.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.