我有一个可编辑的数据表,包含“数据类型”列。编辑此列时,使用 selectOneMenu 选择值“String”、“Number”或“Date”。当我进入编辑模式时,“数据类型”列设置为“字符串”(数据类型列表的第一项),但我希望它是该列的当前值(如 Primefaces 展示中: Click - 例如,如果我单击第二个表的第一行和第三列,则应选择“Fiat”,而不是 selectOneMenu 中的第一项 - “BMW” - 就像我的情况一样)。
我的代码可能有什么问题?
xhtml:
<p:column headerText="Type" >
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{item.dataType.code}" />
</f:facet>
<f:facet name="input">
<p:selectOneMenu value="#{item.dataType}" converter="myConverter" >
<f:selectItems value="#{backingBean.dataTypeList}" var="dt" itemLabel="#{dt.code}" itemValue="#{dt}" />
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</p:column>
数据类型类:
public class DataType implements Serializable {
private BigDecimal id;
private String code;
private String descr;
// Getters+Setters.
}
使用 Primefaces 5.1。
如果需要任何其他信息,我可以为您提供。
最佳答案
前提是 myConverter
标识的 Converter
实现是 properly如果相关实体 DataType
没有 equals()
(和 hashCode()
),则可能会发生这种情况正确实现。
只需在您的实体中添加/自动生成它们即可。它至少应该是这样的:
@Override
public int hashCode() {
return (id != null)
? (getClass().hashCode() + id.hashCode())
: super.hashCode();
}
@Override
public boolean equals(Object other) {
return (other != null && getClass() == other.getClass() && id != null)
? id.equals(((DataType) other).id)
: (other == this);
}
这也应该立即解决 "Validation Error: Value is not valid"提交表单时出错。
您的所有实体都应该实现它们。为了避免重复样板,请考虑创建一个所有实体都从中扩展的基础实体。另请参阅Implement converters for entities with Java Generics .
关于jsf - SelectOneMenu 编辑模式下的默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27858650/