4

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>
Onur
  • 958
  • 2
  • 13
  • 18

2 Answers2

8

You need to put columns inside a <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>

(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)

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • answers.answer is supposed to make a call to getAnswer(), but editor still does not even actually list "answer" as a property. Despite IDE's suggestion I put it in there and it still does not get the answer - it shows an empty column. – Onur Sep 21 '11 at 01:39
  • Then the problem is caused by something else. Perhaps there are just no answers? If the property name was wrong, you'd have gotten a `PropertyNotFoundException` anyway. – BalusC Sep 21 '11 at 01:42
  • Even if I replace it with it still does not give me any errors. It is very strange indeed because getAnswer is defined in the Answer entity class. – Onur Sep 21 '11 at 01:45
  • That still won't be displayed when there are no answers. You need to debug `#{question.answers}`. – BalusC Sep 21 '11 at 01:46
  • This is what blows my mind. I doubled checked everything. http://imageshack.us/f/217/jsfh.jpg/ – Onur Sep 21 '11 at 01:58
  • Ignore the EL autocompletion of the IDE. You shouldn't expect that an IDE is smart enough to figure that (this appies not only to Netbeans, but also to Eclipse). What does `` say? Put a breakpoint on `getAnswers()` method of the `Question` class. As far now, I don't see any other cause than that the `List` of the `Question` is simply empty. – BalusC Sep 21 '11 at 02:09
  • I debugged it like this: and it prints the number of answers per question correctly, but I am not able to get the answers under this line still. – Onur Sep 22 '11 at 02:18
  • question have other attributes such as "user" besides "answers". It does not print any of its attributes inside the embedded datatable whatsoever. – Onur Sep 22 '11 at 04:20
1

You can do the same thing mentioned in BalusC's answer using PrimeFaces as well. Just replace <h:dataTable> with <p:dataTable> and the same goes for column tags as well. You can create nested datatable with PrimeFaces. I recommend people to use PrimeFaces as it reduces the amount of time people spend on writing/modifying css definitions.

ChaitanyaBhatt
  • 1,158
  • 14
  • 11