Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1
10-08-2008, 12:30 PM #1
- Join Date
- Oct 2002
- Thanked 0 Times in 0 Posts
getCellIndex advanced, taking colspan and rowspan into account
I'm trying to build a getCellIndex function to get the position of a cell in its row.
I know you can easily read out the cellIndex attribute or count the cells in front of the target cell, but I want to take spanning into account. colspan is not that much of an issue, we can just count the cells in front of the target cell by their colspan, but it's the rowspan (of cells above that reaches onto the row of the target cell) that's really being a pain. How do you know whether such a cell that's spanning to the row of our target cell is in front of the target cell or not?!
I've been wrapping my mind around this problem for some time now and I think I finally got to the correct solution.
First I just plainly count the cells in front of the target cell (by their colspan) and remember it as initIndex
Then I check every row above the row of the target cell to see if there is any cell spanning to the row of our target cell. To be sure to only check the columns that can be in front of the target cell I check initIndex + 1 cell in each row. Now on every cell I find to be spanning down onto the row of the target cell I break and start over with an initIndex increased by 1. I mark such cells by adding an attribute to them to be able to recognize them in the next run.
I increase the initIndex because the cell spanning down onto the row of the target cell is 'pushing' the target cell no position to the right. And I restart because we have been checking not enough cell in the rows above up untill now.
This happens again for every cell I find spanning down to the row of the target cell.
Then next to cells spanning all the way down to the row of the target cell I also keep record of cells just spanning down. This to know that we need to check less cell in the rows below. For this I keep an array on which I push an element for every cell spanning down, with the value of the element being the rowspan of the detected cell. After every row I decrease every value in the array and for the next row I count the element in the array whose value is higher than 0. That's the number of cells spanning down onto the row we're currenlty checking and thus can skip or in more technical term: the cells we need to check on every row is initIndex + 1 - number of cells spanning down.
I also take the colspan into account on each row, counting the cells their colspan instead of the cells themselves.
The initIndex when we reach the row of the target cell is the real index of the cell! (first cell in a row is at index 0 by the way)
Testing this procedure manually to these example tables below brings up the correct index, so I believe this is the solution.
If anyone can think up something better or can correct me if I'm wrong, please do