0

举个例子,我有一些输入字段,例如,

<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。

4

2 回答 2

4

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

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

添加 p:remoteCommand 如下所示。

<p:remoteCommand name="updateTable" update="dataTable">

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

是的,在js下面添加。

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

高温高压

于 2013-07-08T08:41:40.543 回答
0

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

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

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

于 2016-08-12T09:03:05.367 回答