Need help? Post your question and get tips & solutions from a community of 402,912 IT Pros & Developers.
It's quick & easy.
P: 2
|
has anyone successfully written a cell merging script that will vertically merge cells with identical values?
I saw a post from a few weeks ago but I can't get in touch with the post writer (samn). I've tried to fill in the missing parts, but I can't seem to get his fragment of a script to work. Any help?
Thanks,
ninerkz
| |
Share this Question
1 Reply
P: 2
|
After literally months of trying to figure this one out (my javascript skills are at a beginner level), I have figured this one out. What I can't figure out is why no one has written or posted a script like this before.
here's the code: Expand|Select|Wrap|Line Numbers - function init() {
-
if (!document.getElementsByTagName || !document.getElementById) return;
-
-
var x = document.getElementsByTagName('table')[0];
-
var xbody = document.getElementById('merge'); //tbody elem (so the thead doesn't get involved)
-
var ybody = document.createElement('tbody');
-
-
// manipulate the tbodies
-
var xrows = xbody.getElementsByTagName('tr');
-
var numXrows = xrows.length;
-
-
var xcells = xbody.getElementsByTagName('td');
-
var xcols = xcells.length / xrows.length;
-
-
var identical;
-
var mergeNum;
-
-
for (var i = 0; i < xcols; i++) { // going across
-
mergeNum = 1;
-
for (var j = 0; j < numXrows; j++) { // going down
-
// for each row in the xbody, read the value of the cell
-
if (mergeNum <= 1) {
-
var myRow = xbody.getElementsByTagName('tr')[j];
-
var myCell= myRow.getElementsByTagName('td')[i];
-
var myCellTxt = myCell.childNodes[0].data;
-
} else {
-
// myRow should be the same as the previous iteration of the loop.
-
}
-
-
// get the next row, same column
-
if (xbody.getElementsByTagName('tr')[j+1]) {
-
var nxtRow = xbody.getElementsByTagName('tr')[j+1];
-
var nxtCell = nxtRow.getElementsByTagName('td')[i];
-
var nxtCellTxt = nxtCell.childNodes[0].data;
-
} else {
-
// next row doesn't exist.
-
}
-
-
// compare the current cell and the next cell
-
if (myCellTxt == nxtCellTxt) {
-
identical = true;
-
mergeNum += 1;
-
nxtCell.style.display = 'none';
-
myCell.rowSpan = mergeNum;
-
} else { // reset the mergeNum
-
mergeNum = 1;
-
}
-
-
// account for last row being completely hidden
-
if (myCell.style.display == 'none' && myCell.rowSpan) {
-
myCell.style.display = '';
-
}
-
}
-
}
-
}
-
Feel free to use it.
~ninerkz
| | | |
|