Welcome to Dream.In.Code
Getting Help is Easy!

Join 136,458 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,873 people online right now. Registration is fast and FREE... Join Now!




Table contents to array

 
Reply to this topicStart new topic

Table contents to array

BetaWar
29 Jul, 2008 - 09:42 PM
Post #1

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,022



Thanked: 81 times
Dream Kudos: 1175
My Contributions
Okay, I have a table that looks like so (more or less)

CODE
Age | Name
14   | John  
20   | Adam


Where Age and Name are table heads. I am trying to get the contents of the table rows (excluding the heads) into an array based on column. So array 1 would be ("14", "20") the ages, and array 2 would be ("John", "Adam")

I have tried using this as my JS (I know it has errors and is overall incorrect)
CODE
function makeSortable(obj){
  obj.order = new Array();
  for(var x=0; x<obj.rows.length; x++){
    if(x == 0){
      for(var y=0; y<obj.rows[0].childNodes.length; y++){
        obj.rows[0].childNodes[y].onclick = function(){
          alert(this.innerHTML+" was clicked");
        }
        obj.order.push(y);
      }
    }
    else{
      for(var y=0; y<obj.rows[x].childNodes.length; y++){
        obj.order[y].push(obj.rows[x].childNodes[y].innerHTML);
      }
      alert(obj.order);
    }
  }
}

function init(){
  tables = document.getElementsByTagName("TABLE");
  for(var i=0; i<tables.length; i++){
    if(tables[i].className == 'sortable'){
      makeSortable(tables[i]);
    }
  }
}


And I have also tried this (Whcih I am a bit confused as to whyu it isn't working but it is (no errors are outputted with this one, it just doesn't work).

CODE
function makeSortable(obj){
  this.order = new Object();
  this.order.colnames = new Array("T");
  alert(this);
  for(var x=0; x<obj.rows.length; x++){
    if(x == 0){
      for(var y=0; y<obj.rows[0].childNodes.length; y++){
        obj.rows[0].childNodes[y].onclick = function(){
          alert(this.innerHTML+" was clicked");
        }
alert(this.order);
        a = new Array();
        this.order.colnames.push(a);
      }
    }
    else{
      for(var y=0; y<obj.rows[x].childNodes.length; y++){
        this.order.colnames[y].a.push(obj.rows[x].childNodes[y]);
      }
      alert(this.order);
    }
  }
}

vat tbl_ = new Array();
function init(){
  tables = document.getElementsByTagName("TABLE");
  for(var i=0; i<tables.length; i++){
    if(tables[i].className == 'sortable'){
      tbl_[i] = new makeSortable(tables[i]);
    }
  }
}


Error Output - Object doesn't support this property or method
Talking about tis line (first off) - obj.order[y].push(obj.rows[x].childNodes[y].innerHTML);

Here is what my HTML looks like at the moment:

CODE
<body onload="init();">

<table class="sortable">
  <tr>
    <th id="col_1" name="numeric">
      Age
    </th>
    <th id="col_2" name="alpha">
      Name
    </th>
  </tr>
  <tr>
    <td>
      14
    </td>
    <td>
      John
    </td>
  </tr>
  <tr>
    <td>
      20
    </td>
    <td>
      Adam
    </td>
  </tr>
</table>


All I need to get done (at the moment) is dividing the table columns up into separate arrays (so I can sort them and everything later and then reorder the table without returning to the server side script after the page has loaded).

I know that there are other scripts out there that do this already but I am just trying to make it from scratch because I have ideas on interesting (and useful) ways of changing it to be better than what is currently out there.

I also understand that the code is a little flawed but I haven't done much difficult stuff with JS pretty much all summer so now that I am getting back into it I begin to realize how much I have lost.

Thanks in advance.
User is online!Profile CardPM
+Quote Post

ahmad_511
RE: Table Contents To Array
1 Aug, 2008 - 08:18 PM
Post #2

D.I.C Regular
Group Icon

Joined: 28 Apr, 2007
Posts: 351



Thanked: 8 times
Dream Kudos: 400
My Contributions
hello,
see if this can help
jscript

function makeSortable(obj){
order = new Array();
for(var y=0; y<obj.rows.length; y++){
if(y == 0){
for(var x=0; x<obj.rows[0].childNodes.length; x++){
obj.rows[0].childNodes[x].onclick = function(){
alert(this.innerHTML+" was clicked");
}
order[x]=new Array();

}
}


else{
for(var x=0; x<obj.rows[y].childNodes.length; x++){
order[x].push(obj.rows[y].childNodes[x].innerHTML);

}
}
}
obj.order=order;
// displaying colums contents
for (x=0;x<obj.order.length;x++){
for (y=0;y<obj.order[x].length;y++){
alert(obj.order[x][y])
}
}
}


here x refers to the column's index and y to the row's index
anyway, it needs a little bit more to work with FF
User is offlineProfile CardPM
+Quote Post

BetaWar
RE: Table Contents To Array
1 Aug, 2008 - 08:38 PM
Post #3

#include <soul.h>
Group Icon

Joined: 7 Sep, 2006
Posts: 2,022



Thanked: 81 times
Dream Kudos: 1175
My Contributions
Thanks, I have finally gotten it (I think) to get all cells into an array, now to get hte sorting to work smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:45PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month