473,395 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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.getElementsByTagName('TH');
for (var i=0; i<ths.length; i++) {
ths[i].innerHTML = ths[i].parentNode.rowIndex + "," + 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">&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
<th>&nbsp;</th>
</tr>
<tr>
<th colspan="2">&nbsp;</th>
<th rowspan="2">&nbsp;</th>
<th>&nbsp;</th>
</tr>
<tr>
<th colspan="2">&nbsp;</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 7552
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(tableCell) {
// returns [leftCol,rightCol] 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.parentNode.parentNode.parentNode.rows[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(topRowCell) {
// returns [leftCol,rightCol] 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=topRowCell.parentNode.parentNode.parentNode .rows[0];
var priorRight = -1;
for (var i=0;topRow.cells[i]!=topRowCell;++i)
priorRight += topRow.cells[i].colspan;
return [priorRight+1,priorRight+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.javascript 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(aTiles, rowNum, colStart, maxCol) {
for (;colStart<=maxCol;++colStart) // find first free index
if (!aTiles[rowNum+"x"+colStart]) break;
return colStart; }

function findTileToCellMap(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;rowNum<table.rows.length;++rowNum) {
var thisRow=table.rows[rowNum];
var colNum=-1;
for (cellNum=0;cellNum<thisRow.cells.length;++cellNum) {
thisCell=thisRow.cells[cellNum];
csp=thisCell.colspan;
colNum=nextFreeIdx(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+csp-1;
if (thisCell==terminatingCell) 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 findTileToCellMap(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.length;++rowNum) {
var thisRow=table.rows[rowNum];
for (cellNum=0, colNum=-1;cellNum<thisRow.cells.length;++cellNum) {
var thisCell=thisRow.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==terminatingCell) return thisCell.tile; } }
return aTiles; }
function getActualCellIndex(cell) {
if (cell.tile) return cell.tile[1]; // cached
var table = cell.parentNode.parentNode.parentNode;
findTileToCellMap (table); // cache values here
return cell.tile[1]; }
You could also implement getActualCellIndex as:

function getActualCellIndex(cell) {
// doesn't use cached values
var table = cell.parentNode.parentNode.parentNode;
return findTileToCellMap (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
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...
19
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...
12
by: plugwalsh | last post by:
Hi I need to generate an HTML table that looks like the following: (Two cells, A & B) ------------------- | | | | | B | | | | | |--------|
7
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"...
0
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...
2
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...
5
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...
5
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...
4
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.