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

查看:21
本文介绍了使用 p:remoteCommand 更新 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>

当给定的被按下时,关联的监听器submitAction()被调用,最后 仅在验证成功时更新.

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: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.

如果 被删除并且给定的 改变如下,

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: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.

如何防止 执行验证,当 中的一行使用

依次更新 持有有问题的 ?

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> 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:panel id="panel"> 通过 <p:ajax> 里面.有时是必要的),给定的 <p<p:panel id="panel"> 中的 :inputText> 导致验证其边框变为红色,这意味着违反了应该发生的相关验证.

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 个(一个来自面板的提交,一个来自 ).

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>).

原因是 本身.它的 process 属性 defaults@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:

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

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

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