commandButton isn't working in a p:dialog

UI Components for JSF
Post Reply
Sharananannias
Posts: 1
Joined: 10 Oct 2016, 18:20

10 Oct 2016, 19:11

Hi!
I'm trying to add an a List a new object when I do click in a commandButton, but It doesn't work, instead, it reloads again de List in the datatable calling the method getList. My code:

Code: Select all

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <ui:composition>
        <h:form id="DepartamentoCreateForm">
            <p:dialog id="DepartamentoCreateDlg" widgetVar="DepartamentoCreateDialog" modal="true" resizable="false" appendTo="@(body)" header="#{bundle.CreateDepartamentoTitle}">
            
                <h:panelGroup id="display">
                    <h:panelGrid columns="2" rendered="#{departamentoController.selected != null}">
                        <p:outputLabel value="#{bundle.CreateDepartamentoLabel_dcodigo}" for="dcodigo" />
                        <p:inputText id="dcodigo" title="#{bundle.CreateDepartamentoTitle_dcodigo}" required="true" requiredMessage="#{bundle.CreateDepartamentoRequiredMessage_dcodigo}">
                            <f:attribute name="departamento" value="#{departamentoController.selected.dcodigo}" />
                        </p:inputText>
                        <p:outputLabel value="#{bundle.CreateDepartamentoLabel_dnombre}" for="dnombre" />
                        <p:inputText id="dnombre" value="#{departamentoController.selected.dnombre}" title="#{bundle.CreateDepartamentoTitle_dnombre}" />
                        
                        <p:outputLabel value="#{bundle.CreateCiudadLabel_ccodigo}" for="ccodigo" />
                        <p:inputText id="ccodigo" value="#{ciudadController.selected.ccodigo}" title="#{bundle.CreateCiudadTitle_ccodigo}" required="true" requiredMessage="#{bundle.CreateCiudadRequiredMessage_ccodigo}"/>
                        
                        <p:outputLabel value="#{bundle.CreateCiudadLabel_cnombre}" for="cnombre" />
                        <p:inputText id="cnombre" value="#{ciudadController.selected.cnombre}" title="#{bundle.CreateCiudadTitle_cnombre}" required="true" requiredMessage="#{bundle.CreateCiudadRequiredMessage_cnombre}"/>
                        
                        <h:inputHidden binding="#{departamento}" value="#{ciudadController.selected.cdepartamento}" />
                    </h:panelGrid>    
                    <p:commandButton value="Add" immediate="true" action="#{ciudadController.addCiudad}" update="DepartamentoCreateForm:tableCiudad"/>
                        
                    <p:dataTable id="tableCiudad" value="#{ciudadController.list}" var="ciudad" scrollable="true" scrollHeight="150" >
                        <p:column width="50" headerText="Ciudad">
                            <p:inputText value="#{ciudad.ccodigo}" />
                        </p:column>
                        <p:column width="50" headerText="Nombre">
                            <p:inputText value="#{ciudad.cnombre}" />
                        </p:column>
                        <p:column width="50" headerText="Action">
                            <p:commandButton value="Delete"  actionListener="#{ciudadController.removeCiudad(ciudad)}" />
                        </p:column>
                    </p:dataTable>
                    <p:commandButton actionListener="#{departamentoController.create}" value="#{bundle.Save}" update="display,:DepartamentoListForm:datalist,:growl" oncomplete="handleSubmit(args,'DepartamentoCreateDialog');"/>
                    <p:commandButton value="#{bundle.Cancel}" onclick="DepartamentoCreateDialog.hide()"/>
                </h:panelGroup>
               
            </p:dialog>
        </h:form>
    </ui:composition>
</html>

Code: Select all

package com.herrajesoskar.Controller;

import com.herrajesoskar.Entities.Ciudad;
import com.herrajesoskar.Controller.util.JsfUtil;
import com.herrajesoskar.Controller.util.JsfUtil.PersistAction;
import com.herrajesoskar.Bean.CiudadFacade;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.ejb.EJBException;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

@Named("ciudadController")
@SessionScoped
public class CiudadController implements Serializable {

    @EJB
    private com.herrajesoskar.Bean.CiudadFacade ejbFacade;
    private List<Ciudad> items = null;
    private List<Ciudad> List = new ArrayList<>();
    private Ciudad selected;

    public CiudadController() {
    }

    public Ciudad getSelected() {
        return selected;
    }

    public void setSelected(Ciudad selected) {
        this.selected = selected;
    }

    protected void setEmbeddableKeys() {
    }

    protected void initializeEmbeddableKey() {
    }

    private CiudadFacade getFacade() {
        return ejbFacade;
    }

    public Ciudad prepareCreate() {
        selected = new Ciudad();
        initializeEmbeddableKey();
        return selected;
    }

    public void create() {
        persist(PersistAction.CREATE, ResourceBundle.getBundle("/Bundle").getString("CiudadCreated"));
        if (!JsfUtil.isValidationFailed()) {
            items = null;    // Invalidate list of items to trigger re-query.
        }
    }

    public void update() {
        persist(PersistAction.UPDATE, ResourceBundle.getBundle("/Bundle").getString("CiudadUpdated"));
    }

    public void destroy() {
        persist(PersistAction.DELETE, ResourceBundle.getBundle("/Bundle").getString("CiudadDeleted"));
        if (!JsfUtil.isValidationFailed()) {
            selected = null; // Remove selection
            items = null;    // Invalidate list of items to trigger re-query.
        }
    }

    public List<Ciudad> getItems() {
        if (items == null) {
            items = getFacade().findAll();
        }
        return items;
    }

    private void persist(PersistAction persistAction, String successMessage) {
        if (selected != null) {
            setEmbeddableKeys();
            try {
                if (persistAction != PersistAction.DELETE) {
                    getFacade().edit(selected);
                } else {
                    getFacade().remove(selected);
                }
                JsfUtil.addSuccessMessage(successMessage);
            } catch (EJBException ex) {
                String msg = "";
                Throwable cause = ex.getCause();
                if (cause != null) {
                    msg = cause.getLocalizedMessage();
                }
                if (msg.length() > 0) {
                    JsfUtil.addErrorMessage(msg);
                } else {
                    JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
                }
            } catch (Exception ex) {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
                JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/Bundle").getString("PersistenceErrorOccured"));
            }
        }
    }

    public Ciudad getCiudad(java.lang.Integer id) {
        return getFacade().find(id);
    }

    public List<Ciudad> getItemsAvailableSelectMany() {
        return getFacade().findAll();
    }

    public List<Ciudad> getItemsAvailableSelectOne() {
        return getFacade().findAll();
    }

    @FacesConverter(forClass = Ciudad.class)
    public static class CiudadControllerConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
            if (value == null || value.length() == 0) {
                return null;
            }
            CiudadController controller = (CiudadController) facesContext.getApplication().getELResolver().
                    getValue(facesContext.getELContext(), null, "ciudadController");
            return controller.getCiudad(getKey(value));
        }

        java.lang.Integer getKey(String value) {
            java.lang.Integer key;
            key = Integer.valueOf(value);
            return key;
        }

        String getStringKey(java.lang.Integer value) {
            StringBuilder sb = new StringBuilder();
            sb.append(value);
            return sb.toString();
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent component, Object object) {
            if (object == null) {
                return null;
            }
            if (object instanceof Ciudad) {
                Ciudad o = (Ciudad) object;
                return getStringKey(o.getCcodigo());
            } else {
                Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, "object {0} is of type {1}; expected type: {2}", new Object[]{object, object.getClass().getName(), Ciudad.class.getName()});
                return null;
            }
        }

    }

    public List<Ciudad> getList() {
        return List;
    }    

    public void setList(List<Ciudad> List) {
        this.List = List;
    }
    
    public void addCiudad(){
        List.add(this.selected);
    }
    
    public void removeCiudad(Ciudad ciudad){
        List.remove(ciudad);
    }
}

Thanks

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 40 guests