3

I need to show the selectManyCheckbox list in 4 columns, but the problem is that this component generates a table, so I do not have any idea of how to define the columns.

I am working with PF 3.4, I can't upgrade to PF 4.x. Do you guys have any solution for this?

EDITED

Now i have this in my code

<h:form id="formAdminAccesosXPerfil">

    <h:panelGrid title="Accesos" columns="5">

    <c:forEach items="#{accesosXPerfilMB.listadoAcceso}" var="availableItem" varStatus="loop">

             <h:panelGroup>
                <p:selectBooleanCheckbox id="box_#{loop.index}" value="#{accesosXPerfilMB.checkBoxItems[availableItem]}" />
                <h:outputLabel for="box_#{loop.index}" value="#{availableItem.nombre}" />
            </h:panelGroup>
    </c:forEach>    

    </h:panelGrid>

The Managebean which is @ViewScoped

I changed the method suggested because it did not work for me...

from:

public void save() {
List<E> selectedItems = checkboxItems.entrySet().stream()
    .filter(e -> e.getValue() == Boolean.TRUE)
    .map(e -> e.getKey())
    .collect(Collectors.toList());
// ...

}

to this:

public void guardarAccesos(){
    try {
        System.out.println("Size: "+getCheckBoxItems().entrySet().size());

        for(BpAcceso acceso:getCheckBoxItems().keySet()){
            System.out.println("Acceso Seleccionado: "+acceso.getNombre());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

But i do not get any selected item on the hashMap. Just to make sure i am using jdk1.6

DuSant
  • 970
  • 12
  • 25

1 Answers1

6

Generate a bunch of selectBooleanCheckbox components in a <c:forEach> loop in a <h:panelGrid columns="X"> and alter the model from List<E> to Map<E, Boolean>.

So, instead of

private List<E> selectedItems;
private List<E> availableItems;
<p:selectManyCheckbox value="#{bean.selectedItems}">
    <f:selectItems value="#{bean.availableItems}" />
</p:selectBooleanCheckbox>

do

private Map<E, Boolean> checkboxItems;
private List<E> availableItems;

@PostConstruct
public void init() {
    checkboxItems = new HashMap<>();
}
<h:panelGrid columns="4">
    <c:forEach items="#{bean.availableItems}" var="availableItem" varStatus="loop">
        <h:panelGroup>
            <p:selectBooleanCheckbox id="box_#{loop.index}" value="#{bean.checkboxItems[availableItem]}" />
            <h:outputLabel for="box_#{loop.index}" value="#{availableItem}" />
        </h:panelGroup>
    </c:forEach>
</h:panelGrid>
public void save() {
    List<E> selectedItems = checkboxItems.entrySet().stream()
        .filter(e -> e.getValue() == Boolean.TRUE)
        .map(e -> e.getKey())
        .collect(Collectors.toList());
    // ...
}

Note that an <ui:repeat> is not applicable for the reasons explained here JSTL in JSF2 Facelets... makes sense?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I could show the 4 column list, but i cant get the selected values in the save method, I'm using jdk1.6. Aparently the list of checkboxItems is not filling with the selected ones. I calculate the size and i got "0" – DuSant Oct 28 '15 at 16:01
  • It works for me in a scratchpad project. To avoid the obvious, you placed it in a `` and your data loading logic takes postbacks into account? (i.e. `availableItems` is view scoped). – BalusC Oct 28 '15 at 19:38
  • Yes it is view scoped, i will edit the post so you can see my code @BalusC – DuSant Oct 28 '15 at 20:03
  • Is this the only form in your test page? Which JSF impl/version? Mojarra versions older than 2.1.18 had trouble with JSTL + @ViewScoped. – BalusC Oct 28 '15 at 21:16
  • The Jsf version es 2.1.2, so I am in troubles :/ – DuSant Oct 28 '15 at 21:47
  • That's indeed ancient. The work around would be to disable partial state saving (see also that JSTL link in end of answer), but this may have some impact. You'd best just upgrade JSF to latest 2.1.x available (which is 2.1.29 as of now). – BalusC Oct 28 '15 at 21:51