使用p:remoteCommand更新p:dataTable [英] Using a p:remoteCommand to update a p:dataTable

查看:167
本文介绍了使用p:remoteCommand更新p:dataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下具有一个<p:inputText>和一个<p:dataTable>仅具有两列的XHTML代码.

Given the following XHTML code that has one <p:inputText> and a <p:dataTable> having only two columns.

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

<p:panel id="panel">
    <p:inputText id="txtValue" value="#{testManagedBean.txtValue}"
                 required="true"/>
    <p:message for="txtValue" showSummary="false"/>
    <p:commandButton actionListener="#{testManagedBean.submitAction}"
                     oncomplete="if(!args.validationFailed) {updateTable();}" 
                     update="panel" value="Submit"/>
</p:panel>

<p:panel id="dataTablePanel" header="Data">
    <p:dataTable id="dataTable" var="row" value="#{testManagedBean}"
                 lazy="true"
                 pageLinks="10"
                 editable="true"
                 rowsPerPageTemplate="5,10,15"
                 rows="10"
                 rowKey="#{row.catId}"
                 editMode="row">

        <p:ajax event="rowEdit" update=":form:panel dataTable"
                listener="#{testManagedBean.onRowEdit}"/>

        <p:column id="id" headerText="Id">
            <h:outputText value="#{row.catId}"/>
        </p:column>

        <p:column id="catName" headerText="Category">
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{row.catName}"/>
                </f:facet>
                <f:facet name="input">
                    <p:inputText value="#{row.catName}" label="Category">
                        <f:validateLength minimum="2" maximum="45"/>
                    </p:inputText>
                </f:facet>
            </p:cellEditor>
        </p:column>

        <p:column headerText="Edit" width="100">
            <p:rowEditor/>
        </p:column>
    </p:dataTable>
</p:panel>

当按下给定的<p:commandButton>时,将调用关联的侦听器submitAction(),最后,只有在验证成功的情况下,<p:remoteCommand> 才会更新<p:dataTable> .

When the given <p:commandButton> is pressed, the associated listener submitAction() is invoked and finally the <p:dataTable> is updated by <p:remoteCommand> only if validations succeed.

执行完此操作后,如果给定的<p:dataTable>保留的行被更新(这又会通过<p:dataTable>内的<p:ajax>来更新<p:panel id="panel">.有时是必要的),<p:panel id="panel">导致验证,其边框变为红色,表示违反了应该发生的相关验证.

After doing this, if a row held by the given <p:dataTable> is updated (which in turn, updates <p:panel id="panel"> via <p:ajax> inside <p:dataTable>. It is sometimes necessary), the given <p:inputText> in <p:panel id="panel"> causes validation its borders turn red implying violating the associated validation that should not happen.

如果<p:remoteCommand>被删除并且给定的<p:commandButton>如下更改,则

If <p:remoteCommand> is removed and the given <p:commandButton> is changed like as follows,

<p:commandButton actionListener="#{testManagedBean.submitAction}"
                 update="panel dataTable" value="Submit"/>

删除oncomplete="if(!args.validationFailed) {updateTable();}"

,并且update属性从update="panel"更改为update="panel dataTable",然后,当更新<p:dataTable>中的一行时,<p:inputText>不会引起验证.

and the update attribute is changed from update="panel" to update="panel dataTable" then, the <p:inputText> does not cause validations, when a row in <p:dataTable> is updated.

当使用<p:ajax>更新<p:dataTable>中的一行时,如何阻止<p:inputText>执行验证?

How to prevent <p:inputText> from performing validations, when a row in <p:dataTable> is updated using <p:ajax> which, in turn updates <p:panel> holding the <p:inputText> in question?

<p:remoteCommand>本身不能省略.仅在没有违反验证的情况下才有必要更新<p:dataTable>.否则,即使存在验证错误,也不必要执行昂贵的业务服务.

<p:remoteCommand> itself in this case, cannot be omitted. It is necessary to update <p:dataTable> only if no validations are violated. Otherwise, costly business services are executed unnecessarily, even though there are validation error(s).

关联的JSF托管bean(尽管完全没有必要).

The associated JSF managed bean (though completely unnecessary).

@ManagedBean
@ViewScoped
public final class TestManagedBean extends LazyDataModel<Category> implements Serializable
{
    @EJB
    private final CategoryBeanLocal categoryService = null;
    private String txtValue;  //Getter and setter.
    private static final long serialVersionUID = 1L;

    @Override
    public List<Category> load(int first, int pageSize, List<SortMeta> multiSortMeta, Map<String, Object> filters) {
        setRowCount(categoryService.rowCount().intValue());
        return categoryService.getList(first, pageSize, multiSortMeta, filters);
    }

    public void submitAction() {
        System.out.println("txtValue : " + txtValue);
        txtValue = null;
    }

    public void onRowEdit(RowEditEvent event) {
        System.out.println("onRowEdit() called.");
    }
}

推荐答案

执行此操作后,如果给定的<p:dataTable>保留的行被更新(这又会通过<p:dataTable>中的<p:ajax>来更新<p:panel id="panel">.有时是必需的),给定的<p:panel id="panel">中的>导致验证,其边框变为红色,表示违反了应该发生的相关验证.

After doing this, if a row held by the given <p:dataTable> is updated (which in turn, updates <p:panel id="panel"> via <p:ajax> inside <p:dataTable>. It is sometimes necessary), the given <p:inputText> in <p:panel id="panel"> causes validation its borders turn red implying violating the associated validation that should not happen.

这不是正在发生的事情.如果是这样,您将在网络监视器中看到3个HTTP请求.但是只有2个(一个来自面板提交,一个来自<p:remoteCommand>).

This isn't what is happening. If that were true, you'd have seen 3 HTTP requests in the network monitor. But there are only 2 (one from the submit of the panel and one from the <p:remoteCommand>).

原因是<p:remoteCommand>本身.其process属性默认值@all( 整体视图").您也可以通过检查网络监视器中的javax.faces.partial.execute请求参数来确认这一点.它说@all.换句话说,整个表单也将被提交/处理,包括那些空的输入.

The cause is the <p:remoteCommand> itself. Its process attribute defaults to @all ("whole view"). You can also confirm this by inspecting the javax.faces.partial.execute request parameter in the network monitor. It says @all. In other words, the entire form is also submitted/processed, including those empty inputs.

您需要将其显式设置为@this:

You need to explicitly set it to @this:

<p:remoteCommand name="updateTable" process="@this" update="dataTable"/>

这篇关于使用p:remoteCommand更新p:dataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆