简体   繁体   English

数据表Primefaces编辑器不发送更新值进行编辑

[英]Datatable Primefaces editor does not send updated value for editing

Good afternoon people! 大家下午好! I'm trying to do an update through the datatable edit the Primefaces, but does not send the value of inputText upgraded to the bean. 我正在尝试通过数据表编辑Primefaces进行更新,但不发送升级到bean的inputText的值。 The process occurs almost correctly, the value is sent and updated, but the old value already loaded into datatable. 该过程几乎正确地进行,该值已发送和更新,但是旧值已加载到数据表中。

can anyone help me on this :) 谁可以帮我这个事 :)

Forgive my English. 原谅我的英语。 I do not speak or write very well .... 我说或写得不太好....

listaProduto.xhtml listaProduto.xhtml

<!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://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://primefaces.org/ui">

    <ui:composition template= "template.xhtml">

        <ui:define name="conteudo" >
            <h:form id="form">
                <p:growl id="msgs" showDetail="true"/>
                <p:dataTable  var="prod" value="#{pBean.listaproduto}" editable="true" style="margin-bottom:20px" id="listaproduto1" >

                    <p:ajax event="rowEditInit" listener="#{pBean.onRowEdit}" update=":form:msgs" immediate="true"  />
                    <p:ajax event="rowEditCancel" listener="#{pBean.onRowCancel}" update=":form:msgs" /> 

                    <p:column headerText="Id">
                        <h:outputText value="#{prod.id}"/>
                    </p:column>
                    <p:column headerText="Nome">
                        <p:cellEditor>
                            <f:facet name="output"><h:outputText value="#{prod.nome}"/></f:facet>
                            <f:facet name="input"><p:inputText  value="#{prod.nome}" style="width:100%"/></f:facet>
                        </p:cellEditor>
                    </p:column>
                    <p:column headerText="Preço">
                        <p:cellEditor>
                            <f:facet name="output"><h:outputText value="#{prod.preco}"/></f:facet>
                            <f:facet name="input"><p:inputText  value="#{prod.preco}" style="width:100%"/></f:facet>
                        </p:cellEditor>
                    </p:column>
                    <p:column headerText="Fornecedor">
                        <p:cellEditor>
                            <f:facet name="output"><h:outputText value="#{prod.fornecedor}"/></f:facet>
                            <f:facet name="input"><p:inputText  value="#{prod.fornecedor}" style="width:100%"/></f:facet>
                        </p:cellEditor>
                    </p:column>
                    <p:column headerText="Categoria">
                        <p:cellEditor>
                            <f:facet name="output"><h:outputText value="#{prod.categoria}"/>  </f:facet>
                            <f:facet name="input"><p:inputText id="cate"  value="#{prod.categoria}" style="width:100%"/></f:facet>
                        </p:cellEditor>
                    </p:column>             

                    <p:column style="width:32px">
                        <p:rowEditor  />
                    </p:column> 
                </p:dataTable>
            </h:form>
        </ui:define>
    </ui:composition>
</html>

ProdutoBean 产豆

package manager;

import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import org.primefaces.event.RowEditEvent;
import br.com.estoque.model.Produto;

import br.com.estoque.persistence.ProdutoDao;

@ManagedBean(name = "pBean")
@ViewScoped
public class ProdutoBean {

    private Produto produto;
    private List<Produto> listaproduto;

    public ProdutoBean() {
        produto = new Produto();
    }

    public Produto getProduto() {
        return produto;
    }

    public void setProduto(Produto produto) {
        this.produto = produto;
    }

    public List<Produto> getListaproduto() {
        try {
            listaproduto = new ProdutoDao().listar();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return listaproduto;
    }

    public void setListaproduto(List<Produto> listaproduto) {
        this.listaproduto = listaproduto;
    }

    public String cadastrar() {
        FacesContext fc = FacesContext.getCurrentInstance();

        try {
            new ProdutoDao().cadastrar(produto);
            fc.addMessage("formproduto", new FacesMessage(produto.getNome() + "produto cadastrado com sucesso"));

            produto = new Produto();

        } catch (Exception e) {
            e.printStackTrace();
            fc.addMessage("formproduto", new FacesMessage(produto.getNome() + "Não cadastrado"));
        }
        return null;
    }

    public void onRowEdit(RowEditEvent event) {
        System.out.println(produto);
        produto = ((Produto) event.getObject());
        System.out.println(produto);
    }

    public void onRowCancel(RowEditEvent event) {
        FacesMessage msg = new FacesMessage("Edit Cancelled");
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }
}

I think you should use other type of event, because rowEditInit is executing when you click pencil icon, and after edition when you click save icon is executing event rowEdit . 我认为你应该使用其他类型的事件,因为当你点击铅笔图标rowEditInit正在执行,并且版本后,当您单击保存图标正在执行事件rowEdit。 Therefore try: 因此,请尝试:

<p:ajax event="rowEdit" listener="#{pBean.onRowEdit}" update=":form:msgs"/>

Also forgive my English :) 也原谅我的英语:)

Staff managed to solve. 员工设法解决。 in fact the problem was in my listaprodutos get ... after entering an if and else, testing whether the product list was null, functioned normally. 实际上,问题出在我的listaprodutos get ...输入if and else之后,测试产品列表是否为null,功能正常。 The cool thing is, as a friend pointed out, always check the values passed by the network browser. 一位朋友指出,很酷的事情是,始终检查网络浏览器传递的值。 Hugs 拥抱

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM