给定以下 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>
当给定
<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"/>
移除
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.");
}
}
最佳答案
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>
)。原因是
<p:remoteCommand>
本身。它的 process
属性 defaults至 @all
(“整体 View ”)。您也可以通过检查 javax.faces.partial.execute
来确认这一点。网络监视器中的请求参数。它说 @all
.换句话说,整个表单也被提交/处理,包括那些空的输入。您需要将其显式设置为
@this
:<p:remoteCommand name="updateTable" process="@this" update="dataTable"/>
关于jsf - 使用 p :remoteCommand to update a p:dataTable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23443463/