I have an ArrayList
of a custom object (ArrayList<Cell> allCells
) and a sublist
of allList (ArrayList<Cell> checkedCells
) . What I would like to do is display the allCells arraylist in a p:selectManyMenu
and show the objects of my sublist checkedCells as checked
in the menu.
I know that the above question is supposed to have already been answered here but I think that the answer is of low quality and unclear for such a basic question, and additionally the OP does not make use of the converter
which I need to use.
Cell class
public class Cell{
private String value;
// rest member variables getters setters default constructor ommitted
}
CellConverter class
@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 code
<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>
Unfortunately, even though the list is shown properly in the selectmanymenu, the elements belonging to the checkedCells
list are not checked
Assuming the checkedCells
list includes cells with values cell2 and cell4
I would like the list to be shown like this
but the list is shown like this (even though i debugged the `checkedCells variable and is actually includes this values)
Primefaces version: 7.0
Thanks in advance.