Variable number of Input fields

Hi,

I have quite a problem with the following.

Imagine a scenario like storing an item with a name and a variable number of details

The Input Form would look like this:

Name: (inputText)

Detail1: (inputText)

add another detail (as a command link)

Clicking the command link should lead to this:

Name: (inputText)

Detail1: (inputText)

Detail2: (inputText)

add another detail (as a command link)

The bean would have String "name" and String[] or List "details" as properties.

But I have no idea how create the dynamic number of input fields.

My current approach:

Add a field "num_details" to the bean which is incremented when the command link is pressed.

Then:

<% FacesContext fc = FacesContext.getCurrentInstance(); %>

<% Application app = fc.getApplication(); %>

<% myBean = (myBean)app.getVariableResolver().resolveVariable(fc,"myBean"); %>

<%for(int i = 0; i<bean.getNum_details(); i++){ %>

<tr>

<td>Detail<%=i%></td>

<td><h:inputText id="placeholder" value="placeholder" /></td>

</tr>

<%} %>

Works fine so far, the problem is, I did not manage to dynamically set an id or a value.

I think the value should be #{myBean.details[ i ]} but obviously it does not work like this.

I'd be glad if anybody could help me with this, either with a solution to make my approach work, or with a completely different approach.

best regards,

David

[1761 byte] By [D@vida] at [2007-11-27 11:19:49]
# 1

Use h:dataTable.

Also see http://balusc.xs4all.nl/srv/dev-jep-dat.html

BalusCa at 2007-7-29 14:38:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

Thank you for the answer, but I am not exactly happy with that for two reasons:

1.) The HTML would look better if there wasn't a table in a table

2.) I was asked to make a freely customizable rating facility for something. Meaning the administrator can decide how many categories there are, and what type, i.e. 1-5 stars, a set of checkboxes, etc.

I wanted to work towards the solution of that with the current, smaller problem, and using a data table does not help me with learning how to dynamically add contents to a JSF page.

D@vida at 2007-7-29 14:38:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

1) Tomahawk's t:tree or t:dataList might be interesting.

2) Adding rows to datatable: dataList.add(new RowObject());

Adding new components: UIComponent.getChildren().add(new UIComponent());

BalusCa at 2007-7-29 14:38:57 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...