简体   繁体   中英

JSF h:selectManyCheckbox inside ui:repeat via Map entry

I faced with problem, when I put value as String into List<String> inside <ui:repeat> when I press button I see that List<String> contains just records for last element of ui:repeat.

<ui:repeat value="#{util:toList(test.mapQestionsWithAnswers)}" var="entry">
                <h:inputHidden value="#{entry.key.questionId}" />
                <h:outputText value="#{entry.key.question}"
                    rendered="#{not empty entry.value}" />
                <p:selectManyCheckbox value="#{test.questionAnswerList}"
                    layout="pageDirection">
                    <f:selectItems value="#{entry.value}" var="ans"
                        itemValue="#{entry.key.questionId} #{ans.answer.answerId}"
                        itemLabel="${ans.answer.answer}" />
                </p:selectManyCheckbox>
            </ui:repeat>

Idea with util:toList from Baluscanswer How to show hashmap values in jsf?

//UPDATED I rewrite this code without using map

<ui:repeat value="${test.questionList}" var="question">
                <h:outputText value="#{question.question}"
                    rendered="#{not empty question}" />
                <p:selectManyCheckbox value="#{test.questionAnswerList}"
                    layout="pageDirection">
                    <f:selectItems value="#{question.questionAnswers}" var="ans"
                        itemValue="#{ans.questionAnswerIdentifer.questionId} #{ans.questionAnswerIdentifer.answerId}"
                        itemLabel="#{ans.answer.answer}" />
                </p:selectManyCheckbox>
            </ui:repeat>

But it doesn't help me I can get in List<String> just pairs question answer for last question in <ui:repeat/> . Seems as after every iteration questionAnswerList become empty

//Ok I read some article Primefaces ManyCheckbox inside ui:repeat calls setter method only for last loop

And I decide use map Map>

But in this part error ClassCastException String cannot be cast to List

public List<String> getQuestionAnswerList() {
    // return questionAnswerList;
    List<String> selectedQuestionAnswer = new ArrayList<String>();
    if (!selectedItems.isEmpty()) {
        Set<Long> idsSet = selectedItems.keySet();
        for(Long questionId : idsSet){              
            List<String> questionAnswerPair = selectedItems.get(questionId);
            selectedQuestionAnswer.addAll(questionAnswerPair);
        }
    }
    return selectedQuestionAnswer;
}
<p:selectManyCheckbox value="#{test.selectedItems[question.questionId]}"
                    layout="pageDirection">
                    <f:selectItems value="#{question.questionAnswers}" var="ans"
                        itemValue="#{ans.questionAnswerIdentifer.questionId} #{ans.questionAnswerIdentifer.answerId}"
                        itemLabel="#{ans.answer.answer}" />
                </p:selectManyCheckbox> 

How I can get all choosen values in List<String> ?

You can make a map (call it myMap for example) where the key will be entry.key and value will be a list of the same type like questionAnswerList

something like

<p:selectManyCheckbox value="#{test.myMap[entry.key].questionAnswerList}"
    layout="pageDirection">
    <f:selectItems value="#{entry.value}" var="ans"
        itemValue="#{entry.key.questionId} #{ans.answer.answerId}"
        itemLabel="${ans.answer.answer}" />
</p:selectManyCheckbox>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM