使用ap:remoteCommand更新ap:dataTable

Tin*_*iny 4 jsf primefaces jsf-2.2 remotecommand

给定以下XHTML代码,其中包含一个<p:inputText><p:dataTable>只有两列.

<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>
Run Code Online (Sandbox Code Playgroud)

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

这样,如果给定召开行之后<p:dataTable>被更新(这反过来,更新<p:panel id="panel">通过<p:ajax>里面<p:dataTable>,它有时是必要的),给定<p:inputText><p:panel id="panel">原因验证其边界变为红色言下之意违反相关的验证应该是不会发生的.

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

<p:commandButton actionListener="#{testManagedBean.submitAction}"
                 update="panel dataTable" value="Submit"/>
Run Code Online (Sandbox Code Playgroud)

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

并且当更新行时,update属性从更改update="panel"update="panel dataTable"然后,<p:inputText>不会导致验证<p:dataTable>.

如何防止<p:inputText>执行验证,当一行<p:dataTable>更新时使用<p:ajax>哪一行,反过来更新<p:panel>持有<p:inputText>问题?

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


关联的JSF托管bean(虽然完全没必要).

@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.");
    }
}
Run Code Online (Sandbox Code Playgroud)

Bal*_*usC 5

这样,如果给定召开行之后<p:dataTable>被更新(这反过来,更新<p:panel id="panel">通过<p:ajax>里面<p:dataTable>,它有时是必要的),给定<p:inputText><p:panel id="panel">原因验证其边界变为红色言下之意违反相关的验证应该是不会发生的.

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

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

您需要将其明确设置为@this:

<p:remoteCommand name="updateTable" process="@this" update="dataTable"/>
Run Code Online (Sandbox Code Playgroud)