1

I'm trying to have 3 checkboxes (a,b & c) in a group where if b is selected, a is also automatically selected. But when I am using the valueChanbgeListener argument, it is only giving me the information of the selectedItem list before committing the most current change to the list. Also, this change needs to be reflected in the front end as well - which means I have to update the ui-component after the backing bean handleChange() method's work is done.

JSF XHTML code:

<h:form id="form" >
    <p:growl id="growl" showDetail="true" sticky="true" /> 

    <h:panelGrid> 
        <p:selectManyCheckbox value="#{superUser.selectedOptions}" 
                              layout="pageDirection" 
                              onchange="submit()" 
                              valueChangeListener="#{superUser.handleChange}">  
            <f:selectItems value="#{superUser.allOptions}" />
        </p:selectManyCheckbox>
    </h:panelGrid>

    <p:commandButton value="Submit" update="display" oncomplete="dlg.show()" />

    <p:dialog header="Selected Values" 
              modal="true" 
              showEffect="fade" 
              hideEffect="fade" 
              widgetVar="dlg">  
        <p:outputPanel id="display">
            <p:dataList value="#{formBean.selected}" var="item">  
                #{item}
            </p:dataList>  
        </p:outputPanel>  
    </p:dialog> 

</h:form>

Backing bean:

@ManagedBean (name="superUser")
@ViewScoped
public class SuperUserBacking implements Serializable {    
    ...
    ...

    private HashMap<Integer, String> monthMap;

    private Map<String, String> allOptions;
    private List<String> selectedOptions;

    public void initializeEventCopyFunction(){
        initializeCheckList();
    }

    private void initializeCheckList(){
        allOptions = new HashMap<String, String>();  
        allOptions.put(this._RACE, this._RACE);  
        allOptions.put(this._COLUMNS, this._COLUMNS);  
        allOptions.put(this._MESSAGE, this._MESSAGE);
        allOptions.put(this._STAFF, this._STAFF);
        allOptions.put(this._EQUIPMENT, this._EQUIPMENT);
        selectedOptions = new ArrayList<String>();
    }

    public void handleChange(){
        LOGGER.info("handleChangeMethod Called");
        if(selectedOptions==null)
            return;

        System.out.println("\n");
        for(String selected : selectedOptions){
            System.out.println("Selected : "+ selected);
        }
        System.out.println("\n");
   }
  //GETTERS AND SETTERS
  ...
}
2
  • I don't understand the problem completely, but try to replace all this: onchange="submit()" valueChangeListener="#{superUser.handleChange}" with <p:ajax listener="{superUser.handleChange}" update="(id of components to update)" />. Put this p:ajax just below the f:selectItems. Maybe the handleChange()-method needs an AjaxBehaviourEvent as parameter, I don't remember Sep 23, 2014 at 5:04
  • @Jaqen H'ghar Thanks so much! This works exactly the way I needed it to work. Now I can dynamically toggle checkboxes in the backing bean.
    – Tanvir
    Sep 23, 2014 at 15:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.