primefaces 中的单元格编辑不会更新值

发布于 2024-11-16 01:16:03 字数 5034 浏览 4 评论 0原文

我的 primefaces 应用程序中有一个数据表。前端的代码有

    <!-- Start of customer datatable -->
            <p:dataTable var="customer" value="#{customerBean.customers}" paginator="true" selection="#{customerBean.selectedCustomer}" 
            selectionMode="single" onRowSelectUpdate=":custList" onRowSelectComplete="custTab.show()" id="custList" widgetVar="custList" update=":custList">
                <f:facet name="header">
                List of Customers
                    <p:outputPanel>
                        <p:commandButton value="+" type="button" onclick="addCustDlg.show()"/>
                     </p:outputPanel>
                </f:facet>


                <p:column sortBy="#{customer.id}" filterBy="#{customer.id}" update=":custList">
                    <f:facet name="header">
                        <h:outputText value="ID"/>
                    </f:facet>
                    <h:outputText value="#{customer.id}"/>
                </p:column>

                <p:column sortBy="#{customer.name}" filterBy="#{customer.name}" headerText="NAME" filterMatchMode="contains" update=":custList">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.name}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.name}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column sortBy="#{customer.description}" filterBy="#{customer.description}" headerText="DESCRIPTION">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.description}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.description}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column sortBy="#{customer.signupDate}" filterBy="#{customer.signupDate}" headerText="SIGN UP DATE">
                    <f:facet name="output">
                        <h:outputText value="#{customer.signupDate}"/>
                    </f:facet>
                </p:column>

                <p:column sortBy="#{customer.validUntil}" filterBy="#{customer.validUntil}" headerText="EXPIRY DATE">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.validUntil}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.validUntil}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column sortBy="#{customer.status}" filterBy="#{customer.status}" headerText="STATUS">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.status}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.status}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column headerText="CREATION DATE" sortBy="#{customer.creationDate}" filterBy="#{customer.creationDate}">
                    <f:facet name="output">
                        <h:outputText value="#{customer.creationDate}"/>
                    </f:facet>
                </p:column>

                <p:column headerText="LAST UPDATE DATE" sortBy="#{customer.lastUpdateDate}" filterBy="#{customer.lastUpdateDate}">
                    <f:facet name="output">
                        <h:outputText value="#{customer.lastUpdateDate}"/>
                    </f:facet>
                </p:column>

                <p:column headerText="Options">
                    <p:rowEditor/>
                </p:column>

            </p:dataTable>
            <!-- End of dataTable (customer datatable) -->

并且处理 rowEvent 的函数在 bean 中指定为 但是

 public void custRowEdit(RowEditEvent event){
    Customer cust = (Customer) event.getObject();
    EntityManagerHelper.beginTransaction();
    custDao.update(cust);
    EntityManagerHelper.commit();
}

,在更新事件中,当我编辑表中的单元格时,我没有获得属性的新更新值。 在此处输入图像描述

如下图所示,当我将 ID 为 1 的条目的状态从 11 编辑为 4 时,在函数 custRowEdit 中,当我尝试获取客户对象时,我仍然得到客户的状态为 11 而不是 4 。

谁能帮助我理解为什么单元格的值没有被设置?

I have a datatable in my primefaces application . The code for the frontend has

    <!-- Start of customer datatable -->
            <p:dataTable var="customer" value="#{customerBean.customers}" paginator="true" selection="#{customerBean.selectedCustomer}" 
            selectionMode="single" onRowSelectUpdate=":custList" onRowSelectComplete="custTab.show()" id="custList" widgetVar="custList" update=":custList">
                <f:facet name="header">
                List of Customers
                    <p:outputPanel>
                        <p:commandButton value="+" type="button" onclick="addCustDlg.show()"/>
                     </p:outputPanel>
                </f:facet>


                <p:column sortBy="#{customer.id}" filterBy="#{customer.id}" update=":custList">
                    <f:facet name="header">
                        <h:outputText value="ID"/>
                    </f:facet>
                    <h:outputText value="#{customer.id}"/>
                </p:column>

                <p:column sortBy="#{customer.name}" filterBy="#{customer.name}" headerText="NAME" filterMatchMode="contains" update=":custList">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.name}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.name}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column sortBy="#{customer.description}" filterBy="#{customer.description}" headerText="DESCRIPTION">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.description}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.description}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column sortBy="#{customer.signupDate}" filterBy="#{customer.signupDate}" headerText="SIGN UP DATE">
                    <f:facet name="output">
                        <h:outputText value="#{customer.signupDate}"/>
                    </f:facet>
                </p:column>

                <p:column sortBy="#{customer.validUntil}" filterBy="#{customer.validUntil}" headerText="EXPIRY DATE">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.validUntil}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.validUntil}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column sortBy="#{customer.status}" filterBy="#{customer.status}" headerText="STATUS">
                  <p:cellEditor>
                    <f:facet name="output">
                        <h:outputText value="#{customer.status}"/>
                    </f:facet>
                    <f:facet name="input">
                        <p:inputText value="#{customer.status}"/>
                    </f:facet>
                  </p:cellEditor>
                </p:column>

                <p:column headerText="CREATION DATE" sortBy="#{customer.creationDate}" filterBy="#{customer.creationDate}">
                    <f:facet name="output">
                        <h:outputText value="#{customer.creationDate}"/>
                    </f:facet>
                </p:column>

                <p:column headerText="LAST UPDATE DATE" sortBy="#{customer.lastUpdateDate}" filterBy="#{customer.lastUpdateDate}">
                    <f:facet name="output">
                        <h:outputText value="#{customer.lastUpdateDate}"/>
                    </f:facet>
                </p:column>

                <p:column headerText="Options">
                    <p:rowEditor/>
                </p:column>

            </p:dataTable>
            <!-- End of dataTable (customer datatable) -->

And the function for handling the rowEvent is specified in the bean as

 public void custRowEdit(RowEditEvent event){
    Customer cust = (Customer) event.getObject();
    EntityManagerHelper.beginTransaction();
    custDao.update(cust);
    EntityManagerHelper.commit();
}

However , on an update event , when I am editing the cell in the table , I do not get the new updated value of the attribute . enter image description here

Like in the image below , when I edit the status of the entry with ID 1 from 11 to 4 , in the function custRowEdit , when I try to get the customer object , I still get the status of the customer as 11 and not 4 .

Can anyone help me with understanding why the value of the cell is not being set ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

聆听风音 2024-11-23 01:16:03

从您调用 custRowEdit(RowEditEvent event) 方法的位置。我在你的代码中没有任何相关的东西。

为了使您的侦听器调用,请在数据表声明中添加以下属性。

rowEditListener="#{customerBean.listenerInBackingBean}"

<p:dataTable var="customer" value="#{customerBean.customers}" paginator="true" selection="#{customerBean.selectedCustomer}" 
            selectionMode="single" onRowSelectUpdate=":custList" onRowSelectComplete="custTab.show()" id="custList" widgetVar="custList" update=":custList">
                <f:facet name="header"

rowEditListener="#{customerBean.cutRowEvent}"
>

from Where You are invoking custRowEdit(RowEditEvent event) method. I have not any related thing in your code.

In order to make your listener invoke add below attribute in your datatable declaration.

rowEditListener="#{customerBean.listenerInBackingBean}"

<p:dataTable var="customer" value="#{customerBean.customers}" paginator="true" selection="#{customerBean.selectedCustomer}" 
            selectionMode="single" onRowSelectUpdate=":custList" onRowSelectComplete="custTab.show()" id="custList" widgetVar="custList" update=":custList">
                <f:facet name="header"

rowEditListener="#{customerBean.cutRowEvent}"
>
扛刀软妹 2024-11-23 01:16:03

检查 customerBean.customers 的实现。每次调用该方法时,我都会从数据库中重新加载内容。错误的。这应该发生在构造函数中。现在一切正常。以为是 JavaScript 错误...

Check the implementation of customerBean.customers. I reloaded the content from the database every time the method got called. Wrong. This should happen in the constructor instead. Now everything works fine. Thought it was a JavaScript error ...

深居我梦 2024-11-23 01:16:03

谢谢这对我有帮助。

我可以补充一点,如果有 @SessionScoped 托管 bean,则可以使用 reset() 方法将列表重置为 null,然后从查询中延迟填充列表,而不是从构造函数中的查询加载列表。然后可以在页面加载时使用 f:event 调用重置:

    <f:view>
        <f:metadata>
            <f:event type="preRenderView" listener="#{sessionScopedBean.reset}"/>
        </f:metadata>
    </f:view>        

Thanks this helped me.

May I add that instead of loading the list from a query in the constructor, if one has a @SessionScoped managed bean one can instead use a reset() method to reset lists to null and then lazily populate the lists from the query. The reset can then be called on page load using an f:event:

    <f:view>
        <f:metadata>
            <f:event type="preRenderView" listener="#{sessionScopedBean.reset}"/>
        </f:metadata>
    </f:view>        
携君以终年 2024-11-23 01:16:03

我遇到了类似的情况,通过从数据表中删除 update="" 标签解决了这个问题。表的默认行为是更新行,但在我的例子中,这显然没有发生。

I encountered a similar situation that was resolved by removing the update="" tag from the dataTable. The table's default behavior is to update the row, which evidently, in my case, was not occurring.

江城子 2024-11-23 01:16:03

您错过了 p:ajax 事件来触发方法、函数或在单​​元格编辑后您想要执行的任何操作

,就像

<p:ajax event="cellEdit" listener="#{mBean.onCellEdit}" update="elementX" />

You are missin the p:ajax event to trigger the method, function or whatever you wanna do after the cell editing

it's something like

<p:ajax event="cellEdit" listener="#{mBean.onCellEdit}" update="elementX" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文