我有ArrayList
一个自定义对象 ( ArrayList<Cell> allCells
) 和一个sublist
allList( ArrayList<Cell> checkedCells
)。我想做的是在 a中显示allCells 数组列表,并在菜单中p:selectManyMenu
显示我的子列表checkedCells的对象。checked
我知道上面的问题应该已经在这里得到了回答,但我认为对于这样一个基本问题,答案质量低下且不清楚,而且 OP 没有使用converter
我需要使用的。
细胞类
public class Cell{
private String value;
// rest member variables getters setters default constructor ommitted
}
细胞转换器类
@FacesConverter(forClass= Cell.class, value = "cellConverter")
public class CellConverter implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.isEmpty()) {
return null;
}
else{
CellBean cellBean = (CellBean) context.getViewRoot().getViewMap().get("cellBean");
for(Cell cell : cellBean.getAllCells()){
String s = cell.getValue();
if(s.equals(value)){
return cell;
}
}
}
return null;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value == null) {
return "";
}
if (!(value instanceof Cell)) {
throw new ConverterException("sth went wrong");
}
Cell cell = (Cell) value;
return cell.getValue();
}
selectManyMenu 代码
<p:selectManyMenu converter="cellConverter" value="#{cellBean.checkedCells}"
var="cell" filter="true" filterMatchMode="contains" showCheckbox="true">
<f:selectItems value="#{cellBean.allCells}"
var="entity" itemLabel="#{entity.value}" itemValue="#{entity}" />
<p:column>
<h:outputText value="#{cell.value}" />
</p:column>
</p:selectManyMenu>
不幸的是,即使列表在 selectmanymenu 中正确显示,属于checkedCells
列表的元素也不会被检查
假设checkedCells
列表包含具有值的单元格cell2 and cell4
我希望列表像这样显示
但是列表是这样显示的(即使我调试了`checkedCells 变量并且实际上包含了这个值)
Primefaces 版本:7.0
提前致谢。