Forums Register Login

How to make an arbitrary number of input fields in JSF?

+Pie Number of slices to send: Send
Hi All-

I'm making good progress with my first JSF application, and one requirement I have is to have a page with an arbitrary number of names, like a sign up sheet. There can be 0 names to several hundred, depending on how many names the user cares to enter. This was easy to do in Stripes/Struts, but I haven't figured out the JSF equivalent.

I know that JSF web page variables are tied to a back bean. So for example, if I have the below code in the backing bean:



And the front end:




Now I have 10 fields to input names - but what strategy can I employ to add new fields dynamically as the user enters them?

When I did it in Stripes, what I'd do is clone the inputText using jQuery with a pattern, like this:




However, this doesn't seem to work in JSF, if you create new DOM input elements following the pattern of existing JSF generated input elements, JSF isn't going to expand the array holding the list of names upon submit.

Is there a way to accomplish having a growing list of input fields in JSF? I'd like to avoid needing an ajax call to the server just to allocate a new String to the name array and needing to refresh the page.

Any hints gratefully appreciated!

-Derrick

+Pie Number of slices to send: Send
I don't how to do this, but, to start with you should have an id for the datatable, like:
Once, you have an id, that can be referenced from a Javascript, like:
where 'myForm' is my form's name. If you don't provide an id, JSF will generate an id, but that will be dynamically vary, so you can't reliably reference it in a Javascript.

Many JSF implementations use jQuery behind the scenes, so, you can check if jQuery is already available for you. If not, you can still include it. And then, you can use the above reference to try and see if you can add components.


I earlier did an implementation with JSF 1.2 where a part of the page would be dynamically built. I did it with:

and then in the backing bean, like:

But, in my case, I knew what had to be built before the page was rendered. I am not sure if you can do this without a AJAX request or page refresh.
+Pie Number of slices to send: Send
 

Derrick Williams wrote:...JSF isn't going to expand the array holding the list of names upon submit.



Hi Derrick,

Regardless of JSF, an ArrayList will grow/shrink dynamically as you add/remove items, so my understanding of your question is a bit confused there. Anyways, if you use the f:ajax tag you can keep adding names to the list without refreshing the page (in a conventional sense) using one input field. I don't know why you would want more than one input field if you can just keep adding and submitting names using just one, unless you have an ulterior motive of course

Try this:

Backing bean:



Front end xhtml page:



I don't know if that is something like what you are after.

Alan
+Pie Number of slices to send: Send
The main thing to keep in mind is that the "var" attribute of a dataTable or related multi-element display tag is supposed to refer to a class object, not a unitary object.

So "#{name}" as an input control value isn't going to work very well. Remember, String objects are immutable!

You need a mutable object here, even if it's nothing more than a class that defines a single String property.
Beware the other head of science - it bites! Nibble on this message:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 4454 times.
Similar Threads
Displaying 2D array using data tables
create multiple checkboxes with Data base values in jsf page
How to make an arbitrary number of input fields in JSF?
Page section does not update after call of a4j:support in IE 9
Java array and javascript array
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 17:08:47.