ui:repeat内部的selectManyCheckbox集合知道其属于中继器的哪个元素 [英] Collection of selectManyCheckbox inside a ui:repeat knows which element of the repeater it belongs to

查看:125
本文介绍了ui:repeat内部的selectManyCheckbox集合知道其属于中继器的哪个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF 2开发Web应用程序. 我的Web应用程序除其他事项外,包含一系列问题,一次回答一个(一次可以看到一个问题),并且有多个答案(我使用h:selectManyCheckbox).

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 multiple answers (I use h:selectManyCheckbox).

我将问题和可能的答案存储在ArrayList("gridQuestionListClone")中,而用户的答案(在最后检查它们之前)存储在HashMap("allQuestionUserAnswerMap")中. (如果答案需要,我可以更改存储方式).

I store the questions and possible answers in a ArrayList ("gridQuestionListClone") and the users answers (before checking them at the end) in a HashMap ("allQuestionUserAnswerMap"). (I could change the storage modality if the answer demands it).

当用户回答完所有问题后,我想让用户有可能在页面中重新检查所有问题和答案(我希望答案已预先加载).因此,用户将所有问题及其对应的复选框(每个问题3个)都选中了一个复选框.

When the user finishes answering all the questions I want to give the user the possibility to recheck all the questions and answers (I want the answers preloaded) in a page. So all the questions and their corresponding checkboxes (3 at each question) with the users checked checkboxes in one page.

因此,我使用ui:repeat遍历所有问题和可能的答案,并且我需要一种机制来检查正确的复选框(用户已选中该复选框),并为用户提供了更改他/她的答案以及当我按时按下的可能性(例如)一个提交按钮,以了解与哪些问题对应的复选框. 一个使事情更清楚的草图:))(也许).因此,当用户回答完所有单个问题后,我想向他展示:

So I use ui:repeat to go through all the questions and possible answers and I need a mechanism to check the right checkboxes (which the user checked) and give the user the possibility to change his/hers answers and when I press a submit button (for example) to know which checkboxes to which questions correspond. A sketch to make things clearer :)) (maybe). So when the user finished answering all the single questions I want to show him:

Question 1:
a) answer1 - CHECKED
b) answer2 - UNCHECKED
c) answer3 - CHECKED
------------------------
Question 2:
a) answer1 - UNCHECKED
b) answer2 - UNCHECKED
c) answer3 - CHECKED
------------------------
Question 3:
a) answer1 - CHECKED
b) answer2 - CHECKED
c) answer3 - UNCHECKED
------------------------
.
.
.
Question n:
a) answer1 - CHECKED
b) answer2 - UNCHECKED
c) answer3 - CHECKED
------------------------ 
SUBMIT button

用户检查他/她的答案,并在需要时对其进行修改,然后按提交按钮. 然后,我想要一个某种类型的集合(或多个我并不在乎的东西,之后我可以合并它们),其中包含问题的编号和可能的答案,例如"allQuestionUserAnswerMap"中的乞讨或类似内容.

The user checks his/hers answers and modifies them if needed and presses the submit button. Then I would like to have a collection of some kind (or multiple I don't really care, afterwards I could merge them) which contains the number of the question and the possible answers like in "allQuestionUserAnswerMap" at the begging or something like that.

我的琐碎代码(不正确):

My trivial code (not correct):

<ui:repeat var="p" value="#{gridPopUpBean.gridQuestionListClone}">
                    <hr />
                    <h:panelGrid columns="2">
      ...
                        <h:panelGroup style="text-align: left">
                            <h:selectManyCheckbox
                                value="#{gridPopUpBean.oneQuestionUserAnswerList}"
                                layout="pageDirection">
                                <f:selectItem itemValue="a"
                                    itemLabel="#{p.a}" />
                                <f:selectItem itemValue="b"
                                    itemLabel="#{p.b}" />
                                <f:selectItem itemValue="c"
                                    itemLabel="#{p.c}" />
                            </h:selectManyCheckbox>
        ...
                        </h:panelGroup>
                    </h:panelGrid>
                </ui:repeat>

有什么想法吗?

推荐答案

最简单的方法是将复选框组的值绑定到当前迭代的对象,而不是将所有复选框组绑定到一个相同的父bean属性.

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.

在代码中,只需替换

value="#{gridPopUpBean.oneQuestionUserAnswerList}"

作者

value="#{p.oneQuestionUserAnswerList}"

并相应地更改模型.

或者,您也可以在父bean中按问题ID提供所有答案的Map.这是一个启动示例,该示例使用比您更多的自记录变量名称,因此更易于理解:

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>

带有例如

private Map<Long, Answer[]> answers = new HashMap<Long, Answer[]>();

这篇关于ui:repeat内部的selectManyCheckbox集合知道其属于中继器的哪个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆