Displaying data using JSF dataTable
Question
I want to display data using the h:dataTable
tags in JSF
The data I am displaying has to be in a format like this:
Name: XXXX
ID: 8989
Age: 32
I have seen several examples on the web that tell you how to display the data like this:
Name ID Age XXX 8989 32
Since the dataTable
tag only has a sub tag h:column
and NO h:row
I am having
issues with this. Is there a better way to do this? I have implemented the first
format I mentioned using h:datable
and h:column
, so first I just write out all the column headers in column 1 and then I write out all the values in column2. The issue I am facing is that when one of the values is blank the next value gets printed in its place and the data appears wrong, this is really a formatting issue. When data is blank it should leave space for it and then print the next value in the next row.
Hope I am making sense, any thoughts will be much appreciated.
I tried adding the below :
<h6><h:outputText value="#{empty imeiInfo.userEmail ? ' ' : imeiInfo.userEmail}"></h:outputText></h6><br/>
But there is a syntax error in this which I cannot figure out, the message I get is:
Syntax error in EL
Any ideas? Thanks.
Solution
Is it just for a non-repeating data? If so, use <h:panelGrid>
For example:
<h:panelGrid columns="2">
<h:outputText value="Name:"/>
<h:outputText value="#{person.name}"/>
<h:outputText value="ID:"/>
<h:outputText value="#{person.id}"/>
<h:outputText value="Age:"/>
<h:outputText value="#{person.age}"/>
</h:panelGrid>
If it is for repeating data (ie. multiple 'persons') then you have a number of options depending on what other libraries you are using. For example:
1: Build your own table using Facelets <ui:repeat>
(you could do the same with <c:for>
). For example:
<table>
<ui:repeat value="#{myBean.persons}" var="person">
<tr>
<td>
<h:outputText value="Name:"/>
</td>
<td>
<h:outputText value="#{person.name}"/>
</td>
</tr>
<tr>
<td>
<h:outputText value="ID:"/>
</td>
<td>
<h:outputText value="#{person.id}"/>
</td>
</tr>
<tr>
<td>
<h:outputText value="Age:"/>
</td>
<td>
<h:outputText value="#{person.age}"/>
</td>
</tr>
<tr>
<td colspan="2">
 
</td>
</tr>
</ui:repeat>
</table>
EDIT: I haven't tried this but you should be able to surround the in the first example (<h:panelGrid>
) with a <c:for>
to render repeating elements. The <c:for>
gets processed before the <h:panelGrid>
. Should work.
OTHER TIPS
I'm not sure to understand how you want to render your table. I think it would help us if you could provide the JSF code of your table...
However, concerning this point:
When data is blank it should leave space for it and then print the next value in the next row
maybe you can try something like that:
<h:outputText value="#{empty aRecord.name ? ' ' : aRecord.name}"/>
This code will render an unbreakable space if the name of the record is empty, otherwise it will render the name
of the record.