Search code examples
jsfrichfaces

How to bind selected values of <a4j:repeat><rich:select> to the model?


I have the below form:

<h:form>
    <a4j:repeat var="drillDownSet" value="#{drilldownRequest.itemDetailMap.entrySet().toArray()}">
        <rich:select>
            <f:selectItems value="#{drillDownSet.value}" var="val"
                itemValue="#{drillDownSet.key}" itemLabel="#{val}" />
        </rich:select>
    </a4j:repeat>
    <h:commandButton value="Summit" action="#{drilldownRequest.itemSpecSummit}" />
</h:form>

And the below backing bean:

@Named
@RequestScoped
public class DrillDownRequest {

    Map<String, ArrayList> itemDetailMap;

    public void itemSpecSummit() {
        // How can I retrieve all the selected values from the dropdown menus here?
    }

}

How can I retrieve all selected values of the <rich:select> in the action method?


Solution

  • First of all, your <f:selectItems itemValue> is broken. It's the same for all options. This isn't right. Provided that the ArrayList is actually a List<String>, just this should do:

    <f:selectItems value="#{drillDownSet.value}" />
    

    See also:


    Coming back to the concrete question, and continuing with the above fix, you just need to bind the <rich:select value> to another Map<String, String> using #{drillDownSet.key} as map key and the <f:selectItems itemValue> as map value.

    private Map<String, String> selectedItems = new HashMap<>(); // +getter (setter not necessary)
    
    <a4j:repeat var="drillDownSet" value="#{drilldownRequest.itemDetailMap.entrySet().toArray()}">
        <rich:select value="#{drilldownRequest.selectedItems[drillDownSet.key]}">
            <f:selectItems value="#{drillDownSet.value}" />
        </rich:select>
    </a4j:repeat>