成功的ajax调用后数据表不更新

发布于 2024-12-26 04:55:07 字数 1808 浏览 4 评论 0 原文

我有一个数据表。表的每一行都有一个名为“Remove”的命令按钮,它应该从模型和视图中删除该行并就地执行更新。作为页脚,我有另一个名为“删除每一行”的 commandButton

最后一个按钮有效。我单击它,从模型中删除每一行(即包含元素的 ArrayList 变为空),并且 dataTable 和 footer facet 是在视图中重新渲染(或更新)。

另一方面,当我单击其中一行上的按钮将其删除时,它部分起作用。相应的元素已从模型中删除,但视图未更新。该行仍然存在于 dataTable 中,并且 footer facet 没有改变。

我的 users.xhtml 中有以下代码。

<f:metadata>
    <f:viewParam name="id" value="#{users.id}" />
    <f:event type="preRenderView" listener="#{users.init}" />
</f:metadata>

...

<h:form id="usersForm">
    <p:outputPanel>    
        <p:dataTable id="userTable" value="#{users.user.friendList}" var="friend">
            <p:column>
                <h:outputText value="#{friend.name}" />
            </p:column>
            <p:column>
                <p:commandButton action="#{users.user.removeFriend(friend)}"
                    ajax="true"
                    update="userTable somethingElse" process="@this"
                    onerror="errorDialog.show();"
                    icon="ui-icon-delete"
                    title="delete user">
                </p:commandButton>
            </p:column>

            <f:facet id="somethingElse" name="footer">  
                    aye: ${users.user.xxx}
            </f:facet>
        </p:dataTable>

    </p:outputPanel>

    <p:commandButton action="#{users.user.removeAllFriends()}" ajax="true"
                update="userTable somethingElse"
                process="@this"
                icon="ui-icon-close"
                value="delete all friends?">
    </p:commandButton>


</h:form>

那么,您认为这里的问题是什么?

我正在使用 JSF 2.0 和 Primefaces 3.0

I have a data table. Each row of the table has a commandButton called 'Remove', which is supposed to remove that row from the model and the view and perform an update in-place. As a footer, I have another commandButton called 'Remove every row'.

The last button works. I click on it, every row is removed from the model (i.e the ArrayList containing the elements becomes empty) and the dataTable and footer facet is re-rendered (or updated) in the view.

On the other hand, when I click a button on one of the rows, to remove it, it partially works. The corresponding element is removed from the model but the view is not updated. That row is still there in the dataTable and the footer facet hasn't changed.

I have the following piece of code in my users.xhtml.

<f:metadata>
    <f:viewParam name="id" value="#{users.id}" />
    <f:event type="preRenderView" listener="#{users.init}" />
</f:metadata>

...

<h:form id="usersForm">
    <p:outputPanel>    
        <p:dataTable id="userTable" value="#{users.user.friendList}" var="friend">
            <p:column>
                <h:outputText value="#{friend.name}" />
            </p:column>
            <p:column>
                <p:commandButton action="#{users.user.removeFriend(friend)}"
                    ajax="true"
                    update="userTable somethingElse" process="@this"
                    onerror="errorDialog.show();"
                    icon="ui-icon-delete"
                    title="delete user">
                </p:commandButton>
            </p:column>

            <f:facet id="somethingElse" name="footer">  
                    aye: ${users.user.xxx}
            </f:facet>
        </p:dataTable>

    </p:outputPanel>

    <p:commandButton action="#{users.user.removeAllFriends()}" ajax="true"
                update="userTable somethingElse"
                process="@this"
                icon="ui-icon-close"
                value="delete all friends?">
    </p:commandButton>


</h:form>

So, what do you think is the problem here?

I'm using JSF 2.0 and Primefaces 3.0

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

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

发布评论

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

评论(6

人事已非 2025-01-02 04:55:07

我从我们当前的 PF 3.0 页面之一中发现了这个问题。如果您使用 update="@form" 代替,它将起作用。我还没有时间去调查真正的原因,以便我可以将其报告给 PF 人员,因为这个问题并未在所有页面中出现。即使在同一张表中,不同的按钮也会按预期工作。

尝试一下:

<p:commandButton update="@form" ... />

更新:仔细想想,这可能与process="@this"的存在有关。

I recognize this problem form one of our current PF 3.0 pages. It will work if you use update="@form" instead. I haven't really had the time to investigate the real cause so that I can report it to the PF guys, because this problem does not manifest in all pages. Even in the same table, a different button works as intented.

Give it a try:

<p:commandButton update="@form" ... />

Update: coming to think about it, it's maybe related to the presence of process="@this".

百思不得你姐 2025-01-02 04:55:07

您可能正在使用 Primefaces 2.2.1,因为这似乎是由于 Primefaces 数据表组件的常见错误造成的。本质上发生的情况是,从 dataTable 元素内部发生的 ajax 回发不会导致 dataTable 中元素的部分页面更新。

最简单的解决方法是从数据表外部的按钮调用回发,其唯一目的只是部分页面更新数据表。这不是最有效的解决方案,因为它会导致两次回发,但它确实有效。

请参阅我之前对此问题的回答此处

You are probably using Primefaces 2.2.1 as this seems like it is due to a common bug with the Primefaces dataTable component. Essentially what occurs is that ajax postbacks occurring from within elements of a dataTable will not result in a partial page update of elements in the dataTable.

The simplest workaround is to invoke a postback from a button outside of the dataTable where its sole purpose is simply to partial page update the dataTable. This is not the most efficient solution as it results in two postbacks, however it does work.

See my previous answer to this problem Here

辞慾 2025-01-02 04:55:07

如果您想使用服务器 ID 来更新组件(例如 userTablesomethingElse...),这些组件需要与触发的组件位于同一命名容器中更新(例如您的按钮)。否则,您需要在更新属性中使用如下表达式:update=":usersForm:userTable"

主要问题是识别命名容器。外部按钮确实有效,因为它与 ID 为 userTable 的表位于同一命名容器(表单)中。面会更新,因为整个表都会更新,但由于数据表本身就是一个命名容器,因此应该不能仅使用 update="somethingElse" 在外部按钮上。

我不确定为什么内部按钮不起作用。我猜数据表中还有另一个命名容器。如果使用 findComponent UIComponent 实现。 信息:在视图中找不到标识符为“userTable”的组件。

If you want to use server IDs for updating components (like userTable, somethingElse, ...), these components need to be in the same naming container as the component which triggers the update (your button for example). Otherwise you need to use an expression like this within the update attribute: update=":usersForm:userTable".

The main problem is to identify the naming containers. The outer button does work, because it is in the same naming container (the form) as the table with the ID userTable. The facet gets updated because the whole table gets updated, but since datatable is a naming container itself, it should not be possible to update the footer by just using update="somethingElse" on the outer button.

I'm not sure, why the inner button does not work. I guess there is another naming container within the datatable. PrimeFaces does log an INFO, if the component could not be found using the findComponent algorithm of the UIComponent implementation. INFO: Cannot find component with identifier "userTable" in view.

悲欢浪云 2025-01-02 04:55:07

我的数据表中有过滤器。所以在这里我解决了这个问题

oncomplete="PF('myWidgetVar').filter()" and update="@form"

I had filters in my datatable. So here I resolved the problem with

oncomplete="PF('myWidgetVar').filter()" and update="@form"
妖妓 2025-01-02 04:55:07

我正在使用 update="@form" 并且它没有完全工作:例如,它没有更新数据表中的行数。我已经在更新表单的按钮上设置了这个:

process="@this" oncomplete="updateRC();"

然后就在下面:


我为解决行更新问题所做的是将 styleClass 添加到 dataTable,例如“myTable”,然后在上面的remoteCommand中添加一个jQuery选择器到该类,例如:

I was using update="@form" and it didn't completely work: it wasn't updating the number of rows in the dataTable, for instance. What I already had in place was this on the button that updated the form:

process="@this" oncomplete="updateRC();"

and then just below I had:

<p:remoteCommand name="updateRC" process="@this" update="@form" />


What I did to fix the rows update problem is add a styleClass to the dataTable, e.g. "myTable", and then add a jQuery selector to that class in the above remoteCommand, e.g.:

<p:remoteCommand name="updateRC" process="@this" update="@form @(.myTable)" />

围归者 2025-01-02 04:55:07

这是jsf 2.0中刷新的解决方案

<p:dataTable  id="empl1" var="emp" value="#{dtEmployeeView.employee}" selectionMode="multiple"  rowKey="#{emp.id}"
                              paginator="true" rows="5" tableStyleClass="ui-table-columntoggle">
    <p:column width="20" headerText="Id"  priority="1">
        <h:link value="#{emp.id}" />
    </p:column>
    <p:column headerText="Employee Name" priority="2">
        <h:outputText value="#{emp.pname}" />
    </p:column>

</p:dataTable>
<p:commandButton update="form1:empl1" ajax="true" value="Submit" action="#{empService.addEmployee(employee)}" >
    <f:event type="preRenderView" listener="#{dtEmployeeView.init()}"/>
</p:commandButton>

This is the solution to refresh in jsf 2.0

<p:dataTable  id="empl1" var="emp" value="#{dtEmployeeView.employee}" selectionMode="multiple"  rowKey="#{emp.id}"
                              paginator="true" rows="5" tableStyleClass="ui-table-columntoggle">
    <p:column width="20" headerText="Id"  priority="1">
        <h:link value="#{emp.id}" />
    </p:column>
    <p:column headerText="Employee Name" priority="2">
        <h:outputText value="#{emp.pname}" />
    </p:column>

</p:dataTable>
<p:commandButton update="form1:empl1" ajax="true" value="Submit" action="#{empService.addEmployee(employee)}" >
    <f:event type="preRenderView" listener="#{dtEmployeeView.init()}"/>
</p:commandButton>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文