Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Showing database rows as columns

 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

I know I can show all database rows on my JSF page through dataTable tag.
However, such tag shows all informations in a ROW oriented way.

How can I change this behavior and show every row in database on JSF page as columns.

Example :



B. Rgds.
Edisandro.
[ June 05, 2007: Message edited by: Edisandro Bessa ]
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18092
48
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For something simple like that, create a Vector<String[]> or List<String[]>. Copy the items over into a multi-column data list:



For more complex collections, where you'd want multiple database columns to format in a single table column, things are a little more complex - perhaps best done by a custom JSF display tag.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Tim for your prompt reply.


final int NUM_COLUMNS = 3;
int colnum = NUM_COLUMNS+1;
while ( rowset.next() ) {
String[] cols;
if ( colnum >= NUM_COLUMNS ) {
cols = new String[NUM_COLUMNS];
list.append( cols );
colnum = 0;
} else {
cols = list.get(list.size()-1);
}
cols[colnum++] = rowset.getString("email");
}
ListModel model = new ListModel(list);


That's the point I've fallen after thinking for a few minutes. I realized that I have to format my column oriented list inside the bean method such as getUsers() and then simply show it on my JSF page.

However, I got another issue :

In your code you defined the number of columns as a constant.

Supposing the number of columns comes from an init parameter and that my getUsers method also has already formatted the user's list accordingly as you posted above.

How can I dynamically create the <h:column> tags inside <h:dataTable> tag so that when the total columns defined as init parameter is reached the dynamic column creation stops ?

I mean :

If the number of columns defined in init parameter is three, the resulting JSF page could be something like

<h:dataTable>
<h:column>...</h:column>
<h:column>...</h:column>
<h:column>...</h:column>
</h:dataTable>

If the number of columns is four, the resulting JSF page could be something like
<h:dataTable>
<h:column>...</h:column>
<h:column>...</h:column>
<h:column>...</h:column>
</h:dataTable>

and so on ...

How to dynamically create these columns ?

Thanks a lot.
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

Any ideas on how to dynamically create columns when using h:dataTable tag ?
 
Edisandro Bessa
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,

For those interested I've just found a great Tomahawk component which I can use to dynamically create column in a dataTable.

http://myfaces.apache.org/tomahawk/columns.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic