Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

event.getObject() in row editing for primefaces sends the old value to the bean

I am trying to use the feature of prime faces which allows user to edit the row data in the table itself.I have followed this link to achieve it:

http://www.primefaces.org/showcase/ui/datatableRowEditing.jsf

When I say edit user the new values which are entered are not sent to the bean.It still shows the old values only.

My code in JSF:

<p:dataTable value="#{mybean.userList}"
        var="item"
        id="dataTab"
        widgetVar="usersTable"
        tableStyleClass="data"  paginator="true" rows="5"  
        filteredValue="#{userController.filteredUsers}"
        editable="true"
        rowKey="#{item}">

    <p:ajax event="rowEdit"   listener="#{mybean.onEdit}" update=":userForm:growl" />  
    <p:ajax event="rowEditCancel" listener="#{mybean.onCancel}" update=":userForm:growl" /> 
    <f:facet name="header">
        <p:outputPanel>  
            <h:outputText value="Search all fields:" />  
            <p:inputText id="globalFilter"     onkeyup="('usersTable').filter()"   style="width:150px" />  
        </p:outputPanel>     
    </f:facet>


    <p:column sortBy="#{item.firstName}" filterBy="#{item.firstName}" 
        filterMatchMode="startsWith">
        <p:cellEditor> 
            <f:facet name="header">  
                <h:outputText value="First Name" />  
            </f:facet>
            <f:facet name="output">
                <h:outputText  value="#{item.firstName}" />
            </f:facet>
            <f:facet name="input">  
                <p:inputText value="#{item.firstName}" style="width:100%"/>  
            </f:facet>  
        </p:cellEditor>
    </p:column>
    <p:column  sortBy="#{item.lastName}" filterBy="#{item.lastName}" filterMatchMode="startsWith">
        <p:cellEditor> 
            <f:facet name="header">  
                <h:outputText value="Last Name" />  
            </f:facet>

            <p:column headerText="Update" style="width:6%">  
                <p:rowEditor />  
            </p:column>    


</p:dataTable>

My code in bean:

public String onEdit(RowEditEvent event) {
    User user=(User)event.getObject());
    user.getFirstName();
}

code in the bean for getting the list in ui:

 public List<UserBean> getUsersList(){
        List<UserBean> retval = new ArrayList<>();            
        for (Object[] tuple : myFacade.getUserList()) {
            UserBean ub = new UserBean();
            ub.setFirstName((String) tuple[0]);
            ub.setLastName((String)tuple[1]);
            ub.setEmailAddress((String)tuple[2]);
            ub.setOfficeNumber((String)tuple[3]);
            ub.setRole((String)tuple[4]);                
            retval.add(ub);
        }
        return retval;
    }

I have tried the suggestions that were given in some of the posts but they did not work.Could anyone let me know how can I get the new values.I am using glassfish 4.0,primefaces 3.5.

like image 283
Valla Avatar asked Oct 02 '13 17:10

Valla


1 Answers

I was able to figure out the problem..Every time when I am getting the list in the getter method I am calling the database to load the data..But this should be done only if the list is null.The following code gives you a clear picture:

public List<UserBean> getUsersList(){


        if(retval == null)
        {
            retval = new ArrayList<>();

            for (Object[] tuple : myFacade.getUserList()) {
            UserBean ub = new UserBean();
            ub.setFirstName((String) tuple[0]);
            ub.setLastName((String)tuple[1]);
            ub.setEmailAddress((String)tuple[2]);
            ub.setOfficeNumber((String)tuple[3]);
            ub.setRole((String)tuple[4]);

            retval.add(ub);
        }
        }
        return retval;
    }

So getUsersList is my value of var in the p:datatable and now when I call the onEdit it will check for the list if it is null it does a database call or does not call the database.

like image 178
Valla Avatar answered Nov 20 '22 09:11

Valla