473,805 Members | 1,963 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Misleading cellIndex values with rowspan/colspan

Using the .cellIndex property of a TH element to find out which table column
it is over can cause misleading results when the table has cells which have
rowspans or colspans greater than 1.
See example: http://www.javascripttoolbox.com/tem...cellindex.html
(code pasted below for reference)

This behaves according to specs and expectations, however the value of the
cellIndex property becomes less useful when using it to determine which
column to sort onclick, etc.

I plan to write some code to calculate the "actual" column index of each
cell in a <theadof a table. But if anyone has already done this exercise,
please do share so I can perhaps use my time working on something else :)

Thanks!

EXAMPLE CODE:

<html>
<head>
<title></title>
<script type="text/javascript">
window.onload = function() {
var ths = document.getEle mentsByTagName( 'TH');
for (var i=0; i<ths.length; i++) {
ths[i].innerHTML = ths[i].parentNode.row Index + "," + ths[i].cellIndex;
}
}
</script>
<style>
th {
vertical-align:top;
text-align:left;
width:50px;
}
</style>
</head>
<body>
<table border="1">
<thead>
<tr>
<th rowspan="2">&nb sp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>
<tr>
<th colspan="2">&nb sp;</th>
<th rowspan="2">&nb sp;</th>
<th>&nbsp;</th>
</tr>
<tr>
<th colspan="2">&nb sp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>
</thead>
</table>
</body>
</html>
--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 3 '06 #1
7 7627
Matt Kruse wrote:
Using the .cellIndex property of a TH element to find out which table column
it is over can cause misleading results when the table has cells which have
rowspans or colspans greater than 1.
See example: http://www.javascripttoolbox.com/tem...cellindex.html
(code pasted below for reference)

This behaves according to specs and expectations, however the value of the
cellIndex property becomes less useful when using it to determine which
column to sort onclick, etc.

I plan to write some code to calculate the "actual" column index of each
cell in a <theadof a table. But if anyone has already done this exercise,
please do share so I can perhaps use my time working on something else :)
Hiya Matt,

The good news: I have done this before. I think I did it because I
wanted a general routine such that when someone clicked on any table
cell, I could find every other cell in the same row or column.
The bad news: I don't know where I put it offhand.
More bad news: The situation is more complicated than your nice
example shows:

<table border bgcolor=blue>
<tr>
<td bgcolor=yellow> 1x1</td>
<td bgcolor=purple colspan=2>2 wide</td>
<td rowspan=3 bgcolor=green valign=bottom>3 deep</td>
</tr>
<tr><td bgcolor=red>1x1 </td></tr>
<tr><td colspan=2 bgcolor=pink>2 wide</td>
<td colspan=2>2 wide</td>
</tr>
</table>

In particular, as my example shows, multiple cells can overlap the same
"tile", and a given "tile" need not be overlapped by any cells at all!

So I might be able to advise you in this endeavor of yours. It seems
like you're after:

function colSpan(tableCe ll) {
// returns [leftCol,rightCo l] that the tableCell corresponds to
// leftCol equals rightCol if colSpan of the tableCell is 1 }

If you can guarantee that you will only query on the top row of the
table, then this function is fairly straightforward because you get the
first row of the table
(tableCell.pare ntNode.parentNo de.parentNode.r ows[0]) and then you march
through the cells one by one determining the colSpan array of each
till you get to the tableCell in question.

function colSpan(topRowC ell) {
// returns [leftCol,rightCo l] that topRowCell corresponds to
// leftCol equals rightCol iff colSpan of the tableCell is 1
// function fails badly if topRowCell isn't in the table's top row
var topRow=topRowCe ll.parentNode.p arentNode.paren tNode.rows[0];
var priorRight = -1;
for (var i=0;topRow.cell s[i]!=topRowCell;++ i)
priorRight += topRow.cells[i].colspan;
return [priorRight+1,pr iorRight+topRow .cells[i].colspan]; }
Disclaimer: I have not tested the above function, even for syntax
errors
If you can't make the top row guarantee, then it is much messier.

Csaba Gabor from Vienna

Aug 3 '06 #2
Csaba Gabor wrote:
In particular, as my example shows, multiple cells can overlap the
same "tile", and a given "tile" need not be overlapped by any cells
at all!
Overlapping of cells is, I believe, invalid.
If you can guarantee that you will only query on the top row of the
table
Which, of course, isn't possible.
If it were, then it would be trivial to find the true cellIndex value.
Colspans are easy to taken into consideration. Rowspans are more difficult,
and seem to require traversal through every row before and including the row
of the cell for which you want to find the true cellIndex.

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 3 '06 #3
Matt Kruse said the following on 8/3/2006 4:43 PM:
Csaba Gabor wrote:
>In particular, as my example shows, multiple cells can overlap the
same "tile", and a given "tile" need not be overlapped by any cells
at all!

Overlapping of cells is, I believe, invalid.
It is, and is only "honored" by IE by joining all the cells.

--
Randy
comp.lang.javas cript FAQ - http://jibbering.com/faq & newsgroup weekly
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Aug 3 '06 #4
Randy Webb wrote:
Matt Kruse said the following on 8/3/2006 4:43 PM:
Csaba Gabor wrote:
In particular, as my example shows, multiple cells can overlap the
same "tile", and a given "tile" need not be overlapped by any cells
at all!
Overlapping of cells is, I believe, invalid.

It is, and is only "honored" by IE by joining all the cells.
I don't really understand the above two comments.
I have tested the table that I gave on both IE 6 and FF 1.5 (and
similar tables on earlier versions a few years ago), and they exhibit
both the "hole" at tiles [1,1] and [1,2] (coordinates are 0 based) with
a blue color (the bgcolor of the table), and a tile overlapped by two
cells at [2,3].

Note that this latter tile shows the (overlapped) text of two cells.
Furthermore, if I had set a bgcolor on the last cell, then it would
have covered the green cell so that the effect would not have been
readily observable.

Csaba

Aug 3 '06 #5
Matt Kruse wrote:
Colspans are easy to taken into consideration. Rowspans are more
difficult, and seem to require traversal through every row before and
including the row of the cell for which you want to find the true
cellIndex.
Well, I've thrown together a really messy proof-of-concept at the same url:
http://www.javascripttoolbox.com/tem...cellindex.html

It appears to work, so I'm going to clean it up, optimize it, and add it to
my table lib.
If anyone sees any problem with the general logic, please do critique.
I guarantee the "finished" version will be much prettier ;)

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Aug 3 '06 #6
Matt Kruse wrote:
Csaba Gabor wrote:
If you can guarantee that you will only query on the top row of the
table

Which, of course, isn't possible.
If it were, then it would be trivial to find the true cellIndex value.
Colspans are easy to taken into consideration. Rowspans are more difficult,
and seem to require traversal through every row before and including the row
of the cell for which you want to find the true cellIndex.
OK, then here's the approach that I would take if I were to do this
again. I would build an associative array of all tiles and have the
value of each one be the cell that covers it (I am assuming that the
table is such that no two cells overlap the same tile - if so, you
could generalize my scheme). The index of tile at position [n,m] will
be n+"x"+m. Thus, the pseudo code is something like:

function nextFreeIdx(aTi les, rowNum, colStart, maxCol) {
for (;colStart<=max Col;++colStart) // find first free index
if (!aTiles[rowNum+"x"+colS tart]) break;
return colStart; }

function findTileToCellM ap(table, terminatingCell ) {
// pass in terminatingCell if you want to get a specific cell's info
// aTiles maps tile position to cell that covers it.
var i,j,csp,maxCol=-1, aTiles = [];
for (rowNum=0;rowNu m<table.rows.le ngth;++rowNum) {
var thisRow=table.r ows[rowNum];
var colNum=-1;
for (cellNum=0;cell Num<thisRow.cel ls.length;++cel lNum) {
thisCell=thisRo w.cells[cellNum];
csp=thisCell.co lspan;
colNum=nextFree Idx(aTiles, rowNum, colNum+1, maxCol);
for (i=0;i<thisCell .rowspan;++i)
for (j=0;j<csp;++j)
aTiles[(thisRow+i)+"x" +(colNum+j)]=thisCell;
if (colNum+csp-1>maxCol) maxCol=colNum+c sp-1;
if (thisCell==term inatingCell) return [rowNum, colNum]; // [top, left]
} }
return aTiles; }

To make this more usable, you should really affix to each cell it's top
left tile position as you compute it. That way, you can not only go
from the tile to the cell, but from the cell to the tile (which is what
was desired in the first place). One should be aware of possible
issues when cells are collapsed/combined (eg. table is 3 tiles high.
Cell at tile 0,3 is two by two, cell at tile 2,3 is two wide by 1 high
=cols 3 and 4 are collapsed into one).

Csaba Gabor

Aug 3 '06 #7
Matt Kruse wrote:
Matt Kruse wrote:
Colspans are easy to taken into consideration. Rowspans are more
difficult, and seem to require traversal through every row before and
including the row of the cell for which you want to find the true
cellIndex.

Well, I've thrown together a really messy proof-of-concept at the same url:
http://www.javascripttoolbox.com/tem...cellindex.html

It appears to work, so I'm going to clean it up, optimize it, and add it to
my table lib.
If anyone sees any problem with the general logic, please do critique.
I guarantee the "finished" version will be much prettier ;)
Seems to work pretty nicely. Nice job.
Since I sent in my suggestion before seeing that you'd already solved
your problem, I figured I may as well clean up my work, too. I've cast
it into the form that you are using. Just cut and paste and you should
see no difference :)
function findTileToCellM ap(table, terminatingCell ) {
// pass in terminatingCell if you want to get a specific cell's info
// aTiles maps tile position to cell that covers it.
var cellNum, colNum, aTiles = [];
for (var rowNum=0;rowNum <table.rows.len gth;++rowNum) {
var thisRow=table.r ows[rowNum];
for (cellNum=0, colNum=-1;cellNum<thisR ow.cells.length ;++cellNum) {
var thisCell=thisRo w.cells[cellNum];
while (aTiles[rowNum+"x"+ ++colNum]); // next free index
for (var i=0;i<(thisCell .rowSpan || 1);++i)
for (var j=0;j<(thisCell .colSpan || 1);++j)
aTiles[(rowNum+i)+"x"+ (colNum+j)]=thisCell;
thisCell.tile = [rowNum, colNum]; // [top, left] - cache value
if (thisCell==term inatingCell) return thisCell.tile; } }
return aTiles; }
function getActualCellIn dex(cell) {
if (cell.tile) return cell.tile[1]; // cached
var table = cell.parentNode .parentNode.par entNode;
findTileToCellM ap (table); // cache values here
return cell.tile[1]; }
You could also implement getActualCellIn dex as:

function getActualCellIn dex(cell) {
// doesn't use cached values
var table = cell.parentNode .parentNode.par entNode;
return findTileToCellM ap (table, cell)[1]; }

Csaba Gabor

Aug 4 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
6762
by: Greg Adourian | last post by:
Hi, I'm breaking my head over the following few lines of code, generated with Photoshop Slices. As soon as the structure is slightly complicated, the output is broken. I can't seem to follow the logic behind it, figuring out when the table breaks or when it's works OK. I searched for tutorials with examples, so far all I've found are tutorials with cells that meet each other at corners, nothing similar to what I have. I'm assuming if...
19
13386
by: Logix | last post by:
Hello! I'm trying to make a sort of online page building system. In order to do this, I represent my page using a HTML table. One of the most basic templates would be a page divided in six equally proportioned rectangles: <table border="1" cellspacing="0" cellpadding="0" width="200" height="200"> <tr>
12
3977
by: plugwalsh | last post by:
Hi I need to generate an HTML table that looks like the following: (Two cells, A & B) ------------------- | | | | | B | | | | | |--------|
7
349
by: Gawel | last post by:
I need necessarily set RowSpan(ColSpan) property of cell in aspx file(not in cs file). But below statement does not work :(. Any idea ? <TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" border="1" runat="server"> <TR> <TD RowSpan="<%# 3 %>">a</TD> <TD>a</TD>
0
4105
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me with my problems. Now for my new little problem,I had a problem posting the values from checkbox fields to a database and thats the obstacle I overcame. Now the second part is my new problem is that I want that the next time that page loads for...
2
1857
by: gnewsgroup | last post by:
I have a stored procedure that give some data that are sorted by Col1, Col2, Col3, Col4. I have a picture which tells you how I want the data to be presented in a web form. Click below to see the picture. http://gnewsgroup.googlepages.com/rowspanajacentcellswiththesamevalue It's basically a tabulation of a tree. For a given column, if an entry has been displayed, don't repeat it. Thus we need rowspan (as
5
1667
by: Sansasoon | last post by:
Hi there, I am supposed to do a shop ... which I ve done so far. The only thing that I can't get working is adding up to get the result It would be really great if someone could help me (Sorry it is in GERMAN) ThX in advance Sansasoon <html> <head> <title>Warenkorb berechnen</title>
5
2684
by: leemarquis | last post by:
Is there a way to determine the correct cellindex of cells in a table when some of the cells have rowspans greater than 1. Perhaps correct is the wrong word - but when I have a cell with a rowspan greater than one, the cellindex of the the cells to its right and one row down are 1 less than I expect. This is causing my cell highlight function to behave incorectly as instead of highlighting say 3 cells in a column, it highlights the first and...
4
3122
Vkas
by: Vkas | last post by:
HELLO!!!!!!!!!!!!! me using ASP.net 2.0, language VB.net! working tool Visula studio.Net 2005 I HAVE CREATED A simple WEB User control (calender.ascx) having a text box , a button, and a calender control
0
10607
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10359
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10104
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7645
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6875
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5541
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4317
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3843
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.