Hi,

Well, lets see:
The first step is to display  a fixed number of text  fields that the
user can set. Say I want to have several fields "from" and "to". So, I
create a bean with
public class ZuordData {
  private String to;
  private String from;
  /* Constructors, getters setters etc */
}

Then create a List of the these beans in your form:

public class FgZuordnungForm extends ActionForm {
  private List zuordAll = new ArrayList();
   /* Constructors, getters setters etc */
}

In the preparing Action you add some values
..
List zuord = ((FgZuordnungForm) form).getZuordAll();
      zuord.add(new ZuordDate("z","a");
      zuord.add(new ZuordDate("there","here");
...

Forward to a jsp to display these Values
...
<html:form action="/zuordSubmit" >
<table id="fgsInner">
<tr>
   <th> From somwhere</th>
   <th> To somewhere</th>
</tr>
<logic:iterate name="FgZuordnungForm" property="zuordAll"
id="zuordAll">
  <tr>
     <td><html:text name="zuordAll" indexed="true" property="from" /></td>
     <td><html:text name="zuordAll" indexed="true" property="to"   "/></td>
  </tr>
  </logic:iterate>
</table>
...

This will work (if I didn't simplify my code to much) and create HTML like:

<input type="text" name="zuordAll[1].from" value="z" >


The second step is to add new fields to enter: have your Javascript
create more input-fields of  the same type, making sure that the index
is increased, Something like

..
function addEntry(myRow, row,what) {
    var myCell = document.createElement('td');
         myInput = document.createElement('input');
         myInput.setAttribute('type','text');
         myInput.setAttribute('name','zuordAll['+row+'].'+ what);
        myInput.setAttribute('value',"");
        myCell.appendChild(myInput);
        myRow.appendChild(myCell);
}
..
var myTable = document.getElementById('fgsInner');
var totalRows = myTable.rows.length;
var myRow = document.createElement('tr');
addEntry(myRow,number,i,'from' );
addEntry(myRow,totalRows-1,'to' );
table.tBodies[0].appendChild(myRow);
...

Execute this and another row of inputs will appear. But if you try to save these
values, you will get an IndexOutOfBoundsException, because you try to
add values to  zuordAll at an index that is to big. This is where the
LazyList comes into play. If you declare

    private List zuordAll=ListUtils.lazyList(new ArrayList(),
                                             new Factory() {
                                                    public Object
create() { return new ZuordData();
                                                    }
                                             });

LazyList will automatically increase the size of the list as needed.

Hope that helps,

Ulrich


On 11/8/05, Agnisys <[EMAIL PROTECTED]> wrote:
>   Will it be posible for you to send some more core around the code snippets 
> you have shown in
> your email?

>
> --- Ulrich Elsner <[EMAIL PROTECTED]> wrote:
>
> > Hi,
> >
> > I found that the easiest way to do this is to use LazyLists. (cf.
> > http://wiki.apache.org/struts/StrutsCatalogLazyList). You can then work just
> > as you
> > with fixed lists. An example:
> >
> > in my jsp I have something like ...
> >
> > <logic:iterate name="FgZuordnungForm" property="zuordAll" id="zuordAll">
> > <tr>
> > <td><html:text name="zuordAll" indexed="true" property="from" /></td>
> > <td><html:text name="zuordAll" indexed="true" property="to" "/></td>
> > </tr>
> > </logic:iterate>
> > <span Id="fgzList"></span>
> >
> > (Note the indexed="true"). I have a javascript function that creates more
> > table rows
> > where the <span...> stands. These rows look just like the ones created by
> > the
> > html:text tag, except that the array-index is increased, i.e.
> > ... myInput.setAttribute('name','zuordAll['+row_cnt+'].'+ 'from');
> >
> > In the corresponding form, the List is simply declared as
> > private List zuordAll=ListUtils.lazyList(new ArrayList(),
> > new Factory() {
> > public Object create() { return new ZuordData();
> > }
> > });
> > plus the normal setter and getter. (ZuordData is just a bean with properties
> > from and to).
> >
> > Thats all. You can pass in a List of size n, add a few rows using Javascript
> > and,
> > after submitting the form get out a List of size n+k.
> >
> > Rather simple once one has figured it out.
> >
> > Hope that helps,
> >
> > Ulrich
> >
> >
> >
> >
> > On 11/6/05, Agnisys <[EMAIL PROTECTED]> wrote:
> > >
> > > Hi,
> > > In my application the user can add any number of input text fields to add
> > > properties to a form
> > > (implemented using Javascript). Is there a way to create an ActionForm
> > > that can get an array of
> > > property values from the JSP page, without knowing how many they are? How
> > > would such properties be
> > > specified in the config file?
> > > I don't see any way of getting all these properties in a JavaBean, since a
> > > bean requires that
> > > the property names and number be known statically.
> > > I looked at the LazyActionForm but I don't think that would help in this
> > > situation.
> > >
> > > Thanks,
> > > Anupam.
> > >
> > >
> > >
> > >
> > >
> > > __________________________________
> > > Start your day with Yahoo! - Make it your home page!
> > > http://www.yahoo.com/r/hs
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: [EMAIL PROTECTED]
> > > For additional commands, e-mail: [EMAIL PROTECTED]
> > >
> > >
> >
>
>
>
>
> __________________________________
> Start your day with Yahoo! - Make it your home page!
> http://www.yahoo.com/r/hs
>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to