简体   繁体   English

JSF-<f:ajax> 无法呈现数据表</f:ajax>

[英]JSF- <f:ajax> can't render datatable

I have a datatable which has a "delete" button at each row, and I want to re-render the table when the data at the row is deleted.我有一个数据表,每行都有一个"delete"按钮,我想在删除该行的数据时重新渲染该表。

But I can not make f:ajax work properly.但我无法使f:ajax正常工作。

Here is the datatable at facelet page:这是facelet页面上的数据表:

<h:form>
 <h:dataTable  value="#{adminPanelBean.activitiesList}" var="activityItem" id="allActivities"
                          styleClass="datatable" cellpadding="2" cellspacing="4"
                          columnClasses="ColName,colType,colTime,colPlace,colSummary" >
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Etkinlik"/>
                    </f:facet>
                    <h:outputText value="#{activityItem.name}  "/>

                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Türü"/>
                    </f:facet>
                    <h:outputText value="#{activityItem.type}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Tarih "/>
                    </f:facet>
                    <h:outputText value="#{activityItem.time}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Yer "/>
                    </f:facet>
                    <h:outputText value="#{activityItem.place}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="Açıklama "/>
                    </f:facet>
                    <h:outputText value="#{activityItem.summary}"/>
                </h:column>
                <h:column>
                    <f:facet name="header">
                        <h:outputText value="&nbsp;"/>
                    </f:facet>
            **<h:commandButton  value="Delete activity" onclick="if(confirm(' Delete #{activityItem.name} ?'))return true; else return false;"  action="#{adminPanelBean.deleteActivity(activityItem.id)}">&nbsp;
                    <f:ajax event="click" render="allActivities" execute="@all" immediate="true"/>
            <f:param name="activityId" value ="#{activityItem.id}"  />
               </h:commandButton>**
             </h:column>
            </h:dataTable>
        </h:form>

When I refresh the page, I see the row is deleted.当我刷新页面时,我看到该行已被删除。 But I want it to be done dynamically.但我希望它是动态完成的。

In the action method you need to remove the item from the list as well (and thus not only from the DB).在 action 方法中,您还需要从列表中删除该项目(因此不仅仅是从数据库中)。 I'd suggest to pass the whole item instead of only the ID so that you can utilize List#remove() method.我建议传递整个项目而不是仅传递 ID,以便您可以使用List#remove()方法。

action="#{adminPanelBean.deleteActivity(activityItem)}">

So that you can do这样你就可以做到

public void deleteActivity(Activity activity) {
    activitiesList.remove(activity);
    // ...
}

Unrelated to the concrete problem, they way how you used confirm() is pretty clumsy.与具体问题无关,他们使用confirm()的方式非常笨拙。

onclick="if(confirm(' Delete #{activityItem.name} ?'))return true; else return false;"

That function returns a boolean already. function 已经返回boolean You do not need to test the obtained boolean within an if in order to return another boolean .您无需在if中测试获得的 boolean 即可返回另一个boolean Just simplify it as follows:只需将其简化如下:

onclick="return confirm('Delete #{activityItem.name}?')"

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

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