简体   繁体   English

成功调用ajax后,Datatable不会更新

[英]Datatable does not update after successful ajax call

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. 该表的每一行都有一个名为“Remove”commandButton ,它应该从模型和视图中删除该行并就地执行更新。 As a footer, I have another commandButton called 'Remove every row' . 作为页脚,我有另一个名为'删除每一行'的 commandButton

The last button works. 最后一个按钮有效。 I click on it, every row is removed from the model (ie the ArrayList containing the elements becomes empty) and the dataTable and footer facet is re-rendered (or updated) in the view. 我点击它,每一行都从模型中删除(即包含元素的ArrayList变为空),并在视图中重新呈现(或更新) dataTablefooter facet

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. 该行仍然存在于dataTable ,并且footer facet没有更改。

I have the following piece of code in my users.xhtml . 我在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 我正在使用JSF 2.0和Primefaces 3.0

I recognize this problem form one of our current PF 3.0 pages. 我从当前的PF 3.0页面中认识到这个问题。 It will work if you use update="@form" instead. 如果您使用update="@form" ,它将起作用。 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. 我没有真正的时间来调查真正的原因,以便我可以向PF人员报告,因为这个问题并没有在所有页面中出现。 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" . 更新 :想想它,它可能与process="@this"的存在有关。

You are probably using Primefaces 2.2.1 as this seems like it is due to a common bug with the Primefaces dataTable component. 您可能正在使用Primefaces 2.2.1,因为这似乎是由于Primefaces dataTable组件的常见错误。 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. 基本上发生的是,从dataTable的元素内部发生的ajax回发不会导致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. 最简单的解决方法是从dataTable外部的按钮调用回发,其唯一目的是简单地部分页面更新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 见我以前回答这个问题, 在这里

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). 如果要使用服务器ID来更新组件(例如userTablesomethingElse ,...),则这些组件需要与触发更新的组件(例如,按钮)位于同一命名容器中。 Otherwise you need to use an expression like this within the update attribute: update=":usersForm:userTable" . 否则,您需要在update属性中使用这样的表达式: 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 . 外部按钮确实有效,因为它与具有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. 小面被更新,因为整个表得到更新,但由于数据表是命名容器本身,它应该是不可能通过只使用更新页脚update="somethingElse"的外键。

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. 如果使用UIComponent实现的findComponent算法找不到组件,PrimeFaces会记录INFO。 INFO: Cannot find component with identifier "userTable" in view.

This is the solution to refresh in jsf 2.0 这是在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>

I had filters in my datatable. 我的数据表中有过滤器。 So here I resolved the problem with 所以我在这里解决了这个问题

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

I was using update="@form" and it didn't completely work: it wasn't updating the number of rows in the dataTable, for instance. 我使用update="@form"并没有完全奏效:例如,它没有更新dataTable中的行数。 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, eg "myTable", and then add a jQuery selector to that class in the above remoteCommand, eg: 我为修复行更新问题所做的是将一个styleClass添加到dataTable,例如“myTable”,然后在上面的remoteCommand中为该类添加一个jQuery选择器,例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM