Collection of selectManyCheckbox inside a ui:repeat knows which element of the repeater it belongs to

后端 未结 1 835
南方客
南方客 2020-12-04 04:23

I am developing a web app using JSF 2. My web app among other things contains a series of questions put one at a time (so one question is visible at a time) and have multip

相关标签:
1条回答
  • 2020-12-04 04:53

    Simplest way would be to just bind the value of the checkbox group to the currently iterated object instead of all checkbox groups to one and same parent bean property.

    In code, just replace

    value="#{gridPopUpBean.oneQuestionUserAnswerList}"
    

    by

    value="#{p.oneQuestionUserAnswerList}"
    

    and make changes in the model accordingly.

    Alternatively, you can also provide a Map of all answers by question ID in the parent bean. Here's a kickoff example which uses more self-documenting variable names than you have, so that it's better understandable:

    <ui:repeat value="#{bean.questions}" var="question">
        ...
        <h:selectManyCheckbox value="#{bean.answers[question.id]}">
            <f:selectItems value="#{question.answers}" />
        </h:selectManyCheckbox>
        ...
    </ui:repeat>
    

    with e.g.

    private Map<Long, Answer[]> answers = new HashMap<Long, Answer[]>();
    
    0 讨论(0)
提交回复
热议问题