By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,585 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,585 IT Pros & Developers. It's quick & easy.

Table data to Array?

P: 2
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
Share this Question
Share on Google+
3 Replies

P: 10
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

100+
P: 428
/*
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

P: 2
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

Post your reply

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