1

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

enter image description here

but the list is shown like this (even though i debugged the `checkedCells variable and is actually includes this values)

enter image description here

Primefaces version: 7.0

Thanks in advance.

NickAth
  • 1,015
  • 1
  • 7
  • 22
  • Does it work if not using the `p:column`? Does it work with other similar components (select chekbox)? Does it work with a plain jsf `h:selectManyMenu`? Or the checkbox ones? – Kukeltje Aug 09 '19 at 16:25
  • If not, you can 'generalize' your search... – Kukeltje Aug 09 '19 at 16:26
  • No they do not :/ . I also tried with `selectManyCheckbox` but still no luck, any idea what could be the issue? – NickAth Aug 09 '19 at 16:38
  • @Kukeltje bingo! Indeed what I needed was a custom implementation of the `equals` method in the `Cell` object. Thank you ! – NickAth Aug 09 '19 at 16:47

0 Answers0