Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested datatable in JSF 2.0

Tags:

jsf

jsf-2

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>
like image 588
Onur Avatar asked Jan 18 '23 16:01

Onur


2 Answers

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)

like image 170
BalusC Avatar answered Jan 24 '23 14:01

BalusC


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.

like image 39
ChaitanyaBhatt Avatar answered Jan 24 '23 12:01

ChaitanyaBhatt