Table data to Array?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • GeoBen
    New Member
    • Feb 2007
    • 2

    Table data to Array?

    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.
  • duye
    New Member
    • Jan 2007
    • 10

    #2
    Originally posted by GeoBen
    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.

    Comment

    • mrhoo
      Contributor
      • Jun 2006
      • 428

      #3
      /*
      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.
      */

      Code:
      function tableData(T){
      	if(!T) T= document.getElementsByTagName('table')[0];
      	if(!T || !T.nodeName || /table/i.test(T.nodeName)==false){
      		 throw new Error('No table found');
      	}
      	
      
      	var A= [];
      	var h= T.getElementsByTagName('thead');
      	var f= T.getElementsByTagName('tfoot');
      	var r= T.getElementsByTagName('tr');
      	var Lh= h.length;
      	var Lf= f.length;
      	var Lr= r.length;
      
      	if(Lh) A.push(h[0]);
      	while(Lr) A.push(r[--Lr]);
      	if(Lf) A.push(f[0]);
      	var L=A.length;
      	var B, row, c, c2, Lc, cell, tem;
      
      	for(var i=0; i<L; i++){
      		B= new Array();
      		row= A[i];
      		c= row.getElementsByTagName('th');
      		c2= row.getElementsByTagName('td');
      		Lc= c.length;
      		Lc2= c2.length;
      		while(Lc){
      			cell= c[--Lc];
      			temp= deepText(cell);
      			B.push(temp);
      		}
      		while(Lc2){
      			cell=c2[--Lc2];
      			temp= deepText(cell);
      			B.push(temp);
      		}
      		A[i]=B;
      	}
      	return A;
      }
      Code:
      function deepText(who){
      	var tmp= '', tex, cnt= 0;
      	var pa= who.childNodes;
      	while (pa && pa[cnt]){
      		tex= pa[cnt++];
      		if(tex.nodeType=== 3 && tex.data) tmp+= ' '+ tex.data;
      		else if(tex.hasChildNodes()) tmp+= deepText(tex);
      	}
      	return tmp;
      }

      //Sample use:

      Code:
      function dataString(A){
      	var L=A.length;
      	var temp=[], tem;
      	for(var i=0;i<L;i++){
      		tem=A[i].join('\t\|\t');
      		temp.push((i+1)+'. '+tem);
      	}
      	return temp.join('\n');
      }
      
      var A=tableData(); 
      var S=dataString(A); // 2 dimensional  array to string
      alert(dataString(A)) // data dump

      Comment

      • GeoBen
        New Member
        • Feb 2007
        • 2

        #4
        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.

        Comment

        Working...