SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Copying a table to an Array?

    Hello everyone I was just try to copy the contents of a table and put it in a multidimentional array! But for some reason it keeps rewritting over each time only returning one row! I just cant figure out what I am doing wrong its probly simple! Any help would be great! ThanX

    Code:
    var tabular=new Array();
    function copyTable(){
    	obj=document.getElementById("theTab");
    	for(i=0,j=0;i<obj.rows.length,j<obj.rows(i).cells.length;i++,j++){
    	tabular[i]=new Array();
    	tabular[i].push(obj.rows(i).cells(j).innerText);
    	}
    	alert(tabular);
    }

  2. #2
    Non-Member QualityWebHosts's Avatar
    Join Date
    Sep 2003
    Location
    USA
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As you probably know, there is no such thing as a multidimensional array in JS.
    You want to get all the rows, and get all the TDs, and get the .innerHTML of each, I guess.
    Try something like this:

    var data=[]; var trs = myTable.getElementsByTagName('tr'); for (var i=0,len=trs.length;i<len;i++){ data[i]=[]; var tds = trs[i].getElementsByTagName('td'); for (var j=0,len2=tds.length;j<len2;j++) data[i][j]=tds[j].innerHTML; }

    If you want only rows in the tbody (not thead or tfoot) use myTable.tBodies[0]

  3. #3
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No such thing as a multidimensional array! Fiddlesticks!
    HTML Code:
    <html>
    <head>
    	<title>Test</title>
    
    	<script language="Javascript">
     
    	function tableToArray( tId )
    	{
    		var t = document.getElementById( tId ),
    			rows = t.getElementsByTagName( "tr" ),
    			row, cell, c = r = -1,
    			data = new Array();
    		while( row = rows[++r] )
    		{
    			data[r] = [];
    			while( cell = row.cells[++c] )
    			{
    				data[r][c] = cell.firstChild.nodeValue;
    			}
    			c = -1;
    		}
    		return data;
    	}
     
    	function init()
    	{
    		var data = tableToArray( 'tester' );
    		alert( data[0] );
    		alert( data[1] );
    		alert( data[2] );
    	}
    
    	</script>
    </head>
     
    <body onload="init()">
    	<table id="tester" border="1">
    		<tr>
    			<td>one</td>
    			<td>two</td>
    			<td>three</td>		
    		</tr>
    		<tr>
    			<td>four</td>
    			<td>five</td>
    			<td>six</td>		
    		</tr>
    		<tr>
    			<td>seven</td>
    			<td>eight</td>
    			<td>nine</td>		
    		</tr>
    	</table>
    </body>
    </html>
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  4. #4
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Iím sure you know already, but for the sake of interest itís worth pointing out that JavaScript really doesnít support multi-dimensional arrays. Or at least, no data-type exists to define a multi-dimensional array in JavaScript. But the elements of a one-dimensional array can be of any type, including another array. Thus you have arrays of arrays, getting round the ďlimitation,Ē and your script illustrates this.

    Myself, I kind of prefer it this way round anyway.
    Of course, that's just my opinion. I could be wrong.

  5. #5
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    * cough * What?

    I don't think /any/ language has a type definition as "multidimensional array", not even cobol.

    It's the tolerence of an array type as an element that allows for it, as you correctly say... but that's how all langauges allow it.

    It doesn't make it any less of a multi-dimensional array because it isn't declared as a multi-dimensional array

    It's arrays that only allow non-complex values at indexes that don't support multi-dimensional arrays, not one that just don't explicitly have a data type definition for them ( which would be pretty much all languages )

    HTH.

    G
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  6. #6
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Pascal supports multi-dimensional arrays that can be indexed using a [ 1, 2, 3, n... ] notation. What I am saying is this type of array is explicitly typed as being a multi-dimensional array, whereas an array in JavaScript can only be indexed singularly [ 1 ]. Itís ďmulti-dimensionalĒ capabilities come from the fact that any element of an array can also be an array.
    Of course, that's just my opinion. I could be wrong.

  7. #7
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope, really, you lost me.

    Multi-dimensional would look like:

    Code:
    [0] -> [1] [2] [3]
            |   |    |
            |   |    ->  [0] [1]
            |    ->      [0] [1] [2] [3]
            ->           [0] [1] [2]
    [1] -> ...
    So ... in Js, or whatever language, you'd access the index like: [0]->[2]->[3]

    You're telling me that JS, and every other langauge I can think of, save Pascal you tell me, works with a "singular index".

    How does the pascal index refer to the different layers then?

    G
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  8. #8
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Flawless_koder
    You're telling me that JS, and every other langauge I can think of, save Pascal you tell me, works with a "singular index".
    No. Iím telling you that with JS, an array is always one-dimensional:
    Code:
    var oArray = new Array ( );
    oArray [ 0 ] = 1;
    oArray [ 1 ] = 2;
    Now look at this:
    Code:
    var oArray = new Array ( );
    oArray [ 0 ] = 1;
    oArray [ 1 ] = new Array ( );
    Is oArray a multi-dimensional array? No, it is one-dimensional. Now take the following:
    Code:
    var oArray = new Array ( );
    oArray [ 0 ] = new Array ( );
    oArray [ 1 ] = new Array ( );
    Is oArray a multi-dimensional array? Practically, the structure serves the purpose. But the object oArray is still just a singularly indexed one-dimensional array loaded with elements that are also arrays.

    Now in Pascal, a one-dimensional array is declared thusly:
    Code:
    var oArray :Array [ 0 .. 9 ] of integer;
    
    begin
    oArray [ 0 ] := 1;
    end.
    And a two-dimensional array is declared like this:
    Code:
    var oArray :Array [ 0 .. 9, 0 .. 9 ] of integer;
    
    begin
    oArray [ 0 ] := 1; // Generates compiler error, because the array is not one-dimensional
    oArray [ 0, 0 ] := 1; // Two vertices specified - a valid index
    end.
    This is a typed, multi-dimensional array. To access a single element within it you must specify an index with two vertices. Three vertices for a three-dimensional array, four vertices for a four-dimensional array, and so forth.

    Because JavaScript is so loosely typed, you can emulate multi-dimensional arrays by create arrays of arrays, and indexing them as you specify: [ 0 ] [ 0 ] etc. But the fact remains that the data-types involved are all one-dimensional arrays.
    Of course, that's just my opinion. I could be wrong.

  9. #9
    Perl/Mason Guru Flawless_koder's Avatar
    Join Date
    Feb 2002
    Location
    Gatwick, UK
    Posts
    1,206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see your point.

    I'm fairly sure that I've dealt with explicitly typed multi-dimensional arrays before, but I can't remember what langauge it was - C++ maybe ?
    ---=| If you're going to buy a pet - get a Shetland Giraffe |=---

  10. #10
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Probably. Iím not really a C/C++ man, but I believe it supports multi-dimensional arrays.
    Of course, that's just my opinion. I could be wrong.

  11. #11
    SitePoint Addict
    Join Date
    Dec 2001
    Location
    Market Harborough, UK
    Posts
    206
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Bizarrely enough, I believe good old BASIC works this way (and I don't mean VB and other "new" versions....)

    I was doing this circa 1980 using MBASIC under cp/m (when I was at school, I hasten to add!)

    Maybe that's the "other language" you were thinking of?
    Paul Simpson, BSc, MCNI, MCNE

  12. #12
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Holy crap! I actually remember that from Pascal!

    That was like 8 years ago [img]images/smilies/xeye.gif[/img]

    Still, just the same, to say that Javascript doesn't support multidimensional arrays is a bit too misleading, esp. for the newbs. I recognize that it's explicitly erroneus to say that Javascript does support them as well.

    However, the point is, that we can create dimensional array structures, and that's what matters. How they are created, accessed, and their syntax, is largely irrelevant.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  13. #13
    SitePoint Evangelist Mr. Brownstone's Avatar
    Join Date
    May 2002
    Location
    Relative
    Posts
    452
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hehe, ditto.

    But these days I code in Delphi.
    Of course, that's just my opinion. I could be wrong.

  14. #14
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by beetle
    No such thing as a multidimensional array! Fiddlesticks!
    HTML Code:
    <html>
    <head>
    	<title>Test</title>
    
    	<script language="Javascript">
     
    	function tableToArray( tId )
    	{
    		var t = document.getElementById( tId ),
    			rows = t.getElementsByTagName( "tr" ),
    			row, cell, c = r = -1,
    			data = new Array();
    		while( row = rows[++r] )
    		{
    			data[r] = [];
    			while( cell = row.cells[++c] )
    			{
    				data[r][c] = cell.firstChild.nodeValue;
    			}
    			c = -1;
    		}
    		return data;
    	}
     
    	function init()
    	{
    		var data = tableToArray( 'tester' );
    		alert( data[0] );
    		alert( data[1] );
    		alert( data[2] );
    	}
    
    	</script>
    </head>
     
    <body onload="init()">
    	<table id="tester" border="1">
    		<tr>
    			<td>one</td>
    			<td>two</td>
    			<td>three</td>		
    		</tr>
    		<tr>
    			<td>four</td>
    			<td>five</td>
    			<td>six</td>		
    		</tr>
    		<tr>
    			<td>seven</td>
    			<td>eight</td>
    			<td>nine</td>		
    		</tr>
    	</table>
    </body>
    </html>

    Is this method not an actuall array I am unable to use data.sort(someFunction)! Just curious!

  15. #15
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is. But, as we've been discussing, a two-dimensional array (in JS) is an array of arrays. So, if you aren't considering that in your someFunction, then you may have problems.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  16. #16
    Under Construction Poop_Shoot's Avatar
    Join Date
    Jul 2003
    Location
    Sacramento, CA
    Posts
    330
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there anything that you dont know beetle! Must be something they put in the water there to breed the Ultimate Javascript Guru's! Or possibly a really good educational system! I wouldnt know what one is I've lived in california my whole life!

  17. #17
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just experience, my friend =)
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  18. #18
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Experience can make any man seem to be a genius.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •