primefacesselectmanycheckbox

select all p:selectManyCheckbox


I'd like to create a button to select all checkboxes but I couldn't find one example that works for me. I'm using primefaces.

     <p:selectManyCheckbox widgetVar="sex" id="sexta" value="#{solicitacaoAcompanhamentoMB.selectedHorariosSexta}" layout="pageDirection">  
        <f:selectItem itemLabel="12:00" itemValue="Sexta-12:00" />  
        <f:selectItem itemLabel="12:30" itemValue="Sexta-12:30" /> 
        <f:selectItem itemLabel="13:00" itemValue="Sexta-13:00" /> 
        <f:selectItem itemLabel="13:30" itemValue="Sexta-13:30" /> 
        <f:selectItem itemLabel="14:00" itemValue="Sexta-14:00" />  
        <f:selectItem itemLabel="14:30" itemValue="Sexta-14:30" /> 
        <f:selectItem itemLabel="15:00" itemValue="Sexta-15:00" />  
        <f:selectItem itemLabel="16:00" itemValue="Sexta-16:00" />  
        <f:selectItem itemLabel="16:30" itemValue="Sexta-16:30" />
        <f:selectItem itemLabel="17:00" itemValue="Sexta-17:00" />  
        <f:selectItem itemLabel="17:30" itemValue="Sexta-17:30" /> 
        <f:selectItem itemLabel="18:00" itemValue="Sexta-18:00" />  
        <f:selectItem itemLabel="18:30" itemValue="Sexta-18:30" /> 
        <f:selectItem itemLabel="19:00" itemValue="Sexta-19:00" />  
        <f:selectItem itemLabel="19:30" itemValue="Sexta-19:30" /> 
        <f:selectItem itemLabel="20:00" itemValue="Sexta-20:00" />  
        <f:selectItem itemLabel="20:30" itemValue="Sexta-20:30" /> 
        <f:selectItem itemLabel="21:00" itemValue="Sexta-21:00" />  
        <f:selectItem itemLabel="21:30" itemValue="Sexta-21:30" /> 
        <f:selectItem itemLabel="22:00" itemValue="Sexta-22:00" />  
        <f:selectItem itemLabel="22:30" itemValue="Sexta-22:30" /> 
     </p:selectManyCheckbox>

Solution

  • You can add commandLink

    <p:commandLink id="selectAll" update="sexta" actionListener="#{yourBean.selectAll}" process="@this,locations">
      <h:outputText value="Select all" />
    </p:commandLink>
    

    And method in Java (in yourBean class):

    public void selectAll(ActionEvent actionEvent) {
        selectedHorariosSexta.clear();
        int startHour = 12;
        for (int i = 0; i < 22; i++) {
            if (i % 2 == 0) {
                selectedHorariosSexta.add("Sexta-" + startHour + ":00");
            }else {
                selectedHorariosSexta.add("Sexta-" + startHour + ":30");
                startHour++;
            }
        }       
    }