Load data into Inputtext JSF + Primefaces

Asked

Viewed 509 times

0

I have a page that searches for a client and returns a datatable, with this return I would like to select the client that was selected and popular another inputText with the data that were selected in the datatable

my page:

<h:form id="frmCadProduto">
                <p:panel header="Pesquisar Cliente">
                    <p:messages/>
                    <p:panelGrid columns="5" id="panelProduto">
                        <p:outputLabel value="Nome: " />
                        <p:inputText value="#{cadastroClienteBean.cliente.nome}"/>

                        <p:outputLabel value="CPF: " />
                        <p:inputText value="#{cadastroClienteBean.cliente.cpf}"/>
                        <p:commandButton value="Pesquisar" action="#{cadastroClienteBean.pesquisarCliente()}" update=":frmCadProduto:dtCadCliente"/>

                    </p:panelGrid>
                </p:panel>

                <p:panel header="Lista de Cliente Cadastrados">
                    <p:dataTable value="#{cadastroClienteBean.listaCliente}" var="cliente"
                                 selectionMode="single" emptyMessage="Não há registros"
                                 rowKey="#{cliente.idCliente}" id="dtCadCliente"
                                 selection="#{cadastroClienteBean.clienteSelecionado}">
                        <p:column headerText="Nome">
                            <f:facet name="header"/>
                            <h:outputLabel value="#{cliente.nome}"/>
                        </p:column>
                        <p:column headerText="CPF">
                            <f:facet name="header"/>
                            <h:outputLabel value="#{cliente.cpf}"/>
                        </p:column>
                        <p:column headerText="Selecionar">
                            <f:facet name="header"/>
                            <p:commandButton value="Selecionar" action="#{cadastroClienteBean.preencherCampo()}" update=":frmCadProduto"/>
                        </p:column>
                    </p:dataTable>
                </p:panel>


                <p:panel header="Cliente Selecionado" >
                    <p:messages/>
                    <p:panelGrid columns="5" id="panelProduto2">
                        <p:outputLabel value="Nome: " />
                        <p:inputText value="#{cadastroClienteBean.cliente.nome}" /> // aqui que eu gostaria que fosse carregado os dados quando fosse selecionado no commandButton

                        <p:outputLabel value="CPF: " />
                        <p:inputText value="#{cadastroClienteBean.cliente.cpf}"/>
                        <p:commandButton value="Iniciar Compra" />

                    </p:panelGrid>

                </p:panel>

in my customer Managedbean:

@ManagedBean
@SessionScoped
public class CadastroClienteBean {

    private Cliente cliente;
    private Cliente clienteSelecionado;
    private List<Cliente> listaCliente;

    public Cliente getCliente() {
        if(cliente == null) {
            cliente = new Cliente();
        }
        return cliente;
    }

    public void setCliente(Cliente cliente) {
        this.cliente = cliente;
    }

    public Cliente getClienteSelecionado() {
        if(clienteSelecionado == null){
            clienteSelecionado = new Cliente();
        }
        return clienteSelecionado;
    }

    public void setClienteSelecionado(Cliente clienteSelecionado) {
        this.clienteSelecionado = clienteSelecionado;
    }

    public List<Cliente> getListaCliente() {
        if(listaCliente == null){
            ClienteDAO cd = new ClienteDAO();
            listaCliente = cd.listarClientes();
        }
        return listaCliente;
    }

    public void setListaCliente(List<Cliente> listaCliente) {
        this.listaCliente = listaCliente;
    }

    public void preencherCampo(){
        setCliente(getClienteSelecionado());
    }

2 answers

0

Put an id on your dashboard:

<p:panel id="painel" header="Pesquisar Cliente">

And in the update use the following:

<p:commandButton value="Selecionar" action="#{cadastroClienteBean.preencherCampo()}" update=":frmCadProduto:painel"/>

0

It worked perfectly when I include an id for the panel that would be the Selected Client. Now in this panel that has the data I would like to pass the data to a method.

<p:panel header="Cliente Selecionado" id="panelClienteSelecionado">
                    <p:messages/>
                    <p:panelGrid columns="5">
                        <p:outputLabel value="Nome: " />
                        <p:inputText value="#{cadastroClienteBean.cliente.nome}" disabled="true"/>

                        <p:outputLabel value="CPF: " />
                        <p:inputText value="#{cadastroClienteBean.cliente.cpf}" disabled="true"/>
                        <p:commandButton value="Iniciar Compra" action="#{cadastroCompraBean.iniciaCompra()}" update=":frmCadProduto:panelListaCompra"/>

                    </p:panelGrid>

                </p:panel>

                <p:panel id="panelListaCompra" header="Lista de Compras">

                    <p:dataTable value="#{cadastroCompraBean.listaCompra}" var="descricao" emptyMessage="Não ha registros">
                        <p:column headerText="Data" >
                            <f:facet name="header">
                                <h:outputLabel value="Data"/>
                            </f:facet>
                            <h:outputText />
                        </p:column>

                        <p:column headerText="Selecionar" >

                            <p:commandButton value="Selecionar" />        
                        </p:column>

                    </p:dataTable>
                </p:panel>

Within my Registersbuybean has the starting method():

public void iniciaCompra(){
    Compra compra = new Compra();
    Date d = new Date();
    compra.setData(d);
    compra.setIdCliente(getCliente());
    CompraDAO cdo = new CompraDAO();
    cdo.salvar(compra);

    getCompra().getProdutoList().add(produto);

Only when I press the Start Buy button it generates an error:

ERROR: Column 'idCliente' cannot be null Warning: #{registersCompraBean.startCompra()}: org.hibernate.Exception.Constraintviolationexception: could not execute statement javax.faces.Facesexception: #{cadastroCompraBean.iniciaCompra()}: org.hibernate.Exception.Constraintviolationexception: could not execute statement at com.sun.faces.application.Actionlistenerimpl.processAction(Actionlistenerimpl.java:118)

I believe that you are not able to send the Client to the method, now I do not know how to send.

Browser other questions tagged

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