Prime faces,play data from a datatable for a dialog

Asked

Viewed 104 times

0

screen contact

<?xml version="1.0" encoding="utf-8" ?>

<p:growl autoUpdate="true"/> 

    <p:panel header="Formulário" id="panel">
        <h:panelGrid columns="2">
            <h:outputText value="Nome"></h:outputText>
            <p:inputText value="#{ContatoControl.contato.nome }"></p:inputText>
            <h:outputText value="Telefone"></h:outputText>
            <p:inputMask value="#{ContatoControl.contato.telefone }"
                mask="(99) 9999-9999"></p:inputMask>
            <p:commandButton actionListener="#{ContatoControl.confirmar }"
                value="Confirmar" update="contatos, panel" />
        </h:panelGrid>
    </p:panel>

    <p:dataTable id="contatos" value="#{ContatoControl.contatos }"
        rows="10" var="cont" width="100%" emptyMessage="Sem registros"
        editable="true">

        <f:facet name="header">
            <p:outputLabel value="Tabela de Contatos: " />
            <br></br>
            <br></br>
            <p:outputLabel value="Consultar: " />
            <p:inputText id="pesquisar" value="#{ContatoControl.nomeOuTelefone}" />
            <p:commandButton value="Pesquisar" update="contatos"
                actionListener="#{ContatoControl.pesquisar}" />


        </f:facet>
        <p:column sortBy="#{cont.id }">

            <f:facet name="header">
                <h:outputText value="Id"></h:outputText>
            </f:facet>

            <h:outputText value="#{cont.id }"></h:outputText>
        </p:column>

        <p:column sortBy="#{cont.nome }">

            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{cont.nome }"></h:outputText>
                </f:facet>

                <f:facet name="input">
                    <p:inputText value="#{cont.nome }"></p:inputText>
                </f:facet>
            </p:cellEditor>

        </p:column>
         <p:ajax event="rowEdit" listener="#{ContatoControl.modificar(cont)}" update="contatos"/>
        <p:column sortBy="#{cont.telefone }">
            <f:facet name="header">
                <h:outputText value="Telefone"></h:outputText>
            </f:facet>
            <h:outputText value="#{cont.telefone }"></h:outputText>

        </p:column>

        <p:column headerText="Ação">

            <p:commandButton icon="ui-icon-pencil" id="btnEditarProduto"
                actionListener="#{ContatoControl.selecionarContatoParaEdicao(cont)}"
                onclick="PF('dialogNovoContato').show();" />
          <p:commandButton value="PopUp"
              actionListener="#{ContatoControl.selecionarContatoParaEdicao(cont)}"
              onclick="PF(alterarpop).show()" />
            <p:commandButton actionListener="#{ContatoControl.excluir(cont)}"
                icon="ui-icon-trash" title="Excluir" update="contatos" />
        </p:column>
        <p:column>
            <p:rowEditor />
        </p:column>

    </p:dataTable>



</ui:define>

controler

`

package br.com.ambientinformatica.primefaces.controle;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.faces.event.ActionEvent;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import br.com.ambientinformatica.ambientjsf.util.UtilFaces;
import br.com.ambientinformatica.jpa.exception.PersistenciaException;
import br.com.ambientinformatica.primefaces.entidade.Contato;
import br.com.ambientinformatica.primefaces.persistencia.ContatoDao;

@Controller("ContatoControl")
@Scope("conversation")
public class ContatoControl {

    private String nomeOuTelefone;


    private Contato contato = new Contato();

    @Autowired
    private ContatoDao contatoDao;

    private List<Contato> contatos = new ArrayList<Contato>();

    private boolean editMode;

    @PostConstruct
    public void init() {
        listar(null);
    }

    public void confirmar(ActionEvent evt) {
        try {
            contatoDao.alterar(contato);
            listar(evt);
            contato = new Contato();
        } catch (Exception e) {
            UtilFaces.addMensagemFaces(e);
        }
    }

    public void listar(ActionEvent evt) {
        try {
            contatos = contatoDao.listar();
        } catch (Exception e) {
            UtilFaces.addMensagemFaces(e);
        }
    }

    public void pesquisar() {
        contatos = contatoDao.listarPorNomeOuTelefone(nomeOuTelefone);
    }

    public void excluir(Contato contato) {
        try {
            contatoDao.excluirPorId(contato.getId());
            contatos = contatoDao.listar();
            UtilFaces.addMensagemFaces("Contato Excluído com sucesso!");
        } catch (Exception e) {
            UtilFaces.addMensagemFaces(e.getMessage());
        }
    }

public void selecionarContatoParaEdicao(Contato  contato){

        try {
            this.contato = contatoDao.consultar(contato.getId());
        } catch (PersistenciaException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


    }
    // Metodo para alterar o contato//
    // public void alterar(Contato contato) throws PersistenciaException {
    // contatoDao.alterar(contato);
    // }

    public void update() {
        this.editMode = true;
    }
    /// teste para ver se altera pelo table esta funcionando
    public void modificar(Contato com) throws PersistenciaException{
    contatoDao.alterar(com);

    }

    public void listarTodos() throws PersistenciaException {
        contatos = contatoDao.listar();
    }

    public Contato getContato() {
        return contato;
    }

    public void setContato(Contato contato) {
        this.contato = contato;
    }

    public List<Contato> getContatos() {
        return contatos;
    }

    public String getNomeOuTelefone() {
        return nomeOuTelefone;
    }

    public void setNomeOuTelefone(String nomeOuTelefone) {
        this.nomeOuTelefone = nomeOuTelefone;
    }

    public boolean isEditMode() {
        return editMode;
    }

    public void setEditMode(boolean editMode) {
        this.editMode = editMode;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((contato == null) ? 0 : contato.hashCode());
        result = prime * result + ((contatoDao == null) ? 0 : contatoDao.hashCode());
        result = prime * result + ((contatos == null) ? 0 : contatos.hashCode());
        result = prime * result + ((nomeOuTelefone == null) ? 0 : nomeOuTelefone.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        ContatoControl other = (ContatoControl) obj;
        if (contato == null) {
            if (other.contato != null)
                return false;
        } else if (!contato.equals(other.contato))
            return false;
        if (contatoDao == null) {
            if (other.contatoDao != null)
                return false;
        } else if (!contatoDao.equals(other.contatoDao))
            return false;
        if (contatos == null) {
            if (other.contatos != null)
                return false;
        } else if (!contatos.equals(other.contatos))
            return false;
        if (nomeOuTelefone == null) {
            if (other.nomeOuTelefone != null)
                return false;
        } else if (!nomeOuTelefone.equals(other.nomeOuTelefone))
            return false;
        return true;
    }



    @Override
    public String toString() {
        return "ContatoControl [nomeOuTelefone=" + nomeOuTelefone + ", contato=" + contato + ", contatoDao="
                + contatoDao + ", contatos=" + contatos + "]";
    }

}

`

class contact

`

package br.com.ambientinformatica.primefaces.entidade;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

@Entity
public class Contato {

   @Id
   @GeneratedValue(generator="contato_seq", strategy=GenerationType.SEQUENCE)
   @SequenceGenerator(name="contato_seq", sequenceName="contato_seq", allocationSize=1, initialValue=1)
   private Integer id;

   private String nome;

   private String telefone;

   public String getNome() {
      return nome;
   }

   public void setNome(String nome) {
      this.nome = nome;
   }

   public String getTelefone() {
      return telefone;
   }

   public void setTelefone(String telefone) {
      this.telefone = telefone;
   }

   public Integer getId() {
      return id;
   }


}

`

In the highlighted code block and where I have the button to do the action,when I take the "PF" before the alterarpop ,the dialog opens,but when it is there it does not open,I need you to open the dialog with the data line filling the fields to edit and save. who can help me, thank you very much.

1 answer

1


Opa.

Here’s a good example: https://www.primefaces.org/showcase/ui/data/datatable/selection.xhtml, Just take a look at the code you will understand, it’s very simple. Briefly, I noticed that you do not use <f:setPropertyActionListener value="#{value}" target="#{ContatoControl.selectedValue}" /> (exemplo) in your actionButton. This is necessary so that you can set the value that will be sent to the element after the click action (in this case), so that this same value can be recovered later: <p:dialog widgetVar="alterarpop" modal="true" showEffect="fade" hideEffect="fade" resizable="false">. Within the dialog you would use the #{ContatoControl.selectedValue}. Obs.: when using PF(alterarpop).show() what goes in the invocation of the dialog must be between '', in this case 'alterarpop'.

  • Thank you!!!!!!!!

Browser other questions tagged

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