0

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
James
  • 1,471
  • 3
  • 23
  • 52
  • Just set the `selectedRoles`? – BalusC Jun 27 '16 at 13:52
  • The 'selectedRoles' is set, when debugged I can see the list contains 2 elements, which relate to the two checkboxes I want to be checked, but they still render unchecked and I'm struggling to discover why. – James Jun 27 '16 at 14:00

1 Answers1

1

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.

Youans
  • 4,801
  • 1
  • 31
  • 57
  • 1
    Thank you, this cleared things up a little and helped me realise I was doing something stupid :) – James Jun 27 '16 at 14:11
  • @James Not at all , JSF is tricky for first time , But by the time she will teach you to do magic :) – Youans Jun 27 '16 at 14:13
  • 1
    Let me guess, you filled it with whole `Role` objects instead of `String` names? – BalusC Jun 27 '16 at 14:14