cellEdit不适合我

时间:2015-01-07 12:00:01

标签: jsf-2 primefaces datatable celleditorlistener

喜 我的DataTable有问题,当我更改值并尝试获取它时,我在更改前具有相同的值。帮助PLZ解决这个问题8天。

这是我的页面xhtml

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.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:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">

    <f:view contentType="text/html">
        <h:head>
            <f:facet name="first">
                <meta content='text/html; charset=UTF-8' http-equiv="Content-Type"/>
                <title>PrimeFaces</title>
            </f:facet>
        </h:head>
    <h:body>
    <center> <h3> Interventions Préventives </h3> </center> 
    <h:form id="form">
    <p:contextMenu for="cars2" widgetVar="cMenu">   
       <p:menuitem value="Edit Cell" icon="ui-icon-search" onclick="PF('cellCars').showCellEditor();return false;"/>  
       <p:menuitem value="Hide Menu" icon="ui-icon-close" onclick="PF('cMenu').hide()"/>  
   </p:contextMenu> 
   <p:growl id="msgs" showDetail="true"/>
  <p:dataTable id="cars2" var="item" value="#{monTest.personnes}" editable="true" editMode="cell" widgetVar="cellCars">
       <f:facet name="header"> Cell Editing with Click and RightClick
       </f:facet>
       <p:ajax event="cellEdit" listener="#{monTest.onCellEdit}" update=":form:msgs" />
       <p:column headerText="Id">
              <p:cellEditor>
                  <f:facet name="output"><h:outputText value="#{item.id}" /></f:facet>
                  <f:facet name="input"><p:inputText id="modelInput" value="#{item.id}" style="width:96%"/></f:facet>
              </p:cellEditor>
          </p:column>

          <p:column headerText="Year">
           <p:cellEditor>
               <f:facet name="output"><h:outputText value="#{item.nom}" /></f:facet>
               <f:facet name="input"><p:inputText value="#{item.nom}" style="width:96%" label="Year"/></f:facet>
           </p:cellEditor>
       </p:column>
</p:dataTable>
  </h:form>
     </h:body>
</f:view>
</html>

这是我的MAnagedBean

package ManagedBeans;

import entities.Personne;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import org.primefaces.event.CellEditEvent;
import session.PersonneFacadeLocal;


@Named(value = "monTest")
@SessionScoped
public class MonTest implements Serializable {
    @EJB
    private PersonneFacadeLocal personneFacade;
    private List<Personne> personnes;
    private String value1;

    @PostConstruct
    public void init(){
        personnes=personneFacade.findAll();
    }

    public MonTest() {
    }

    public List<Personne> getPersonnes() {

        personnes=personneFacade.findAll();
        return personnes;
    }
    public void test(Personne p){

        System.out.println("*************************************************************************");
        System.out.println(p.getNom()+"**************"+p.getPrenom()+"*********************"+p.getAge());
    }

    public PersonneFacadeLocal getPersonneFacade() {
        return personneFacade;
    }

    public void setPersonneFacade(PersonneFacadeLocal personneFacade) {
        this.personneFacade = personneFacade;
    }

    public String getValue1() {
        return value1;
    }

    public void setValue1(String value1) {
        this.value1 = value1;
    }

     public void onCellEdit(CellEditEvent event) {
        Object oldValue = event.getOldValue();
        Object newValue = event.getNewValue();

        if(newValue != null && !newValue.equals(oldValue)) {
            FacesMessage msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Cell Changed", "Old: " + oldValue + ", New:" + newValue);
            FacesContext.getCurrentInstance().addMessage(null, msg);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您对列表人员的获取是从数据库中检索数据,而您的onCellEdit方法未更新数据库。因此数据表上的数据将始终相同。 更改onCellEdit方法以触发更改方法到正在更改的实体(获取此实体,使用event.getObject())。