jahroy
jahroy

Reputation: 22692

JSF 1.2 - generate dynamic ID attributes for tables using <h:dataTable>

What is the best way to create dynamic ID attributes for tables (in a loop) using <h:dataTable>?

We use the following:

We have a page that has 8 tables.

Each table MUST have a unique ID attribute.

The tables are drawn by iterating over a Map that contains elements that contain table data.

The keys of the Map should be used when defining the tables' ID attributes.

Example:

If my map contains these keys: Dog, Cat, Mouse, Bird

Ideally, I want the ID attributes of my tables to be: dog-table, cat-table, mouse-table, bird-table

Something random would work as well: t1-table, t2-table, t3-table

The ID attribute of <h:dataTable> does not permit EL expressions (so I can't access my map keys directly).

What is the best way to create my ID attributes?

Two approaches come to mind, but they both seem hacky:

  1. use a scriptlet or JSP value expression

  2. use jQuery to assign unique IDs to each table (after its rendered and before its processed)

The ultimate goal is to display these tables using jqGrid (so we're using jQuery anyways).

Any help would be awesome...


NOTE:

We are using a helper class to simulate iterating over a Map (since this can't be done in JSF 1.2).

In stead of actually iterating over a Map, we generate a List of objects that have a name and a List<Foo>.

The name represents the key and the List represents the rows of the table(s).


Edit:

It eventually occurred to me that <h:dataTable> generates its own unique IDs in this scenario.

The generated names are good enough to solve my issue...

Even though my page is working, I'm still curious about how to approach this situation in the future.

Upvotes: 4

Views: 1186

Answers (1)

Bhushankumar Lilapara
Bhushankumar Lilapara

Reputation: 780

I think this will help you...

All expressions using the ${} syntax are evaluated immediately. These expressions can be used only within template text or as the value of a tag attribute that can accept runtime expressions.

   id="${data.map}-table

Upvotes: 1

Related Questions