JSF 2.0中的嵌套数据表 [英] Nested datatable in JSF 2.0
问题描述
我正在尝试获取问题和相关的答案.我能够很好地解决问题,但是嵌套的数据表甚至无法识别每个答案具有的属性,IDE也指出了这一点.也许这不是解决此问题的正确方法.如何迭代与每个问题相关的答案?
I am trying to fetch the questions and the associated answers. I am able to get the questions fine but the nested datatable does not even recognize the properties each answer has, and this is also pointed out by the IDE as well. Maybe this is not the right way to go about this. How do I iterate over the answers associated with each question ?
<h:dataTable value="#{questionBacking.recentlyAskedQuestions}" var="questions"
rendered="#{questionBacking.recentlyAskedQuestions.size() > 0}"
border="1">
<h:column>
<h:outputText value="#{questions.questionTitle}" />
<br/>
<h:outputText value="#{questions.questionBody}" />
</h:column>
<h:dataTable value="#{questions.answers}" var="answers">
<tr>
<h:outputText value="#{answers.answer}" />
</tr>
</h:dataTable>
</h:dataTable>
推荐答案
您需要将列放在<h:column>
内.
<h:dataTable value="#{questionBacking.recentlyAskedQuestions}" var="question" rendered="#{not empty questionBacking.recentlyAskedQuestions}">
<h:column>
<h:outputText value="#{question.questionTitle}" />
<br/>
<h:outputText value="#{question.questionBody}" />
</h:column>
<h:column>
<h:dataTable value="#{question.answers}" var="answer">
<h:column>
<h:outputText value="#{answer.answer}" />
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
(请注意,我将rendered
和var
属性更改为更具自记录性,您可能想将questionTitle
,questionBody
和answer
重命名为title
,body
和body
分别,这样您就不会重复复制含义)
(note that I changed the rendered
and the var
attributes to be a bit more self-documenting, you might want to rename questionTitle
, questionBody
and answer
to title
, body
and body
respectively as well so that you don't keep duplicating the meaning)
这篇关于JSF 2.0中的嵌套数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!