473,396 Members | 2,026 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,396 software developers and data experts.

cell merging script

ninerkz
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
Sep 19 '06 #1
1 7914
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
  1. function init() {
  2.     if (!document.getElementsByTagName || !document.getElementById) return;
  3.  
  4.     var x = document.getElementsByTagName('table')[0];
  5.     var xbody = document.getElementById('merge'); //tbody elem (so the thead doesn't get involved)
  6.     var ybody = document.createElement('tbody');
  7.  
  8. // manipulate the tbodies
  9.     var xrows = xbody.getElementsByTagName('tr');
  10.     var numXrows = xrows.length;
  11.  
  12.     var xcells = xbody.getElementsByTagName('td');
  13.     var xcols = xcells.length / xrows.length;
  14.  
  15.     var identical;
  16.     var mergeNum;
  17.  
  18.     for (var i = 0; i < xcols; i++) { // going across
  19.         mergeNum = 1;
  20.         for (var j = 0; j < numXrows; j++) { // going down
  21.             // for each row in the xbody, read the value of the cell
  22.             if (mergeNum <= 1) {
  23.                 var myRow = xbody.getElementsByTagName('tr')[j];
  24.                 var myCell= myRow.getElementsByTagName('td')[i];
  25.                 var myCellTxt = myCell.childNodes[0].data;
  26.             } else {
  27.                 // myRow should be the same as the previous iteration of the loop.
  28.             }
  29.  
  30.             // get the next row, same column
  31.             if (xbody.getElementsByTagName('tr')[j+1]) {
  32.                 var nxtRow = xbody.getElementsByTagName('tr')[j+1];
  33.                 var nxtCell = nxtRow.getElementsByTagName('td')[i];
  34.                 var nxtCellTxt = nxtCell.childNodes[0].data;
  35.             } else {
  36.                 // next row doesn't exist.
  37.             }
  38.  
  39.             // compare the current cell and the next cell
  40.             if (myCellTxt == nxtCellTxt) {
  41.                 identical = true;
  42.                 mergeNum += 1;
  43.                 nxtCell.style.display = 'none';
  44.                 myCell.rowSpan = mergeNum;
  45.             } else { // reset the mergeNum
  46.                 mergeNum = 1;
  47.             }
  48.  
  49.             // account for last row being completely hidden
  50.             if (myCell.style.display == 'none' && myCell.rowSpan) {
  51.                 myCell.style.display = '';
  52.             }
  53.         }
  54.     }
  55. }
  56.  
Feel free to use it.
~ninerkz
Sep 19 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Lan Vuong | last post by:
Hi everyone, I'm trying to retrieve the width of a cell as the table is being dynamically created and add it to a total. I can get the width from an eventhandler such as: <td...
3
by: Peter Williams | last post by:
Hi All, I want to write some javascript for a html page which does the following. Imagine that the page contains a table with 2 columns and 3 rows, e.g.: +---+---+ | A | B | +---+---+
4
by: simon | last post by:
which data control allows me to use a cell doubleClick event? I have time cells (something like Outlook calendar) and when user double click on one cell I would like to redirect to other window...
2
by: Nalaka | last post by:
Hi, How come, there is no "split cell" functionality in vs2005? Makes life a little difficult without this. Nalaka
2
by: Peter | last post by:
Hi, this is the code, and new row and new cell generated ok, but why the onclick and onmouseover doen't work? Thank you in advance! <html> <head> <script language="javascript"> function...
10
by: steve | last post by:
Hi All I would like to be able to change the cell borders on certain cells to none at runtime to make a group of cells appear to be merged I have tried the following in the cellformatting...
2
by: timlitw | last post by:
A while back I researched and wrote a script that uses gd to make merge an arrow on a compass to show wind direction, and another on a speedometer to show wind speed, now I'd like to enhance these...
1
by: gbengston | last post by:
Please look at my example, I have a script that pulls the value of one cell and inserts it into an input box in another cell. My problem is that the source cell is itself an input box so when I pull...
1
by: swetha123 | last post by:
hello , I need to alert the value entered in the text box,which is in TD cell, using DOM can any one tell how please here is the code with out a text box which is working <html> <head> ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
0
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...

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.