Question

I have a multiselect listbox and depending on how many elements user will choose I have to show same number of checkboxes. I'm using jsf and primefaces /spring webflow. How I can do that? Any examples? And I have to make one checkbox which allow to check all generated checkboxes using one click.

Was it helpful?

Solution

I have a multiselect listbox

Thus, a <h:selectManyListbox> with a fixed <f:selectItems> value.

<h:selectManyListbox id="listbox" value="#{bean.selectedListboxItems}">
    <f:selectItems value="#{bean.availableListboxItems}" />
</h:selectManyListbox>

With something like:

private List<String> availableListboxItems;
private List<String> selectedListboxItems;

@PostConstruct
public void init() {
    availableListboxItems = new ArrayList<String>();
    availableListboxItems.add("menu item 1");
    availableListboxItems.add("menu item 2");
    availableListboxItems.add("menu item 3");
}

and depending on how many elements user will choose I have to show same number of checkboxes

Thus, just prepopulate the <f:selectItems> of a <h:selectManyCheckbox> based on the value of the <h:selectManyListbox>.

<h:commandButton value="Generate checkboxes">
    <f:ajax execute="listbox" listener="#{bean.generateCheckboxes}" render="checkboxes" />
</h:commandButton>
<h:selectManyCheckbox id="checkboxes" value="#{bean.selectedCheckboxItems}">
    <f:selectItems value="#{bean.availableCheckboxItems}" />
</h:selectManyCheckbox>

with something like

private List<String> availableCheckboxItems;
private List<String> selectedCheckboxItems;

public void generateCheckboxes() {
    availableCheckboxItems = new ArrayList<String>();

    for (int i = 1; i <= selectedListboxItems.size(); i++) {
        availableCheckboxItems.add("checkbox item " + i);
    }
}

I'm using jsf and primefaces /spring webflow. How I can do that? Any examples?

I believe the above trivial examples are sufficient. For PrimeFaces, just replace <h:xxx> with <p:xxx>. I have however no idea how Spring Webflow plays a role in this as I have never used it.


And I have to make one checkbox which allow to check all generated checkboxes using one click.

That's left as an exercise for you. Hint: make sure that availableCheckboxItems contains the desired values depending on the current value of the <h:selectBooleanCheckbox> which should represent the "toggle selected items".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top