【问题标题】:Update Primefaces DataTable only if validation succeeds仅在验证成功时更新 Primefaces DataTable
【发布时间】:2013-07-08 08:28:49
【问题描述】:

例如,我有一些输入字段,例如,

<p:panel id="panel" closable="false" toggleOrientation="horizontal" toggleable="true" header="New">
    <p:focus context="panel"/>
    <p:watermark for="txtCountryName" value="Enter a valid country name."/>
    <p:watermark for="txtCountryCode" value="Enter a valid country code."/>
    <p:messages id="systemMessages" globalOnly="true" redisplay="false" showDetail="true" showSummary="true" autoUpdate="false" closable="true"/>
    <p:messages id="specificSystemMessages" for="paramId" globalOnly="false" redisplay="false" showDetail="true" showSummary="false" autoUpdate="false" closable="true"/>

        <h:panelGrid id="panelGrid" columns="3" cellpadding="5">
            <p:outputLabel for="txtCountryName" value="Country"/>
            <p:inputText id="txtCountryName" value="#{countryManagedBean.txtCountryName}" label="Country name" required="true" maxlength="45">
                <f:validateLength minimum="2" maximum="45"/>
            </p:inputText>
            <p:message for="txtCountryName" showSummary="false"/>

            <p:outputLabel for="txtCountryCode" value="Country Code"/>
            <p:inputText id="txtCountryCode" value="#{countryManagedBean.txtCountryCode}" required="true" maxlength="45" label="Country code">
                <f:validateLength minimum="2" maximum="45"/>
            </p:inputText>
            <p:message for="txtCountryCode" showSummary="false"/>


            <p:commandButton id="btnSubmit" update="dataTable panel messages" actionListener="#{countryManagedBean.insert}" icon="ui-icon-check" value="Save"/>
</h:panelGrid>
</p:panel>

还有一个DataTable如下。

<p:panel id="dataTablePanel" toggleable="true" toggleOrientation="horizontal" closable="false" header="Data">
    <p:dataTable id="dataTable" var="row" value="#{countryManagedBean}"
                 lazy="true"
                 pageLinks="10"
                 paginator="true"
                 sortMode="multiple"
                 resizableColumns="true"
                 sortOrder="descending"
                 editable="true"
                 filterEvent="keyup"
                 selection="#{countryManagedBean.selectedValues}"
                 rowsPerPageTemplate="5,10,15"
                 rows="10"
                 rowKey="#{row.countryId}"
                 rowIndexVar="rowIndex"
                 rowStyleClass="#{row.countryId eq countryManagedBean.id? 'selected-data-row' : null}"
                 editMode="row">


                 ...
                 ...
                 ...
    </p:dataTable>
</p:panel>

当这个命令按钮时,

<p:commandButton id="btnSubmit" update="dataTable panel messages" actionListener="#{countryManagedBean.insert}" icon="ui-icon-check" value="Save"/>

点击后,如果一行满足所有验证规则并且使用update="dataTable panel messages" 属性更新DataTable,则会将一行添加到基础数据库中。

当且仅当满足这些输入字段上指定的所有验证条件并且实际创建行时,我才想更新此 DataTable。

如果这些验证规则中的任何一个失败,则不应再更新此 DataTable,这是非常不必要的,并且会导致执行一些代价高昂的 JPA 标准和/或 JPQL 查询。这可能吗,怎么做?

这是 Primefaces 3.5。

【问题讨论】:

    标签: jsf jsf-2 primefaces


    【解决方案1】:

    在这种情况下尝试使用远程命令。 如下所示更改您的提交按钮。

    <p:commandButton id="btnSubmit" action="#{countryManagedBean.insert}" icon="ui-icon-check" value="Save" oncomplete="handleRequest(xhr, status, args)"/>
    

    如下图添加p:remoteCommand。

    &lt;p:remoteCommand name="updateTable" update="dataTable"&gt;

    此远程命令将用于更新您的表格。

    是的,在js下面添加。

    <script type="text/javascript">  
            function handleRequest(xhr, status, args) {  
                if(!args.validationFailed) {  
                    updateTable();
                    }   
               }  
        </script> 
    

    HTH

    【讨论】:

    • panelmessages 以及 dataTable 也应该更新。这不会发生。否则,它会起作用。如何更新panelmessages 以及dataTable
    • 使用&lt;p:remoteCommand name="updateTable panel messages" update="dataTable"&gt;
    • &lt;p:remoteCommand name="updateTable panel messages" update="dataTable"/&gt; 不会更新 panelmessages。它只会在成功(验证成功时)更新dataTable
    • 很抱歉出现错误,请使用&lt;p:remoteCommand name="updateTable" update="dataTable panel messages"&gt;
    • panelmessages 是 ID。 messages&lt;p:growl&gt; 的 id,例如 &lt;p:growl id="messages" globalOnly="true" showDetail="true" showSummary="true" sticky="false" autoUpdate="false"/&gt;。不过我没有在问题中发帖。
    【解决方案2】:

    尝试将此代码添加到您的 bakebean countryManagedBean 在插入函数的末尾或您检查验证的任何位置。

    RequestContext context = RequestContext.getCurrentInstance();
    context.addCallbackParam("validationFailed", "true");
    

    这将创建您使用 javascript 检查的回调参数。

    【讨论】:

    • 更简单的是从 bean 显式更新数据表。因为只有在验证成功时才会调用该方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-19
    • 1970-01-01
    • 2016-03-01
    • 2014-09-02
    • 2015-12-03
    • 2012-03-27
    • 2018-07-15
    相关资源
    最近更新 更多