wood burning stoves 2.0*
The moose likes Struts and the fly likes Adding Dynamic Rows to Table using addrow button Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Adding Dynamic Rows to Table using addrow button" Watch "Adding Dynamic Rows to Table using addrow button" New topic
Author

Adding Dynamic Rows to Table using addrow button

Aayush Surana
Greenhorn

Joined: Oct 18, 2011
Posts: 8
Hi I have a table to which I want to dynamically add rows when the user presses on a button . I am using an arraylist containing objects of a class in my action form which are populated using lazylist method . My question is that How can I add the dynamically added row to the arraylist ? What should be the appropriate javascript code ?

ArrayList is a list of objects of a class containing

public static String vendorID;
public static String address;
public static String userAsins;

and the necessary getters and setters

In my javascript addrow() function I am creating a new row with 3 text boxes for the above fields . However I dont know how can I link these textboxes to their respective attributes of the object and subsequently add them to the arraylist .
Any help would be deeply appreciated .
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


DynaActionForm? Name/ID the textfield(s) appropriately.

Am not sure this is the best possible way. I was just presenting an approach.

Cheers,
Raj.

Aayush Surana
Greenhorn

Joined: Oct 18, 2011
Posts: 8
Hi Raj ,

Thanks for the reply ! could you please elaborate ?
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


Using Struts there should be properties in your Form Bean corresponding to the HTML FORM elements for retrieving the user entered data.
In your scenario, You would not know the number of rows and hence the number of TEXTFIELDs in the HTML FORM at the time the request is submitted, correct?

Struts provides DynaActionForm for such situations. Try a google on DynaActionForm for more information and usage.

Cheers,
Raj.

Aayush Surana
Greenhorn

Joined: Oct 18, 2011
Posts: 8


Raj Kamal wrote:
Using Struts there should be properties in your Form Bean corresponding to the HTML FORM elements for retrieving the user entered data.



Yes , I have an arraylist but it is not necessarily corresponding to my HTML form elements , in the sense , here is my jsp function :-

function addRow() {

var table = document.getElementById('details');
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);

var cell1 = row.insertCell(0);
var element1 = document.createElement("input");
element1.type = "textbox";
element1.style.textalign="center";
cell1.appendChild(element1);

var cell2 = row.insertCell(1);
.......
var cell3 = row.insertCell(2);
.......

cell3.appendChild(element3);

}


Now my html has the following code for iteration over the lazy list :-

<logic:iterate id="vendorInfos" name="createRemovalsReportForm" property="vendorInfos">
<tr CLASS="MYTABLE">
<td CLASS="MTABLE" >
<html:textarea name="vendorInfos" property="vendorID" rows="1" cols="30" ></html:textarea>
</td>
<td CLASS="MTABLE" >
<html:textarea name="vendorInfos" property="address" rows="1" cols="30" ></html:textarea>
</td>
<td CLASS="MTABLE" >
<html:textarea name="vendorInfos" property="userAsins" rows="1" cols="30" ></html:textarea>
</td>
</tr>
</logic:iterate>

Where VendorInfos is my ArrayList .

My question is how do I add the newly added rows to this arraylist ?

Thanks a lot !
Michael Jack
Greenhorn

Joined: Oct 20, 2011
Posts: 1
Hi, In my point of view, how about 'JQuery'? It maybe help you!
Aayush Surana
Greenhorn

Joined: Oct 18, 2011
Posts: 8
Michael Jack wrote:Hi, In my point of view, how about 'JQuery'? It maybe help you!


Hey Michael , I have never used JQuery before , could you please help me out , how this can be done ?
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


jQuery or any of the AJAX based frameworks would work well if you intend to have AJAX capabilities in your application.

Is the application working with the code you provided? I am not too sure about how STRUTS would be able to populate the ArrayList with the correct values on FORM submission. Using logic:iterate you would be able to get the values on the page but have you checked how it works the other way round?

In the DynaActionForm approach I mentioned, the form elements can be accessed as in a map.



The above code would return the value in TEXTAREA named "vendorID_1". Thats the reason I had mentioned naming the FORM elements appropriately.

Cheers,
Raj.
Aayush Surana
Greenhorn

Joined: Oct 18, 2011
Posts: 8
Raj Kamal wrote: I am not too sure about how STRUTS would be able to populate the ArrayList with the correct values on FORM submission. Using logic:iterate you would be able to get the values on the page but have you checked how it works the other way round?

In the DynaActionForm approach I mentioned, the form elements can be accessed as in a map.



The above code would return the value in TEXTAREA named "vendorID_1". Thats the reason I had mentioned naming the FORM elements appropriately.

Cheers,
Raj.


Hi , as you mentioned , I don't know how I would be able to get these values onto the arraylist , is it possible to use the getProperty or getParameter method in normal ActionForm ?
I thought i could assign the elements in my javascript like this :-

element2.name="sessiondata1" idNo;

and then access them in my action class using getParameter method and then subsequently attach them to my arraylist using the setter ?
Would this work ?

Thanks !
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


What happens when you use that logic?

Cheers,
Raj.
Aayush Surana
Greenhorn

Joined: Oct 18, 2011
Posts: 8
Raj Kamal wrote:
What happens when you use that logic?

Cheers,
Raj.


Its returning NULL in my action class , i guess it doesnt work in action form , i'll switch to dynamic action form ...
Thanks !
Rajkamal Pillai
Ranch Hand

Joined: Mar 02, 2005
Posts: 443
    
    1


I wonder if you are able to get the values to and from the web page without adding any new rows.

Aayush Surana
Greenhorn

Joined: Oct 18, 2011
Posts: 8
Raj Kamal wrote:
I wonder if you are able to get the values to and from the web page without adding any new rows.




I had first declared only an object of a class containing 3 fields , i was able to get the values from the webpage without any trouble ...
but with the arraylist of these objects ,i am having trouble ... i would be able to get the values , IF the fields are there beforehand and the user simply enters the value into these and presses submit ...
however with the addrow button , dunno how i would take in the values of the newly added rows and add them to the arraylist ..

could you please provide any link to an example which uses dynamic action forms and also uses lazy list / or in some way incorporates dynamic addition of data ..

Grateful thanks for all your help !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Adding Dynamic Rows to Table using addrow button
 
Similar Threads
Using polymorphism with arraylist
String and Numeric Parameters
urgent plz
Casting Problem *Help Please
row selection