uday
uday

Reputation: 315

Conditionally creating table rows JSF2

I want to create a table which display a list of values in several rows, each row should contains only 3 columns.

I can create this by using JSTL but not using JSF. I am suspecting that it is xhtml related issue, not a problem in JSP, xhtml is very strict about the elements formation. Is there a way to achieve this?

Using JSTL:

<c:forEach var="book" value="${bookBean.bookList}" varStatus="bookStatus">
  <c:if test="${bookStatus.index eq 0 or bookStatus.index mod 3 eq 0}">
    <tr>
  </c:if>
       <td>
         <table>
           <tr>
        <td>#{book.bookId}</td>
        <td>#{book.bookName}</td>
        <td>#{book.price}</td>
        <td>#{book.author}</td>
           </tr>
          </table>
       </td>
 <c:if test="${bookStatus.count mod 3 eq 0}">
    </tr>
 </c:if> 
</c:forEach>

Using JSF:

  <ui:repeat var="book" value="#{bookBean.bookList}" varStatus="bookStatus">
    <ui:fragment rendered="#{bookStatus.first or bookStatus.index mod 3 eq 0}">
        <tr>   <!-- Here i am getting an error saying closing tr missed.--->
        </ui:fragment>
           <td>
             <table>
               <tr>
            <td>#{book.bookId}</td>
            <td>#{book.bookName}</td>
            <td>#{book.price}</td>
            <td>#{book.author}</td>
               </tr>
              </table>
           </td>
      <ui:fragment rendered="#{(bookStatus.index+1) mod 3 eq 0}">
        </tr>
      </ui:fragment>
  </ui:repeat>

Upvotes: 0

Views: 2387

Answers (2)

Jamil
Jamil

Reputation: 1

As @FMQ has mentioned, try to use a JSF component library. Primefaces could be a good option, checkout the Demo page.

Upvotes: 0

FMQ
FMQ

Reputation: 418

This is actually not easy to do without using some component library (richfaces I know for a fact that has a component that does this.. rich:dataGrid..)

Here's a solution I found a while back to solve this issue... (I'm not a big fan of component libraries myself... ).. it's not the most elegant solution.. but it does the trick..

<table>
  <tr>
    <ui:repeat var="book" value="#{bookBean.bookList}" varStatus="bookStatus"> 
      <td>
         <table>
            <tr>
               <td>#{book.bookId}</td>
               <td>#{book.bookName}</td>
               <td>#{book.price}</td>
               <td>#{book.author}</td>
            </tr>
         </table>
       </td>
       <h:outputText value="&lt;/tr&gt;&lt;tr&gt;" escape="false" rendered="#{(bookStatus.index + 1 ) mod 3 eq 0}"/>
    </ui:repeat>               
  </tr>                        
</table>  

Upvotes: 1

Related Questions