Page 1 of 1

dataTable - refresh after insert record.

Posted: 30 Nov 2010, 17:31
by jlferreira
Hi all.
I have an In-cell editin dataTable and I want to add a "insert" button.
When I press this button a new dialog is show.
In this dialog I have a "Save" button.
When I press this button, the information in this form is passed to a bean and inserted in the database using JPA.
The problem is that when I press the Save button, the dialog is closed, but the information in the datatable are not updated.
If I press ctrl+F5, I can see the new register as a row, but it I press the add button again, the dialog is opened with the information that I provided before.
May someone help me?
This is the .xhtml page:

Code: Select all

<!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:h="http://java.sun.com/jsf/html"
	xmlns:f="http://java.sun.com/jsf/core"
	xmlns:p="http://primefaces.prime.com.tr/ui"
	xmlns:ui="http://java.sun.com/jsf/facelets"
	xmlns:c="http://java.sun.com/jsp/jstl/core">
	<f:view>
		<script type="text/javascript">
			function handleConfirmaRequest(xhr, status, args) {
				if(args.validationFailed || !args.loggedIn) {
					jQuery('#dlg_ins_seg_grupos').effect("shake", { times:3 }, 100);
				} else {
					dlg_ins_seg_grupos.hide();
//					jQuery('#loginLink').fadeOut();
				}
			}
		</script>
			<ui:insert name="padraocabecalho">
				<ui:include src="/resources/templates/padraocabecalhopfp.xhtml" />
			</ui:insert>
			<ui:insert name="menubar">
				<ui:include src="/resources/templates/menubar.xhtml" />
			</ui:insert>
		<h:form id="form_gruposseguranca">
			<p:growl id="growl" showDetail="true" sticky="true" rendered="true"/>
		    <p:dataTable id="segGruposList" var="c" value="#{segGruposController.listaobj}"
		    				rowEditListener="#{segGruposController.dataTableOnRowEdit}"
		    				paginator="true" rows="10"
		    				paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
		    				rowsPerPageTemplate="10,20,40" >
				<f:facet name="header">
					<table width="100%" cellpadding="0" cellspacing="0" border="0">
						<tr>
							<td align="center" width="100%">
								<h:outputText value="Grupos de Segurança"/>
							</td>
							<td align="right">
								<p:commandButton image="#{request.contextPath}/resources/images/add.png" onclick="dlg_ins_seg_grupos.show()" />
							</td>
						</tr>
					</table>
				</f:facet>  

				<p:column headerText="Código" style="width:350px">  
					<p:cellEditor>  
						<f:facet name="output">  
							<h:outputText value="#{c.segGruposCodigo}" />  
						</f:facet>  
						<f:facet name="input">  
							<h:inputText value="#{c.segGruposCodigo}" disabled="true" readonly="true" style="width:100%"/>  
						</f:facet>  
					</p:cellEditor>  
				</p:column>  

				<p:column headerText="Descrição" style="width:350px">  
					<p:cellEditor>
						<f:facet name="output">  
							<h:outputText value="#{c.segGruposDescri}" />  
						</f:facet>  
						<f:facet name="input">  
							<h:inputText value="#{c.segGruposDescri}" style="width:100%"/>
						</f:facet>
					</p:cellEditor>  
				</p:column>  
				<p:column headerText="Opções">
					<p:rowEditor />
				</p:column>  
		    </p:dataTable>
		    
			
		</h:form>
			<p:growl id="growl2" showDetail="true" life="3000" />
			<p:dialog id="Ldlg_ins_seg_grupos" header="Login" widgetVar="dlg_ins_seg_grupos" modal="false" height="180">
				<h:form>
			
					<h:panelGrid columns="2" cellpadding="5">
						
						<h:outputLabel for="descricao" value="Descrição do Grupo: " />
						<h:inputText value="#{segGruposController.classeobj.segGruposDescri}" 
								id="descricao" required="true" label="descricao" />
						
						<f:facet name="footer">  
							<p:commandButton value="Salvar" style="text-align:left"    
								action="#{segGruposController.salvar}"
								oncomplete="dlg_ins_seg_grupos.hide();" update="segGruposList" />  
						</f:facet>
					</h:panelGrid>  
				</h:form>  
			</p:dialog>  
	</f:view>
</html>
This is the SegGruposController.java:

Code: Select all

package br.com.sitic.sitic.seguranca.factory.backing;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import org.primefaces.event.RowEditEvent;

import br.com.sitic.sitic.seguranca.action.SegGruposDAO;
import br.com.sitic.sitic.sistema.model.SegGrupos;

public class SegGruposController implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private SegGrupos classeobj;
	private List<SegGrupos>	listaobj;
	private Integer 		seggruposcodigo;
	@SuppressWarnings("rawtypes")
	private DataModel model;

	public SegGruposController(){
		classeobj = new SegGrupos();
		listaobj = new ArrayList<SegGrupos>();
		popularListaobj(listaobj);
//		SegGruposSmall = new ArrayList<SegGrupos>();

//		classeobj = new SegGrupos();
	}
	
	public Integer getSeggruposcodigo() {
		return seggruposcodigo;
	}
	public void setSeggruposcodigo(Integer seggruposcodigo) {
		this.seggruposcodigo = seggruposcodigo;
	}
	
	@SuppressWarnings("unchecked")
	private void popularListaobj(List<SegGrupos> plistaobj){
		SegGruposDAO objdao = new SegGruposDAO();
		plistaobj.addAll(objdao.exibir());

	}
	
	public List<SegGrupos> getListaobj(){
		return listaobj;
	}
	
	public SegGrupos getClasseobj(){
		return classeobj;
	}
	
	public void setClasseobj(SegGrupos classeobj){
		this.classeobj = classeobj;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public DataModel getModel(){
		SegGruposDAO objdao = new SegGruposDAO();
		model = new ListDataModel(objdao.exibir());
		return model;
	}
		
	public int getCount(){
		SegGruposDAO objdao = new SegGruposDAO();
		return objdao.exibir().size();
	}

	public String adicionar(SegGrupos classeobj) {
		this.classeobj = classeobj;
		return "Adicionar";
	}

	public String deletar(SegGrupos classeobj){
		SegGruposDAO objdao = new SegGruposDAO();
		objdao.deletar(classeobj);
		return "Deletar";
	}

	public String salvar() {
//		System.out.println("getSeggrupos().getSegGruposCodigo(): "+getSeggrupos().getSegGruposCodigo());
		SegGruposDAO objdao = new SegGruposDAO();
		if (classeobj.getSegGruposCodigo() == null) {
			objdao.adicinar(classeobj);
		}
		else {
			objdao.alterar(classeobj);
		}
		return "Salvar";
	}

	public void dataTableOnRowEdit(RowEditEvent event){
		SegGruposDAO objdao = new SegGruposDAO();
		SegGrupos sclasseobj =(SegGrupos) event.getObject();
		System.out.println("Codigo: "+sclasseobj.getSegGruposCodigo()+", Descrição: "+sclasseobj.getSegGruposDescri());
		objdao.alterar(sclasseobj);
	}
}

Re: dataTable - refresh after insert record.

Posted: 30 Nov 2010, 18:23
by kwintesencja
Could you try this?

Code: Select all

     <!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:h="http://java.sun.com/jsf/html"
   xmlns:f="http://java.sun.com/jsf/core"
   xmlns:p="http://primefaces.prime.com.tr/ui"
   xmlns:ui="http://java.sun.com/jsf/facelets"
   xmlns:c="http://java.sun.com/jsp/jstl/core">
   <f:view>
      <script type="text/javascript">
         function handleConfirmaRequest(xhr, status, args) {
            if(args.validationFailed || !args.loggedIn) {
               jQuery('#dlg_ins_seg_grupos').effect("shake", { times:3 }, 100);
            } else {
               dlg_ins_seg_grupos.hide();
//               jQuery('#loginLink').fadeOut();
            }
         }
      </script>
         <ui:insert name="padraocabecalho">
            <ui:include src="/resources/templates/padraocabecalhopfp.xhtml" />
         </ui:insert>
         <ui:insert name="menubar">
            <ui:include src="/resources/templates/menubar.xhtml" />
         </ui:insert>
      <h:form id="form_gruposseguranca">
         <p:growl id="growl" showDetail="true" sticky="true" rendered="true"/>
         <p:outputPanel id="table_panel">
             
          <p:dataTable id="segGruposList" var="c" value="#{segGruposController.listaobj}"
                      rowEditListener="#{segGruposController.dataTableOnRowEdit}"
                      paginator="true" rows="10"
                      paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
                      rowsPerPageTemplate="10,20,40" >
            <f:facet name="header">
               <table width="100%" cellpadding="0" cellspacing="0" border="0">
                  <tr>
                     <td align="center" width="100%">
                        <h:outputText value="Grupos de Segurança"/>
                     </td>
                     <td align="right">
                         <p:commandButton image="#{request.contextPath}/resources/images/add.png" update="dlg_form:dlg_panel" oncomplete="dlg_ins_seg_grupos.show()" />
                     </td>
                  </tr>
               </table>
            </f:facet> 

            <p:column headerText="Código" style="width:350px"> 
               <p:cellEditor> 
                  <f:facet name="output"> 
                     <h:outputText value="#{c.segGruposCodigo}" /> 
                  </f:facet> 
                  <f:facet name="input"> 
                     <h:inputText value="#{c.segGruposCodigo}" disabled="true" readonly="true" style="width:100%"/> 
                  </f:facet> 
               </p:cellEditor> 
            </p:column> 

            <p:column headerText="Descrição" style="width:350px"> 
               <p:cellEditor>
                  <f:facet name="output"> 
                     <h:outputText value="#{c.segGruposDescri}" /> 
                  </f:facet> 
                  <f:facet name="input"> 
                     <h:inputText value="#{c.segGruposDescri}" style="width:100%"/>
                  </f:facet>
               </p:cellEditor> 
            </p:column> 
            <p:column headerText="Opções">
               <p:rowEditor />
            </p:column> 
          </p:dataTable>
         </p:outputPanel>
      </h:form>
         <p:growl id="growl2" showDetail="true" life="3000" />
         <p:dialog id="Ldlg_ins_seg_grupos" header="Login" widgetVar="dlg_ins_seg_grupos" modal="false" height="180">
             <h:form id="dlg_form" >
                 <p:outputPanel id="dlg_panel">
               <h:panelGrid columns="2" cellpadding="5">
                  
                  <h:outputLabel for="descricao" value="Descrição do Grupo: " />
                  <h:inputText value="#{segGruposController.classeobj.segGruposDescri}"
                        id="descricao" required="true" label="descricao" />
                  
                  <f:facet name="footer"> 
                     <p:commandButton value="Salvar" style="text-align:left"   
                        action="#{segGruposController.salvar}"
                        oncomplete="dlg_ins_seg_grupos.hide();" update="form_gruposseguranca:table_panel" /> 
                  </f:facet>
               </h:panelGrid> 
                       </p:outputPanel>
            </h:form> 
         </p:dialog> 
   </f:view>
</html>
I hope it helps.

Re: dataTable - refresh after insert record.

Posted: 30 Nov 2010, 19:03
by jlferreira
The problem persists.
I only can see the inserted register after an update on screen.
Some other idea?
Thanks for reply...

Re: dataTable - refresh after insert record.

Posted: 30 Nov 2010, 19:18
by kwintesencja
SegGruposController is view scope?


I think In method salvar() you must reload your list with popularListaobj(listaobj);

Re: dataTable - refresh after insert record.

Posted: 30 Nov 2010, 21:43
by jlferreira
Hi kwintesencja,
Thanks for help.
You was wright.
After I made this changes, the dataTable is being updated after save the record.
Only a problem still hapening.
After I save a record, when I press the add button again, the field <h:inputText value="#{segGruposController.classeobj.segGruposDescri}" id="descricao" required="true" label="descricao" /> has the values that I had already inserted (in the same session, moments ago).
If anyone may help me, I appreciate.

Thanks.

Re: dataTable - refresh after insert record.

Posted: 30 Nov 2010, 23:13
by kwintesencja
Glad it works.

regardling the last problem try to reset the classeobj after you save it in the database:

Code: Select all

public String salvar() {
//      System.out.println("getSeggrupos().getSegGruposCodigo(): "+getSeggrupos().getSegGruposCodigo());
      SegGruposDAO objdao = new SegGruposDAO();
      if (classeobj.getSegGruposCodigo() == null) {
         objdao.adicinar(classeobj);
      }
      else {
         objdao.alterar(classeobj);
      }
      classeobj= new SegGrupos();
      return "Salvar";
   }

Re: dataTable - refresh after insert record.

Posted: 01 Dec 2010, 17:49
by jlferreira
Putz...
Agora que ví que você é brasileiro.
E eu brigando com o Inglês aqui.
Valeu mais uma vez pela ajuda.
Tentei o que você falou, alias fiz umas modificações no Controller, mas não deu não...
Quando clico em adicionar, continua aparecendo os dados do dialog que eu havia inserido.
Mais uma vez obrigado.

Re: dataTable - refresh after insert record.

Posted: 01 Dec 2010, 18:01
by kimausoleil
After you must update the form in your p:dialog !
Do this :

Code: Select all

         <p:dialog id="Ldlg_ins_seg_grupos" header="Login" widgetVar="dlg_ins_seg_grupos" modal="false" height="180">
             <h:form id="dlg_form" >

                     <p:commandButton value="Salvar" style="text-align:left"   
                        action="#{segGruposController.salvar}"
                        oncomplete="dlg_ins_seg_grupos.hide();" 
                        update="@form, form_gruposseguranca:table_panel" /> 

            </h:form> 
         </p:dialog> 


Re: dataTable - refresh after insert record.

Posted: 01 Dec 2010, 18:23
by kwintesencja
Opa, eu imaginei pelo código da página mas sei se o idioma padrão não é ingles mas enfim....


Você está atualizando o Dialog quando chama ele?

Code: Select all

 <p:commandButton image="#{request.contextPath}/resources/images/add.png" update="dlg_form:dlg_panel" oncomplete="dlg_ins_seg_grupos.show()"/>
Dando um new no seu objeto toda vez que termina de salvar e atualizando o dialog antes de abrir era pra resolver. Tenta printar o Objeto classeobj antes de abrir o dialog pra ver se ele está realmente null.