0

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>?

Community
  • 1
  • 1
Ray
  • 1,788
  • 7
  • 55
  • 92
  • @Daniel Thanks but it doesn't help( – Ray Nov 25 '12 at 15:31
  • I mean't to say that you shouldn't use `$` in general... Not as an answer... You should create a map that will hold the selections per question – Daniel Nov 25 '12 at 15:44

1 Answers1

1

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>
Daniel
  • 36,833
  • 10
  • 119
  • 200
  • @Ok I try to do as you say but get other problem see above – Ray Nov 25 '12 at 17:22
  • @Ray instead of `itemValue="#{ans.questionAnswerIdentifer.questionId} #{ans.questionAnswerIdentifer.answerId}"` make the `itemValue` to be of a type which are present in the `questionAnswerList` type `` , cause right now `"#{ans.questionAnswerIdentifer.questionId} #{ans.questionAnswerIdentifer.answerId}"` is a string – Daniel Nov 25 '12 at 20:58
  • This doesnt work for me. I get an array of objects and fails to cast to myObjectType. @Daniel do you know what else is missing from this example? i can facility my code through email or something. thank you – allegjdm93 Jan 28 '15 at 20:44
  • @allegjdm93, I suggest you to post a new question – Daniel Jan 29 '15 at 08:04