【问题标题】:Show row data from datatable in dialog in JSF在 JSF 的对话框中显示数据表中的行数据
【发布时间】:2013-10-02 12:59:33
【问题描述】:

我有一个用 JSF 2.2 和 PrimeFaces 3.5 编写的简单 CRUD 应用程序。该数据库由两个表组成 - 客户和订单。我有一个数据表视图,其中列出了所有客户,最后一列是三个按钮 - 用于 eidt、删除和显示每个客户的订单。单击编辑按钮时,应在模式对话框表单中加载所选客户端的数据,但无论在对话框中按下哪个按钮,始终为第一行中的客户端加载数据。删除按钮也是如此。

这是视图的代码:

<h:form id="form">

    <p:growl id="msgs" showDetail="true" />

    <p:commandButton id="addNewClientButton" type="button"
        onclick="add.show();" value="Add new client"
        action="clientBeans.newClient" />
    <br />

    <p:dataTable var="client" value="#{clientBeans.getAllClients()}"
        border="true">
        <p:column headerText="Client Id">
            <h:outputText value="#{client.clientId}" />
        </p:column>
        <p:column headerText="Name">
            <h:outputText value="#{client.name}" />
        </p:column>
        <p:column headerText="Address">
            <h:outputText value="#{client.address}" />
        </p:column>
        <p:column headerText="Email">
            <h:outputText value="#{client.email}" />
        </p:column>
        <p:column headerText="Options">
            <p:commandButton value="Edit" id="editClientButton"
                action="#{clientBeans.editClient}" type="button"
                update=":form:editClient" onclick="edit.show();">
                <f:setPropertyActionListener 
                                      target="#{clientBeans.client}"
                    value="#{client}" />
            </p:commandButton>
            <p:commandButton action="#{clientBeans.deleteClient}"                              
                                   value="Delete"
                id="deleteClientButton" type="button"             
                                    onclick="remove.show();"
                icon="ui-icon-circle-close">
                <f:setPropertyActionListener
                    target="#{clientBeans.client.clientId}" 
                            value="#{client}" />
            </p:commandButton>
            <p:commandButton
               action="#orderBeans.getSpecificOrders(client.clientId)}"
                value="Orders">
                <f:setPropertyActionListener
                    target="#{clientBeans.client.clientId}" 
                 value="#{client.clientId}" />
            </p:commandButton>
        </p:column>
    </p:dataTable>
</h:form>

<h:form>
    <p:dialog id="addNewClient" header="Add new client" widgetVar="add"
        modal="true" resizable="false">
        <p:panelGrid columns="2" border="false">
            <h:outputLabel for="name" value="Name: " />
            <p:inputText id="name" value="#{clientBeans.client.name}"
                label="name" required="true" />

            <h:outputLabel for="address" value="Address: " />
            <p:inputText id="address" 
                        value="#{clientBeans.client.address}"
                label="address" required="true" />

            <h:outputLabel for="email" value="Email: " />
            <p:inputText id="email" value="#{clientBeans.client.email}"
                label="email" required="true" />

            <f:facet name="footer">
                <p:commandButton 
                        action="#{clientBeans.createClient}" value="Save"
                    icon="ui-icon-check" style="margin:0">  
                             </p:commandButton>
            </f:facet>
        </p:panelGrid>
    </p:dialog>
</h:form>

<h:form>
    <p:dialog id="editClient" header="Edit client" widgetVar="edit"
        modal="true" resizable="false">
        <h:panelGrid columns="2" id="displayClient">
            <h:outputLabel for="id" value="Id: " />
        <h:outputText id="id" value="#{client.clientId}" label="id" />

            <h:outputLabel for="name" value="Name: " />
            <p:inputText id="name" value="#{client.name}" label="name"
                required="true" />

            <h:outputLabel for="address" value="Address: " />
        <p:inputText id="address" value="#{client.address}" label="address"
                required="true" />

            <h:outputLabel for="email" value="Email: " />
        <p:inputText id="email" value="#{client.email}" label="email"
                required="true" />

            <f:facet name="footer">
                <p:commandButton 
                        action="#{clientBeans.updateClient}" value="Save"
                    icon="ui-icon-check" style="margin:0"> 
                           </p:commandButton>
            </f:facet>
        </h:panelGrid>
    </p:dialog>
</h:form>

<h:form>
    <p:dialog id="deleteClient"
        header="Are you sure you want to delete this client?"
        widgetVar="remove" modal="true" resizable="false">
        <p:panelGrid columns="2">
            <h:outputLabel for="id" value="Id: " />
        <h:outputText id="id" value="#{client.clientId}" label="id" />

            <h:outputLabel for="name" value="Name: " />
        <h:outputText id="name" value="#{client.name}" label="name" />

            <h:outputLabel for="address" value="Address: " />
    <h:outputText id="address" value="#{client.address}" label="address" />

            <h:outputLabel for="email" value="Email: " />
    <h:outputText id="email" value="#{client.email}" label="email" />

            <f:facet name="footer">
        <p:commandButton action="#{clientBeans.confirmDeleteClient}"
                value="Delete" icon="ui-icon-circle-close" />
            </f:facet>
        </p:panelGrid>
    </p:dialog>
</h:form>

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    我没有浏览所有代码,但我可以很快发现您误用了&lt;p:commandButton&gt;:属性type="button" 应该只用于调用自定义JavaScript(客户端)代码,因此它与actionactionListener,适用于调用服务器操作。移除此属性并确保调用托管 bean 的方法 #{clientBeans.editClient}

    查看 PrimeFaces 文档,type="button" 的 commandButtons 称为“Push Buttons”。

    其次,使用oncomplete 而不是onclick,以便在ajax 方法完成其工作后显示对话框

    【讨论】:

    • @perissf 很好看。我还要补充一点,通常对话框必须将其形式嵌套在内部,而不是相反。在某些情况下,这很重要。
    • @Владислав 修复你的代码,删除动作监听器的不一致,最小化你的代码,运行它并在你遇到麻烦时返回。
    • 我明白了!在对话框中而不是 client.name 应该是 clientBeans.client.name。现在有效!
    • 很高兴问题得到解决。但是...为什么您没有将答案标记为已接受?您是否认真认为如果没有答案中描述的更大缺陷,您的修复会起作用?没有这些修复它可以工作吗?如果是,请告诉我,以便我可以编辑/删除它!
    • 是的,实际上我删除了你所说的属性 type="button"。
    猜你喜欢
    • 2013-09-24
    • 2014-07-03
    • 1970-01-01
    • 2013-09-21
    • 2014-01-06
    • 2017-12-03
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多