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

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

问题描述

我正在使用 JSF 2 开发 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.

>

在代码中,只需替换

In code, just replace

value="#{gridPopUpBean.oneQuestionUserAnswerList}"

value="#{p.oneQuestionUserAnswerList}"

并相应地对模型进行更改.

and make changes in the model accordingly.

或者,您也可以在父 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天全站免登陆