素面行编辑中的 event.getObject() 将旧值发送到 bean [英] event.getObject() in row editing for primefaces sends the old value to the bean

查看:10
本文介绍了素面行编辑中的 event.getObject() 将旧值发送到 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

当我说编辑用户时,输入的新值不会发送到 bean.它仍然只显示旧值.

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

我在 JSF 中的代码:

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>

我在 bean 中的代码:

My code in bean:

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

bean 中用于在 ui 中获取列表的代码:

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;
    }

我已经尝试了一些帖子中给出的建议,但没有奏效.谁能告诉我如何获得新值.我使用的是 glassfish 4.0,primefaces 3.5.

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.

推荐答案

我能够找出问题所在..每次当我在 getter 方法中获取列表时,我都会调用数据库来加载数据..但是只有当列表为空时才应该这样做.以下代码为您提供了清晰的图片:

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;
    }

所以 getUsersList 是我在 p:datatable 中的 var 值,现在当我调用 onEdit 时,它会检查列表是否为空,它会调用数据库还是不调用数据库.

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.

这篇关于素面行编辑中的 event.getObject() 将旧值发送到 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆