473,509 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Table data to Array?

2 New Member
I am attempting to employ simple tables that my client can use to supply info to her website. I would like to read the table from an HTML page and transfer the tabular data to an array for manipulation.

any idea?

geo.
Feb 1 '07 #1
3 2225
duye
10 New Member
I am attempting to employ simple tables that my client can use to supply info to her website. I would like to read the table from an HTML page and transfer the tabular data to an array for manipulation.

any idea?

geo.
using DOM scripting,
1. get the table id
2. loop through the table's child element tr, td.
3, extract every td's text node value while push them into an array.
Feb 1 '07 #2
mrhoo
428 Contributor
/*
This function returns an Array whose elements each represent
a row in a table as an Array of its cells. It can handle a lot of rows and columns.

It only gets text, but it gets all the text, no matter how many
child elements it encounters, into a single string from each cell.

It will give you a start.
*/

Expand|Select|Wrap|Line Numbers
  1. function tableData(T){
  2.     if(!T) T= document.getElementsByTagName('table')[0];
  3.     if(!T || !T.nodeName || /table/i.test(T.nodeName)==false){
  4.          throw new Error('No table found');
  5.     }
  6.  
  7.  
  8.     var A= [];
  9.     var h= T.getElementsByTagName('thead');
  10.     var f= T.getElementsByTagName('tfoot');
  11.     var r= T.getElementsByTagName('tr');
  12.     var Lh= h.length;
  13.     var Lf= f.length;
  14.     var Lr= r.length;
  15.  
  16.     if(Lh) A.push(h[0]);
  17.     while(Lr) A.push(r[--Lr]);
  18.     if(Lf) A.push(f[0]);
  19.     var L=A.length;
  20.     var B, row, c, c2, Lc, cell, tem;
  21.  
  22.     for(var i=0; i<L; i++){
  23.         B= new Array();
  24.         row= A[i];
  25.         c= row.getElementsByTagName('th');
  26.         c2= row.getElementsByTagName('td');
  27.         Lc= c.length;
  28.         Lc2= c2.length;
  29.         while(Lc){
  30.             cell= c[--Lc];
  31.             temp= deepText(cell);
  32.             B.push(temp);
  33.         }
  34.         while(Lc2){
  35.             cell=c2[--Lc2];
  36.             temp= deepText(cell);
  37.             B.push(temp);
  38.         }
  39.         A[i]=B;
  40.     }
  41.     return A;
  42. }
Expand|Select|Wrap|Line Numbers
  1. function deepText(who){
  2.     var tmp= '', tex, cnt= 0;
  3.     var pa= who.childNodes;
  4.     while (pa && pa[cnt]){
  5.         tex= pa[cnt++];
  6.         if(tex.nodeType=== 3 && tex.data) tmp+= ' '+ tex.data;
  7.         else if(tex.hasChildNodes()) tmp+= deepText(tex);
  8.     }
  9.     return tmp;
  10. }

//Sample use:

Expand|Select|Wrap|Line Numbers
  1. function dataString(A){
  2.     var L=A.length;
  3.     var temp=[], tem;
  4.     for(var i=0;i<L;i++){
  5.         tem=A[i].join('\t\|\t');
  6.         temp.push((i+1)+'. '+tem);
  7.     }
  8.     return temp.join('\n');
  9. }
  10.  
  11. var A=tableData(); 
  12. var S=dataString(A); // 2 dimensional  array to string
  13. alert(dataString(A)) // data dump
Feb 1 '07 #3
GeoBen
2 New Member
Duye,

thanks. Yes, i was reading Danny Goodman's Javascript Bible when I thought to step over here and ask... save myself a little difficut reading.

MrHoo,

Thanks hugely.

am studying the code and will let you know the results. You have been an enormous help.


geo.
Feb 1 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
3468
by: Thomas Matthews | last post by:
Hi, How do I create a const table of pointers to member functions? I'm implementing a Factory pattern (or jump table). I want to iterate through the table, calling each member function until a...
1
4300
by: ajay | last post by:
I have following code for a slide menu but i twiked it to work for a single level menu. Open it in a Browser to get a clear picture. I have 2 Qs 1) How to make first entry as non-link. i.e i...
6
6459
by: Brendan.Collins | last post by:
Hi I have a javascript problem that has been annoying me for two days now and thought that a javascript expert might have the magic solution. I am populating a table dynamically from the...
10
7378
by: John Smith | last post by:
I know that uploading an image to a database has been covered, oh, about 3 trillion times. However, I haven't found anything covering uploading to a MySQL database with .net. Please don't...
6
3161
by: Kyle Teague | last post by:
What would give better performance, serializing a multidimensional array and storing it in a single entry in a table or storing each element of the array in a separate table and associating the...
21
3015
by: Stephen.Schoenberger | last post by:
Hello, My C is a bit rusty (.NET programmer normally but need to do this in C) and I need to read in a text file that is setup as a table. The general form of the file is 00000000 USNIST00Z...
6
7965
by: Romulo NF | last post by:
Greetings again to everyone, Im back to show this grid componenet i´ve developed. With this grid you can show the data like a normal table, remove the rows that you need, add rows, import data,...
0
1540
by: runway27 | last post by:
hi my question is about "SHOW TABLE STATUS LIKE 'tablename'"; following is the code i am presently using ================================================================== $conn =...
5
4902
by: jrod11 | last post by:
hi, I found a jquery html table sorting code i have implemented. I am trying to figure out how to edit how many colums there are, but every time i remove code that I think controls how many colums...
8
3878
by: thatcollegeguy | last post by:
http://smarterfootball.com/exits/theHTML.html I am not sure what is wrong w/ this code. The main issue is that the table that is in the initial html will empty its td but the table that I load...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7135
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7342
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7410
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1570
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
440
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.