Load data in InputText JSF + Primefaces

0

I have a page that does the search for a client and returns a datatable, with this return I would 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 Managed 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());
    }
    
asked by anonymous 27.03.2018 / 20:39

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"/>
    
27.03.2018 / 21:00
0

It worked perfectly when I included an id for the panel that would be the Selected Client. Now in that 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 CadastroCompraBean, you have the startCompra () 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);

But when I click on the Buy button, it generates an error:

  

ERROR: Column 'CustomerID' can not be null   Warning: # {cadastroCompraBean.inpp ()}: org.hibernate.exception.ConstraintViolationException: could not execute statement   javax.faces.FacesException: # {cadastroCompraBean.inpp ()}: 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 it.

    
28.03.2018 / 20:18