4
votes

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

2 Answers

8
votes

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)

1
votes

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.