Primefaces:confirmDialog中的commandButton无法以相同的形式更新数据表

时间:2012-02-13 07:01:54

标签: jsf-2 primefaces

我有1个数据表,每行包含编辑和删除命令按钮。 “删除”按钮显示一个确认对话框,并通过actionListener将currentObject变量设置为同一行中它所属的对象。

在确认对话框中,Yes也是一个命令按钮,它调用bean的delete(object)方法。

一切正常。删除功能如下:

    public void delete(ActionEvent actionEvent) {
    categoryRepository.delete(currentCategory);
    currentCategory = new Category();
    categoryList = categoryRepository.findAll();
}

我想在删除后更新数据表。但更新不适用于数据表。我只能更新@form,但这一次,页面中的所有内容都变为禁用,我无法选择任何内容。可能是一个错误。

这是xhtml代码:

    <h:form id="categoryForm">
                <p:commandButton id="addCategoryButton" value="New"
                    onclick="categoryDialog.show();" type="button" />

                <p:dataTable var="cat" value="#{categoryBean.categoryList}"
                    rowKey="#{cat.id}" paginator="true" rows="10"
                    selection="#{categoryBean.selectedCategories}" id="categoryTable"
                    widgetVar="categoryTable">

                    <f:facet name="header">
        Category List
    </f:facet>
                    <p:column selectionMode="multiple" />
                    <p:column headerText="Name" sortBy="#{cat.name}"
                        filterBy="#{cat.name}" id="name">
                     #{cat.name[categoryBean.currentLocale.language]}
                    </p:column>
                    <p:column headerText="Sort Order" sortBy="#{cat.sortOrder}"
                        filterBy="#{cat.sortOrder}" id="sortOrder">
                        #{cat.sortOrder}
                    </p:column>
                    <p:column headerText="Actions" id="actions">
                        <h:commandButton action="#{categoryBean.edit(cat)}" value="Edit" />

                        <h:commandButton value="Delete"
                            onclick="deleteConfirmation.show()" type="button">
                            <f:setPropertyActionListener value="#{cat}"
                                target="#{categoryBean.currentCategory}" />
                        </h:commandButton>

                    </p:column>

                </p:dataTable>
                <p:confirmDialog id="deleteConfirmDialog" message="Are you sure?"
                    header="Initiating destroy process" severity="alert"
                    widgetVar="deleteConfirmation">

                    <p:commandButton id="confirm" value="Yes Sure"
                        update=":categoryForm:categoryTable"
                        oncomplete="deleteConfirmation.hide()"
                        actionListener="#{categoryBean.delete}" />
                    <p:commandButton id="decline" value="Not Yet"
                        onclick="deleteConfirmation.hide()" type="button" />
                </p:confirmDialog>

                <p:dialog id="categoryDialog" header="Category Detail"
                    widgetVar="categoryDialog" resizable="false" style="width:90%;"
                    showEffect="explode" hideEffect="explode">
                    <p:panel id="panel" header="Edit Category"
                        style="margin-bottom:10px;">
                        <p:messages id="messages" />
                        <h:panelGrid columns="3">
                            <h:outputLabel for="nameTabView" value="Name: " />
                            <p:tabView id="nameTabView">
                                <c:forEach var="locale" items="#{categoryBean.userLocales}">
                                    <p:tab title="#{locale.displayLanguage}">
                                        <h:panelGrid columns="2" cellpadding="10">
                                            <h:inputText
                                                value="#{categoryBean.currentCategory.name[locale.language]}" />
                                        </h:panelGrid>
                                    </p:tab>
                                </c:forEach>
                            </p:tabView>
                        </h:panelGrid>
                        <p:commandButton id="saveCategoryButton" value="Save"
                            oncomplete="categoryDialog.hide()"
                            actionListener="#{categoryBean.save}"
                            update="categoryTable, categoryDialog" />
                    </p:panel>

                </p:dialog>
            </h:form>

2 个答案:

答案 0 :(得分:0)

这似乎是在AJAX请求之后更新DOM的问题。我无法确定,因为你没有提供页面标记,但我想这是由于以下原因:

当您希望更新页面的某些部分时,在完成ajax调用之后,您需要提供要更新的组件的ID作为命令按钮/命令链接的update属性的值。我想这就是你做的。

问题是,如果组件的服务器端ID与触发请求的组件位于同一命名容器中,则可能只提供要更新的组件的服务器端ID。您可以尝试使用以下ID来更新表<p:commandButton action="xxx" update=":myForm:myTable" />,以便在ID为“myForm”的表单中更新表格ID为“myTable”。

答案 1 :(得分:0)

我犯了一个大错误。我用过标签。我应该使用primefaces。

                <h:form id="categoryForm">
                <p:commandButton id="addCategoryButton" value="New"
                    onclick="categoryDialog.show();" type="button" />

                <p:dataTable var="cat" value="#{categoryBean.categoryList}"
                    rowKey="#{cat.id}" paginator="true" rows="10"
                    selection="#{categoryBean.selectedCategories}" id="categoryTable"
                    widgetVar="categoryTable">

                    <f:facet name="header">
        Category List
    </f:facet>
                    <p:column selectionMode="multiple" />
                    <p:column headerText="Name" sortBy="#{cat.name}"
                        filterBy="#{cat.name}" id="name">
                     #{cat.name[categoryBean.currentLocale.language]}
                    </p:column>
                    <p:column headerText="Sort Order" sortBy="#{cat.sortOrder}"
                        filterBy="#{cat.sortOrder}" id="sortOrder">
                        #{cat.sortOrder}
                    </p:column>
                    <p:column headerText="Actions" id="actions">
                        <p:panelGrid columns="2" styleClass="actions" cellpadding="2">
                            <p:commandButton action="#{categoryBean.edit(cat)}" value="Edit" />
                            <p:commandButton value="Delete"
                                onclick="deleteConfirmation.show()">
                                <f:setPropertyActionListener value="#{cat}"
                                    target="#{categoryBean.currentCategory}" />
                            </p:commandButton>
                        </p:panelGrid>
                    </p:column>

                </p:dataTable>
                <p:confirmDialog id="deleteConfirmDialog" message="Are you sure?"
                    header="Initiating destroy process" severity="alert"
                    widgetVar="deleteConfirmation">

                    <p:commandButton id="confirm" value="Yes Sure"
                        update=":categoryForm:categoryTable"
                        oncomplete="deleteConfirmation.hide()"
                        actionListener="#{categoryBean.delete}" />
                    <p:commandButton id="decline" value="Not Yet"
                        onclick="deleteConfirmation.hide()" type="button" />
                </p:confirmDialog>

                <p:dialog id="categoryDialog" header="Category Detail"
                    widgetVar="categoryDialog" resizable="false" style="width:90%;"
                    showEffect="explode" hideEffect="explode">
                    <p:panel id="panel" header="Edit Category"
                        style="margin-bottom:10px;">
                        <p:messages id="messages" />
                        <h:panelGrid columns="3">
                            <h:outputLabel for="nameTabView" value="Name: " />
                            <p:tabView id="nameTabView">
                                <c:forEach var="locale" items="#{categoryBean.userLocales}">
                                    <p:tab title="#{locale.displayLanguage}">
                                        <h:panelGrid columns="2" cellpadding="10">
                                            <h:inputText
                                                value="#{categoryBean.currentCategory.name[locale.language]}" />
                                        </h:panelGrid>
                                    </p:tab>
                                </c:forEach>
                            </p:tabView>
                        </h:panelGrid>
                        <p:commandButton id="saveCategoryButton" value="Save"
                            oncomplete="categoryDialog.hide()"
                            actionListener="#{categoryBean.save}"
                            update="categoryTable, categoryDialog" />
                    </p:panel>

                </p:dialog>
            </h:form>

感谢您的帮助