I have faced the issue with the <p:dataTable> when using rowEdit ajax event. Firstly, I tried to google some information regarding this and found out that many people faced the same issue.

The issue was in the following: when I try to edit row in the dataTable, all is going well. But when I try to save the changes, the new value doesn't retrieve and new value has not been saved.

The listener for the dataTable is defined in the following way:

<p:ajax event="rowEdit" listener="#{diarylMB.editRecord}" update="@this"/>

The function editRecord from managed bean:

public void editRecord(RowEditEvent event){
        Diary updated = (Diary) event.getObject();

event.getObject() retrieves the new value from the dataTable and edit() updates the record in the database.

But this code didn't work for me. event.getObject didn't retrieve the new data and as the result the record was not updated.

So, the main problem is that getObject() retrieves for some reason an old data.

Let's take a look at this line:

                                    <p:dataTable var="diary" value="#{diarylMB.lstDiary}" paginator="true" rows="10" paginatorPosition="bottom" editable="true">

The list displayed in the dataTable is getting in the getLstDiary function. This is a simple getter that retrieves the list of diary records from the database using the service class:

lstDiary = (List<Diary>) diaryService.getAll(usrMB.getCurrentUser());

So, this data always retrieves from the database and that is why getObject() returned an old value ( actually the value from the database). In order to fix this issue I have implemented the simplest checker:

if (lstDiary == null){
            lstDiary = (List<Diary>) diaryService.getAll(usrMB.getCurrentUser());
        return lstDiary;

That is all. So, firstly, before submitting bug to the PrimeFaces, please check your code ;)

2 коммент.:

Subscribe to RSS Feed Follow me on Twitter!