Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Cell click reveals row number

Reply
Thread Tools

Cell click reveals row number

 
 
Camet
Guest
Posts: n/a
 
      07-05-2006
I have a table with a dynamic number of rows and cells. From the
nature of the script I am writing, I cannot use IDs.

Each row has from 1 to 3 cells. Javascript tables have cells[] and
rows[].

cells [] is an array of all the cells of the table, supposedly 1D.
rows [] is an array of all the rows of the table. You can grab the
cells from the row click.

I wish to go the other way

Is there any way to find out what row was clicked on by the mouse when
the left most cell is clicked but the script ignores the rest of the
cells in the row.

 
Reply With Quote
 
 
 
 
Matt Kruse
Guest
Posts: n/a
 
      07-05-2006
Camet wrote:
> Is there any way to find out what row was clicked on by the mouse
> when the left most cell is clicked but the script ignores the rest of
> the cells in the row.


You can just go up the parentNode chain from whatever was clicked until you
find a TR.
Then, look at the TR's rowIndex property.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com


 
Reply With Quote
 
 
 
 
Camet
Guest
Posts: n/a
 
      07-06-2006
> You can just go up the parentNode chain from whatever was clicked until you
> find a TR.
> Then, look at the TR's rowIndex property.
>
> --
> Matt Kruse
> http://www.JavascriptToolbox.com
> http://www.AjaxToolbox.com


I do not know what you mean by parentNode in relation to tables. I
have only used those in relation to reading xml files.

 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      07-06-2006
Camet wrote on 06 jul 2006 in comp.lang.javascript:

>> You can just go up the parentNode chain from whatever was clicked
>> until you find a TR.
>> Then, look at the TR's rowIndex property.
>>
>> --
>> Matt Kruse
>> http://www.JavascriptToolbox.com
>> http://www.AjaxToolbox.com

>
> I do not know what you mean by parentNode in relation to tables. I
> have only used those in relation to reading xml files.
>
>


In DOM like in any other tree structure,
all elements exept the root["global"]
have a parent[node].

DOM elements are nearly people, who have two.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Camet
Guest
Posts: n/a
 
      07-06-2006
> In DOM like in any other tree structure,
> all elements exept the root["global"]
> have a parent[node].
>
> DOM elements are nearly people, who have two.
>
> --
> Evertjan.
> The Netherlands.
> (Please change the x'es to dots in my emailaddress)


Okay, can you please give an example of this in commented code?

Thanks

Camet

 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-06-2006
"Camet" <> writes:

> I do not know what you mean by parentNode in relation to tables. I
> have only used those in relation to reading xml files.


The XML files are probably read into a W3C DOM structure. The same
thing happens with HTML in a browser, and there is a ECMAScript
binding for the DOM objects and methods that specifies how to use
it:
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>

In this case, you can do something like:

function findRowNumber(element) { // element is a descendent of a tr element
while(element.tagName.toLowerCase() != "tr") {
element = element.parentNode; // breaks if no "tr" in path to root
}
return element.rowIndex;
}

and call it when clicking on the table:

<table
onclick="doSomethingWith(findRowNumber(event.targe t||event.srcElement));">
...
</table>

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Evertjan.
Guest
Posts: n/a
 
      07-06-2006
Lasse Reichstein Nielsen wrote on 06 jul 2006 in comp.lang.javascript:

> "Camet" <> writes:
>
>> I do not know what you mean by parentNode in relation to tables. I
>> have only used those in relation to reading xml files.

>
> The XML files are probably read into a W3C DOM structure. The same
> thing happens with HTML in a browser, and there is a ECMAScript
> binding for the DOM objects and methods that specifies how to use
> it:
> <URL:http://www.w3.org/TR/DOM-Level-2-HTML/ecma-script-binding.html>
>
> In this case, you can do something like:
>
> function findRowNumber(element) { // element is a descendent of a tr
> element
> while(element.tagName.toLowerCase() != "tr") {
> element = element.parentNode; // breaks if no "tr" in path to
> root
> }
> return element.rowIndex;
> }
>
> and call it when clicking on the table:
>
> <table
> onclick="doSomethingWith(findRowNumber(event.targe t||event.srcElemen
> t));"> ...
> </table>


In IE HTML I simply do:

<table border=1>
<tr><td onclick='whichRowandCell(this)'>aaaaa
<td onclick='whichRowandCell(this)'>bbbbb
<tr><td onclick='whichRowandCell(this)'>cccc
<td onclick='whichRowandCell(this)'>ddd
</table>

<script type='text/javascript'>
function whichRowandCell(x){
alert('rowIndex: '+x.parentNode.rowIndex+
'\ncellIndex: '+x.cellIndex)
}
</script>

Should one expect a element between the tr and the td?
I think not.




--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      07-06-2006
"Evertjan." <> writes:

> Should one expect a element between the tr and the td?
> I think not.


Not in valid HTML, no. If you put the onclick on the table and use
event.target (with IE fallback to event.srcElement), then you might
need to traverse upwards to find the cell and row (but on the other
hand, you should also remember, as I didn't, the case where you click
on the table between rows.

/L
--
Lasse Reichstein Nielsen -
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to change the style for the row while mouse over the cell of that row? Cylix Javascript 0 06-13-2006 11:06 AM
ok I can do a totals row but how about a percentage row after each data row D ASP .Net Datagrid Control 0 05-23-2005 04:10 PM
Highlight the row and click anywhere to select a row in a GridView Fernando Lopes ASP .Net 0 04-28-2005 12:24 PM
Router reveals port activity RAH Computer Support 5 08-28-2004 10:03 AM
I need to have a tooltip appear over a cell showing data from another cell in the same row. Thomasa Gregg ASP .Net Datagrid Control 1 06-09-2004 08:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57