Click to See Complete Forum and Search --> : Enumerating rows in dynamically generated table


sharapov
04-19-2003, 05:21 PM
I am generating a table from the database. What I need is to calculate how many rows in the table and write the number of the row in the first cell of each row. In other words if table has 10 rows (for example) I want the first cell in the first row to display 1., the first cell in the second row to display 2. etc.

Since I am pulling all the data from the database I can't rely on cell's ID or names. Is there any other way? Can anybody help?

Thanks.

khalidali63
04-19-2003, 05:58 PM
just curius,how come you are not able to do that when you create the table using data from the db?
see below how I think It shoudl have been done

some database records loop{
<tr>
at this level I know the records I want to put in the td and I'd put ctr++ in the first cell
</tr>

}

make any sense?

sharapov
04-19-2003, 06:06 PM
Well, I can do it when I create the table, but I need the solution for the scenario that I described above. I give user ability to delete/remove rows from the table. When this hapens assuming row numbering was created when the table is generetade originally the row sequence doesn't update automatically. In other words if I have table with 5 rows (ex: 12345) and user removes row 4, my table rows will display 1235 and not 1234 (as it supposed to). I don't want to send the script back to the server (after user delets the row) just to re-calculate row numbering. That is why I need the solution using javascript! Thanks.

khalidali63
04-19-2003, 06:17 PM
not quite,what you can do is,once a row is deleted or added run a check for how many rows are there and then update the indexing...

sharapov
04-19-2003, 06:19 PM
Which indexing are you talking about?

khalidali63
04-19-2003, 07:59 PM
the row numbers...
once a row is added or deleted,thencheck how many rows are there and update the appropriate cells innerText with respective row number..

make sense?

sharapov
04-19-2003, 08:02 PM
I undarstand what you are saying, but how can I update the innerText of the cell if I can't refer it by ID. Is there any other way to accsess the cell. Do you mind giving me an example?

Thanks.

khalidali63
04-19-2003, 08:23 PM
yes there is a pure DOM way of getting reference to any html element

now this will be limitied only by your design of the page or the number of table elementsin your doc.just for the example sake say you have 1 table
then this how you would do it.
var rows = document.getElementsByTagName("tr");

this will give you all the tr's in this a table( if more then 1 table then it should give you a nodelist of all the trs).

Now you have a rows( NodeList) object.

var numberOfRows = rows.length;
loop throwgh the rows with x<numberOfRows;
and update the first(hoping) cell
rows[x][0].innerHTML = (x+1);

There now you have almost all the code..

sharapov
04-19-2003, 08:43 PM
khalidali63,

Thank you for your help. I solved it. Below is the function for those who is interested.


function test(){

var rows = document.getElementsByTagName("tr");
var numberOfRows = rows.length;

for (i=0; i<numberOfRows; i++){
rows(i).cells(0).innerHTML = i+1;
}
}

khalidali63
04-19-2003, 08:46 PM
you are welcome...

glad to be of help



:D

dbutlerman
03-24-2006, 04:20 PM
How do I tell the function to display those numbers?

dbutlerman
03-24-2006, 04:31 PM
I am new to Java so a walkthrough would be greatly appreciated.