JSF & RichFaces table with dynamic columns and rows

11 08 2012

To build JSF table (picture below) where subcol3-1, subcol3-… and subcol3-n are known at the runtime, not earlier, you need to use <c:forEach> from JSTL library. Rich:dataTable is insufficient due to the reasons mentioned at the end of that post

Table with dynamic columns that has to be built

Corresponding JSF code for this table is:

		<th colspan="#{variable_indicating_dynamic_columns_count}">column-3</th>
		<c:forEach items="#{Java_ArrayList<String>_with_dynamic_columns_labels}" var="colLabel">
			<!-- colLabels in this templates is ArrayList containing: [subcol.3-1, subcol.3-..., subcol.3-n] -->

	<c:forEach items="#{table_row_java_object}"
			<td>#{item.dataRow_x-1}</td>	<!-- value for row x, column-1 -->
			<td>#{item.dataRow_x-2}</td>	<!-- value for row x, column-2 -->

			<!-- values for row x, dynamic columns: dyn-data col 1,	dyn-data col ?,	dyn-data col n -->
			<rich:columns value="#{item.ArrayList_with_values_to_put_in_dynamic_columns}"
				var="column" index="index">			
				<h:outputText value="#{item.ArrayList_with_values_to_put_in_dynamic_columns[index].value_field}" />

Here you operate on List of Java Objects (table_row_java_object) – each object is one row. Moreover, each row object contains two ArrayLists with same size for each row – first of them contains dynamic columns labels (Strings), second one – corresponding values. Preferable you should also extract dynamic columns labels to JAva Bean to handle table headers more easily. Additionaly two other columns fields should be present – item.dataRow_x-1 and item.dataRow_x-2.

Why don’t use rich:dataTable?
rich:columns tag is just like c:forEach, and c:forEach is built earlier than rich:dataTable in JSF lifecycle. See RichFaces explanation:

The <rich:columns> tag is initialized during components tree building process. This process precedes page rendering at “Render Response” JSF phase. To be rendered properly the component needs all it variables to be initialized while the components tree is being building. Ajavax.servlet.jsp.JspTagException occurs if <rich:columns> uses variables passed from other components, if these variables are initialized during rendering. Thus, when <rich:columns> is asking for such variables they do not already exist. Use <c:forEach> JSP standard tag as workaround.

Please read details carefully here. This is rich:dataTable is not used in my example.

Where from do I know that?

This is the knowledge compiled from all of sources below. It’s ‘enough’ to read them carefully

Did I help you?
I manage this blog and share my knowledge for free sacrificing my time. If you appreciate it and find this information helpful, please consider making a donation in order to keep this page alive and improve quality

Donate Button with Credit Cards

Thank You!




One response

18 10 2012

Tnx for your post. It helped me a lot. :)

Give Your feedback:

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: