3 Replies Latest reply on Aug 1, 2007 5:27 AM by Damian Harvey

    AJAX question: how to dynamically add fields to a form?

    None None Master

      Simple question here: A user has to enter a series of serial numbers on a form. We don't know in advance how many serial numbers the user will enter. It could be five or fifty.

      What would be cool is if a new text field would get added when the last empty one is filled in.

      I'm sure there's some AJAX way of doing this but I'm new to the whole thing. Does anyone have ideas on this? By the way I'm using Richfaces, not Icefaces, as my AJAX toolkit. At this point anyway.

      Thanks

        • 1. Re: AJAX question: how to dynamically add fields to a form?
          Samuel Doyle Apprentice

          Um, off hand I think you would have to use some DHTML to do this and not necessarily use AJAX. I never tried doing that before with JSF components so I don't know how that would work since the the DHTML is primarily all client side and JSF requires the backing beans and in Seam's case EJB.

          • 2. Re: AJAX question: how to dynamically add fields to a form?
            None None Master

            Actually I mean DHTML, not AJAX.

            Anyway, in this case, it might be better for me to have a scrollable panel that opens up, and provide a couple hundred number fields, and be done with it. Easy to implement, and if we need something more advanced later, I'll change it.

            • 3. Re: AJAX question: how to dynamically add fields to a form?
              Damian Harvey Master

              You could do this easily with Ajax4JSF if you wanted to (if you're using Richfaces then you're already using this). Several advantages over DHTML eg. server-side validation, not trusting the client...etc.

              Say you had a Bean called MySerialNumbersBean and it had a List of Serial numbers called mySerialNumbers (with the getters and setters). In your page you would have something (roughly) like this:

              <s:div id="serialNumbersDiv">
               <ui:repeat value="#{mySerialNumbersBean.mySerialNumbers}" var="serialNumber">
               <h:inputText id="serialNumber" value="#{serialNumber}">
               <a4j:support event="onblur" action="#{mySerialNumbersBean.mySerialNumbers.addSerialNumber()}" reRender="serialNumbersDiv"/>
               </h:inputText>
               </ui:repeat>
              </s:div>
              

              Where all the addSerialNumber() method did was a add(""). You could do this directly from the EL I suppose.

              Cheers,

              Damian.