webtier@glassfish.java.net

Re: [webtier] Conditionally rendering h:colun

From: Alex Sherwin <alex.sherwin_at_acadiasoft.com>
Date: Thu, 12 Nov 2009 16:52:57 -0500

Ah, but you missed one important aspect. The same logic is being used
to render the <th> elements (the header row... <tr><th>1
header</th><th>2 header</th></tr>).

The problem you're having is that your logic is setting rendered="true"
for the 5 rows it iterates over, however, when JSF is creating the
header elements, your row is undefined at that point, causing one of a
false/null/undefined value to be present in your rendered attribute at
that time, causing the header <th> element to not be rendered, so your
resultant HTML looks like this:

<table>
<tr><th>header1</th></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
<tr><td>1</td><td>2</td></tr>
</table>

I had this same issue before... So basically, your logic for the
rendered attribute cannot come from the variable you're iterating over
in the h:dataTable, since it will not be defined properly for the header
row.


Uwe Seimet wrote:
> Hi Alex,
>
>
>> You have to think of it in terms of how an HTML table is drawn, you
>> cannot have a variable number of columns per row. If you have a dataset
>> of 5 rows, and you're deciding the rendered attribute of h:column using
>> data from the row item where it is not rendered in one row only, you'll
>> be generating mal-formed HTML:
>>
>> <table>
>> <tr><td>1</td><td>2</td</tr>
>> <tr><td>1</td><td>2</td</tr>
>> <tr><td>1</td><td>2</td</tr>
>> <tr><td>1</td><td>2</td</tr>
>> <tr><td>1</td>
>> </table>
>>
>>
>> Your condition for the rendered attribute of h:column can of course come
>> from anywhere, but it must be true for your entire dataset (and as such,
>> probably should not be derived from your iterated table data)
>>
>
> I see your point, and there is indeed the potential of generating
> illegal HTML. In my case, however, the number of rendered columns was
> constant while rendering the table. Nevertheless the rendered result
> was incorrect. Provided that the count of rendered columns does not
> change while rendering a particular table (i.e. #{item.rendered} always
> returning the same value) I would have expected my code to work, but
> despite the fact that #{item.rendered} always returned 'true' the table
> was rendered incorrectly. I wonder why this happens and whether there is
> a technical explanation.
>
> Best regards
>
> Uwe
>
>