0

I would like to edit datatable row. My code here

my ManagedBean

@ManagedBean
@ViewScoped 
public class course implements Serializable{

private int course_Id ; 
private String course_Name;
private Integer course_Hours  ;
private Date course_Date;
private Double course_Prise ;

SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");  
private String sd  ;


   getter and setter .....

  public void onRowEdit(RowEditEvent event) {
     course c =((course) event.getObject());

       Transaction trn = null ;
      Session session = HibernateUtil.getSessionFactory().openSession();
      trn = session.beginTransaction();
     c.setCourse_Id(getCourse_Id());
     c.setCourse_Name(getCourse_Name());
     c.setCourse_Hours(getCourse_Hours());
     c.setCourse_Prise(getCourse_Prise());
     c.setCourse_Date(getCourse_Date());

        session.update(c);

          session.getTransaction().commit();

    FacesMessage msg = new FacesMessage("Course Edited", ((course) event.getObject()).getCourse_Name());
    FacesContext.getCurrentInstance().addMessage(null, msg);
}

My JSF Page

       <?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:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
          <title>Facelet Title</title>
</h:head>
<h:body>
    <h:form id="form">
    <p:growl id="msgs" showDetail="true"/>
    <p:dataTable  id="dt" value="#{course.allcourses}" var="course" editable="true">
        <p:ajax event="rowEdit" listener="#{course.onRowEdit}" update=":form:msgs"  />
        <p:ajax event="rowEditCancel" listener="#{course.onRowCancel}" update=":form:msgs" />

     <p:column id="colId" filterBy="#{course.course_Id}" sortBy="#{course.course_Id}"
                                                  headerText="ID" footerText="contains" 

                                                  filterMatchMode="contains">


                                            <h:outputText value="#{course.course_Id}" />                    
                                        </p:column>
    <p:column filterBy="#{course.course_Name}" headerText="Name" sortBy="#{course.course_Name}">

                                            <p:cellEditor >
                                             <f:facet name="output">  
                                           <h:outputText value="#{course.course_Name}" />  
                                                </f:facet>  
                                                <f:facet name="input">  
                            <p:inputText value="#{course.course_Name}" style="width:100%"/>  
                                                </f:facet> 
                                            </p:cellEditor>
                                        </p:column>
 <p:column filterBy="#{course.course_Hours}" headerText="Hours" sortBy="#{course.course_Hours}">
                                        <p:cellEditor >
                                          <f:facet name="output">  
                                        <h:outputText value="#{course.course_Hours}" />  
                                                </f:facet>  
                                                <f:facet name="input">  
                         <p:inputText value="#{course.course_Hours}" style="width:100%"/>  
                                                </f:facet> 
                                            </p:cellEditor>
                                        </p:column>
  <p:column filterBy="#{course.course_Prise}" headerText="Prise" sortBy="#{course.course_Prise}">
                                     <p:cellEditor >
                                       <f:facet name="output">  
                                <h:outputText value="#{course.course_Prise}" />  
                                                </f:facet>  
                                                <f:facet name="input">  
                        <p:inputText value="#{course.course_Prise}" style="width:100%"/>  
                                                </f:facet> 
                                            </p:cellEditor>
                                        </p:column>
          <p:column filterBy="#{course.course_Date}"    headerText="Start Date" sortBy="#  {course.course_Date}">
                                            <p:cellEditor >
                                                <f:facet name="output">  
                                               <h:outputText value="#{course.course_Date}" />  
                                                </f:facet>  
                                                <f:facet name="input">  
                                    <p:inputText value="#{course.sd}" style="width:100%"/>  
                                                </f:facet> 
                                            </p:cellEditor>


                                        </p:column>
         <p:column style="width:32px">
       <p:rowEditor />

         </p:column>


    </p:dataTable>
    </h:form>
</h:body>

When I modify the cells and i click update button, then the cells return to original state

How can i modify and update the rows?

thank you very much

Note when i use this code c.setCourse_Name("Hibernate"); for example the update work well

this is my method in course Bean

  `public List <course> getAllcourses(){

    List<course> courses ;
    courseDAO dao2 = new courseDAO();
    courses = dao2.getCourses();
    return courses;

   }
     `

and this is getCourses Mathod in course DAo Class

   `public List<course> getCourses (){  

    List<course> allcourses =  null ;
      Transaction trns = null;
    Session session =HibernateUtil.getSessionFactory().openSession();
        try {
       trns= session.beginTransaction();
        allcourses = session.createCriteria(course.class).list();
        session.getTransaction().commit();

         } catch (HibernateException e) {
             e.getMessage();
            trns.rollback();
           } finally {
        session.flush();
        session.close();
    }
    return allcourses;
    }

` ** you see any errors in my code ?**

2 Answers2

1

Your logic is turned around here... You are getting your course object from the event:

course c =((course) event.getObject()); 

which holds the updated values from the datatable, then you replace the values with the old ones by calling the setters.

Remove all this and it should work:

 c.setCourse_Id(getCourse_Id());
 c.setCourse_Name(getCourse_Name());
 c.setCourse_Hours(getCourse_Hours());
 c.setCourse_Prise(getCourse_Prise());
 c.setCourse_Date(getCourse_Date());
Emil Kaminski
  • 1,886
  • 2
  • 16
  • 26
  • Dear Emil Kaminski i did as you mentioned but still the cell return to the original state , Do you see an error in my code ? this issue drive me to crazy – Mohmed Ataala Nov 06 '14 at 15:13
  • Hmm.. i guess you need to debug your problem a little more. Please check that: Are your setters being called? Does your course c =((course) event.getObject()) obejct hold the updated values right before the session.update(c) call? Do your private fields have the right values? What about your #{course.allcourses}"? Does this method contain any logic that would reset the values to the original state? – Emil Kaminski Nov 07 '14 at 07:13
  • Dear Emil firstly thanks for replay , I did debug for many times to check this issue but every thing was working well , as for #{course.allcourses}" this is list of Course class that get allcourses from course table to shows it in datatable – Mohmed Ataala Nov 08 '14 at 18:58
  • one more thing why when i use this code `c.setCourse_Name("Hibernate");` for example the update work well and i see that course name cell updated why ? – Mohmed Ataala Nov 08 '14 at 20:40
  • **i need to help this issue drive me to crazy** – Mohmed Ataala Nov 09 '14 at 16:14
0

I was looking for a solution for this issue but i didn't find and i solved it like below:

    //managed bean

    public List<Operacao> operacoes;

        @PostConstruct
        public void init(){
            this.operacoes = operacaoService.operacoes();
        }

        public List<Operacao> getOperacoes() {
            return operacoes;        
        }

    //xhtml 

<p:dataTable id="cars1" var="car" value="#{tabelaTrades.operacoes}" editable="true" >

i was doing, value="#{tabelaTrades.operacoes()}" and it didn't update the datatable after some change

http://www.mastertheboss.com/jboss-web/primefaces/datatables-with-primefaces?showall=&start=1

Eve
  • 37
  • 9