我在页面上有一个输入字段和一个表格。每当一个字段中的数字发生变化时(ajax监听器更改事件) - 我希望相应地更改行数。在primefaces中单元格编辑后表格消失(也未保存数据)
我将表格指向Backing bean中的Arraylist
,并播放其大小。
但是当我在表中编辑一个值时 - 按下回车键,表格消失,并且只有当我再次更改字段值时才会重新出现。
作为奖励,在调试模式下,我看到支持arraylist总是有空值。
这里去bean代码:
@ManagedBean(name = "bean")
@ViewScoped
public class TestBean {
private List<String> hostnames = new ArrayList<String>();
private int copiesQuantityJustCopy;
public int getCopiesQuantityJustCopy() {
return copiesQuantityJustCopy;
}
public void setCopiesQuantityJustCopy(int copiesQuantityJustCopy) {
this.copiesQuantityJustCopy = copiesQuantityJustCopy;
}
public List<String> getHostnames() {
return hostnames;
}
public void setHostnames(List<String> hostnames) {
this.hostnames = hostnames;
}
public void onUpdateCount() {
int hostnamesCount = hostnames.size();
System.out.println("delta = " + hostnamesCount + " - " + copiesQuantityJustCopy);
int delta = hostnamesCount - copiesQuantityJustCopy;
if (delta > 0)
for (int i = 1; i < delta; i++)
hostnames.remove(delta);
if (delta < 0)
for (int i = 0; i < -delta; i++)
hostnames.add("");
}
}
和视图代码:
<h:form id="form1">
<p:inputMask mask="9?99" maxlength="3" placeHolder=" " value="#{bean.copiesQuantityJustCopy}">
<p:ajax event="change" listener="#{bean.onUpdateCount()}" update=":form1:hostnamesTable" />
</p:inputMask>
<p:outputPanel id="hostnamesTable" rendered="true">
<p:dataTable value="#{bean.hostnames}" var="hostname"
id="hostnames" editable="true" editMode="cell">
<p:ajax event="cellEdit" update=":form1:hostnamesTable" process="@this" />
<p:column>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{hostname}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{hostname}" style="width:96%" />
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</p:outputPanel>
</h:form>
“*消失*”意味着在这种情况下是什么?表格是否变空或它从HTML DOM树中自行消失?您还需要在用@ PostConstruct注释的方法中填充列表,例如在页面加载并实例化bean时,让列表初始化。 (视图范围的bean也需要'java.io.Serializable'接口与私有静态最终长字段'serialVersionUid'一起实现)。 – Tiny
并检查正确的Viewscoped包与ManagedBean – Kukeltje
@Tiny嗨!我试过了:1)实现可序列化:对行为没有影响; 2)调查开发人员工具inspector:只看到div这个名字,没有更多:
, – Anton