【问题标题】:Primefaces: commandButton in confirmDialog cannot update datatable in the same formPrimefaces:confirmDialog 中的 commandButton 无法以相同的形式更新数据表
【发布时间】:2012-03-04 15:03:35
【问题描述】:

我有 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>

【问题讨论】:

  • 请出示facelet代码。

标签: jsf-2 primefaces


【解决方案1】:

我犯了一个大错误。我用了标签。我应该使用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>

感谢您的帮助

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • OP提到@form确实更新了表格,所以表格肯定在与按钮相同的表单内。
    • @BalusC - 你是对的。如果没有给出 facelet 代码,我们几乎无法判断问题出在哪里……老实说,我确实忽略了“变为禁用”部分,并认为 OP 只想更新表格而不是整个表格。我的错 :) 让我们等待 facelet 代码。
    猜你喜欢
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 2017-04-02
    • 1970-01-01
    • 2019-09-08
    • 2012-08-16
    • 1970-01-01
    • 2011-06-06
    相关资源
    最近更新 更多