jsfprimefacesinitializationselectmanycheckbox

How to make checkboxes display checked on display


I am using the following guide to implement many checkboxes into my tool: http://www.primefaces.org/showcase/ui/input/manyCheckbox.xhtml

I have successfully done so, but what I want to happen is I want the checkboxes to be already ticked on display if a user has that role, and not checked if they don't. The idea being, an admin can check/uncheck which roles they want a user to have, with the ones they already have checked.

Here is what I have tried so far:

<p:selectManyCheckbox id="roles" value="#{usersView.selectedRoles}" layout="grid" columns="3">
    <f:selectItems value="#{rolesView.roles}" var="role" itemLabel="#{role.name}" itemValue="#{role.name}" />
</p:selectManyCheckbox>

In my usersView, the selectedRoles is simply an ArrayList of all of the roles a user has.

Any help is appreciated.


Solution

  • If you have selectedRoles match the itemValue one by one on same order then Bingo your checkboxes will be auto checked

    for ex:

    selectedRoles={'Mark','Jack'}

    and in your #{rolesView.roles} If you have the same #{'Mark','Jack'} as #{role.name} at the same order

    Then simply, You got your self auto checked checkboxes.