469,323 Members | 1,560 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

Help please !! Get th table id from the Row Id

I'm working on an existing project.

I've got a Row Id and I must find the table id (the table who contains the
row).

Is it possible ?

In fact, from a Row Id I must delete the row but I don't know the name or
the table Id.

Please help,

Stan
Jul 20 '05 #1
5 13616
Stanislas wrote:
I'm working on an existing project.

I've got a Row Id and I must find the table id (the table who contains the
row).

Is it possible ?

In fact, from a Row Id I must delete the row but I don't know the name or
the table Id.

Please help,

Stan


I dont think there is a direct way, but you can traverse the DOM tree.

A row must have a parent, grandparent, etc that is of type TABLE. So
you do something like this (pseudocode) :

function find_owning_table( row )
{
current_node = row;
while(current_node.parentNode.nodeName != "TABLE")
current_node = current_row.parentNode;

return current_row.parentNode;
}

Brian

Jul 20 '05 #2
On Wed, 21 Jan 2004 19:31:38 +0100, Stanislas <sr******@intertek.fr> wrote:
I've got a Row Id and I must find the table id (the table who contains
the
row).

Is it possible ?
Using the DOM, yes.

var tableRow = document.getElementById( 'rowID' );
// The first parent node points to the TBODY that contains the row.
// Even if one wasn't explicitly added, a TBODY element will be
// added implicitly
var table = tableRow.parentNode.parentNode;

This gives you references to the table row, and its containing table.
In fact, from a Row Id I must delete the row but I don't know the name or
the table Id.


Using the results above, this will delete the row:

table.deleteRow( tableRow.rowIndex );

If you aren't going to use the table reference for any other purpose, you
could make this slightly more efficient.

var tableRow = document.getElementById( 'rowID' );
tableRow.parentNode.deleteRow( tableRow.sectionRowIndex );

Don't forget that you should for support of the properties and methods
accessed.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #3
Michael Winter wrote:
On Wed, 21 Jan 2004 19:31:38 +0100, Stanislas <sr******@intertek.fr> wrote:
I've got a Row Id and I must find the table id (the table who contains
the
row).

Is it possible ?

Using the DOM, yes.

var tableRow = document.getElementById( 'rowID' );
// The first parent node points to the TBODY that contains the row.
// Even if one wasn't explicitly added, a TBODY element will be
// added implicitly
var table = tableRow.parentNode.parentNode;


Hmmmm... Is this a safe assumption? Do all DOM-compliant browsers do it
this way? What if it does not implicityly add a TBODY? I think a more
save approach is to traverse upwards, until you find the TABLE tag...
(like I mentioned in my earlier post)

What do you think?
Brian

Jul 20 '05 #4
On Wed, 21 Jan 2004 14:39:48 -0500, Brian Genisio
<Br**********@yahoo.com> wrote:
Hmmmm... Is this a safe assumption? Do all DOM-compliant browsers do it
this way? What if it does not implicityly add a TBODY?
If it does or not would depend on the doctype, I agree I wouldn't rely
on it. (and if you're of the opinion that compatilbility with XHTML
user agents is relevant you can guarantee they won't.)
I think a more
save approach is to traverse upwards, until you find the TABLE tag...
(like I mentioned in my earlier post)


Makes sense to me!

Jim.
--
comp.lang.javascript FAQ - http://jibbering.com/faq/

Jul 20 '05 #5
On Wed, 21 Jan 2004 14:39:48 -0500, Brian Genisio <Br**********@yahoo.com>
wrote:
Michael Winter wrote:


<snip>
var tableRow = document.getElementById( 'rowID' );
// The first parent node points to the TBODY that contains the row.
// Even if one wasn't explicitly added, a TBODY element will be
// added implicitly
var table = tableRow.parentNode.parentNode;


Hmmmm... Is this a safe assumption? Do all DOM-compliant browsers do it
this way? What if it does not implicityly add a TBODY? I think a more
save approach is to traverse upwards, until you find the TABLE tag...
(like I mentioned in my earlier post)

What do you think?


Technically, yes. The DTD description of the table requires that at least
one TBODY element must be present inside a table. If there is only one
body, the user may omit the actual tag, but the TBODY is still there.
However, I guess there aren't really any guarantees.

You wouldn't need to traverse far, anyway. The table node will either be
the first parent node, if TBODY is omitted, or the second, if it isn't.

Mike

--
Michael Winter
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Ben | last post: by
1 post views Thread by HolaGoogle | last post: by
5 posts views Thread by Craig Keightley | last post: by
23 posts views Thread by Jason | last post: by
1 post views Thread by lilbit02 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.