Meaningless Drivel is fun!*
The moose likes HTML, CSS and JavaScript and the fly likes Getting Content of Table Cells into an Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Getting Content of Table Cells into an Array" Watch "Getting Content of Table Cells into an Array" New topic
Author

Getting Content of Table Cells into an Array

Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
I have a table being populated by jstl. It has one column with input text boxes. My idea is the user types necessary data into each box. Then a javascript will run that will iterate through the table putting the contents into an array which I will send to servlet using JSON. The trouble is I have no idea how to read the table content into an array with javascript.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Something you posted in another topic leads me to believe that you are using jQuery. No?

If so:
[ June 12, 2008: Message edited by: Bear Bibeault ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
First Bear, thanks for helping me out. I would really be in trouble without your help.

If I am understanding this right my javascript will be:



Now if this is right how do I get the values of the elementsList in my servlet. I will pass this to a class where the values can be writen to a remote data file.

Right now this show elementsList is null:


[ June 13, 2008: Message edited by: Steve Dyke ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

I don't understand your selector: '#t1 :hv'

What are you trying to do with this?

And, I can't help myself: doGet(HttpServletRequest arg0, HttpServletResponse arg1)

arg0 and arg1? You should name the parameters with meaningful names. How about request and response?

With regards to passing the array, once you construct an array of the values (not the elements), your syntax for $.getJSON looks right.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
I guess I didn't understand the syntax. I have looked everywhere tring to get some data on the suggested code you provided.

The selector elementsList = ${'#mytable :input'}.get() if I understand now gets all the input tag values from the table with id=mytable into the variable called elementsList.

Now my table has(variable rows) and two columns, one coulmn is a string representing a date the second represents hours assigned to the date. I need an array with two elements(0=date, 1=hours)

Can elementList handle both elements and how do I turn elementsList into an array?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Originally posted by Steve Dyke:
The selector elementsList = ${'#mytable :input'}.get() if I understand now gets all the input tag values from the table with id=mytable into the variable called elementsList.
Almost, but not quite. Firstly, you use ()'s not {}'s. $ is a function.

Secondly, elementList will contain an array of all input elements (not their values) in the table.

To get the values from the elements takes another step. Here is one way to do it:I separated it into two steps for readability. The first statement creates an array of the elements. The second fetches the values from those elements.

In a real page, I'd combine those steps into:

Now my table has(variable rows) and two columns, one coulmn is a string representing a date the second represents hours assigned to the date. I need an array with two elements(0=date, 1=hours)
I'm not sure exactly what structure you are after, but it'd be easy to distinguish between the various types of inputs using the selectors.

For example, if all the date inputs have a name that starts with "date", the selector "#myTable input[name^=date]" will match only those.

There are other possibilities depending upon the nature of the elements. The jQuery selectors are usually powerful enough to be able to pick out the elements that you want to distinguish. If not, you can always fall back on uses CSS classes to distinguish them.
Steve Dyke
Ranch Hand

Joined: Nov 16, 2004
Posts: 1393
I just miss typed the '{' sorry. I think I need to explain about the table a little more. On a page the user is given the option to perform a quickschedule for a particular project. The project already has a start and finish date and scheduled hours. When the user selects this option code is ran to generate an array of months between start and finish and divide the hours between each month. This array is then made into a session attribute which my jsp presents as a table. The months are in the first column as read only. The hours are placed inside input boxes(one on each date row) in the second column. Now the user can change the hours in each row. Then click a buttn or what ever and I need javascript to iterate through the table, collecting the date and hours in each row and send this to a servlet for processing.

Right now my jQuery.getJSON is sending an array to the servlet but when I try System.out.println(myarray[0]) it returns [object].

I feel like I am close to having what I need. I hope this explanation will help you help me get this done.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60783
    
  65

Using Firebug in Firefox you will be able to see the query string that is being sent to the servlet. Check it for correctness. That will tell us whether the problem is on the client or server end.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Getting Content of Table Cells into an Array
 
Similar Threads
Edit table content when click button
Java Script Help Needed
Tabbed menus - jsp
Append a table inside a TD
Multiple Level Sorting