473,394 Members | 1,759 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,394 developers and data experts.

Show or Hide Columns in a Table

Frinavale
9,735 Expert Mod 8TB
This code snippet is just a little bit of fun.

It demonstrates how you use JavaScript and CSS to show or hide columns in a table depending on whether or not a checkbox corresponding with the column is checked.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function ShowHideField(header, gridID, checkboxElement) {
  5.     var theGrid = document.getElementById(gridID);
  6.     var displayValue;
  7.  
  8.     if (checkboxElement.checked) {
  9.         displayValue = "";
  10.     } else { 
  11.         displayValue = "none";
  12.     }
  13.  
  14.  
  15.     if (theGrid != null) {
  16.         var theHeaders = theGrid.getElementsByTagName("th");
  17.         var theRows = theGrid.getElementsByTagName("tr");
  18.         var numHeaders = theHeaders.length;
  19.         var numRows = theRows.length;
  20.         var i;
  21.         var foundHeader = false;
  22.         //searching through the headers in the grid for one that matches the header value provided
  23.         for (i = 0; i < numHeaders && foundHeader==false; i++) {
  24.             var headerText = theHeaders[i].innerHTML;
  25.             if (headerText == header) {
  26.             //once found the header, set the header's display value to display or not depending on what was provided
  27.                 foundHeader = true;
  28.                 theHeaders[i].style.display = displayValue;
  29.                 var j;
  30.                 for (j = 0; j < numRows; j++) {
  31.                 //looping through all of the rows in the grid and setting each cell in the column to display or not.
  32.                     var cells = theRows[j].getElementsByTagName("td");
  33.                     if (cells.length > i) {
  34.                         cells[i].style.display = displayValue;
  35.                     }                  
  36.                 }
  37.  
  38.             }
  39.         }        
  40.     }
  41.  
  42. }
  43. </script>
  44. </head>
  45.  
  46. <body>
  47. <form>
  48.  
  49.   <input type="checkbox" onclick="ShowHideField('Field 1', 'grid', this)" id="Field1" checked="true" /><label for="Field1">Field 1</label><br />
  50.   <input type="checkbox" onclick="ShowHideField('Field 2', 'grid', this)" id="Field2" checked="true" /><label for="Field2">Field 2</label><br />
  51.   <input type="checkbox" onclick="ShowHideField('Field 3', 'grid', this)" id="Field3" checked="true" /><label for="Field3">Field 3</label><br />
  52.   <input type="checkbox" onclick="ShowHideField('Field 4', 'grid', this)" id="Field4" checked="true" /><label for="Field4">Field 4</label><br />
  53. <br />
  54.  
  55. <table id="grid">
  56.   <tr>
  57.     <th>Field 1</th>
  58.     <th>Field 2</th>
  59.     <th>Field 3</th>
  60.     <th>Field 4</th>
  61.   </tr>
  62.   <tr>
  63.     <td>1,1</td>
  64.     <td>2,1</td>
  65.     <td>3,1</td>
  66.     <td>4,1</td>
  67.   </tr>
  68.   <tr>
  69.     <td>1,2</td>
  70.     <td>2,2</td>
  71.     <td>3,2</td>
  72.     <td>4,2</td>
  73.   </tr>
  74.   <tr>
  75.     <td>1,3</td>
  76.     <td>2,3</td>
  77.     <td>3,3</td>
  78.     <td>4,3</td>
  79.   </tr>
  80. </table>
  81. </form>
  82. </body>
  83.  
  84. </html>
Happy Coding!

-Frinny
Aug 12 '09 #1
0 8941

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

Similar topics

10
by: oLE | last post by:
I would like to add some javascript to show/hide a certain row of a table. The first row of the table contain the hyperlink that calls the javascript the second row is the one i want to show/hide...
4
by: Tim | last post by:
Hi, I am trying to hide the datagrid row header (the left most column that has the 'select' triangle in it which moves with the selected row). It seems to be fairly simple;...
12
by: Jerad Rose | last post by:
I searched for a while trying to find the answer to this, but to no avail. I am trying to find the best way (or any way) to dynamically show and hide groups of TR's. For example, I have a...
1
by: lydia sista via DotNetMonster.com | last post by:
hi! help... How do you show/hide item's data? Like sale item on product catalogue. I want to show the sale image when the status of product is on sale. in my catalogue I use datalist:...
2
by: jeet_sen | last post by:
Hi, I have created a table colelcting data from an XML source. After I built the whole table I ahve given user options to filter out columns from the table. For this I have collected all the...
1
by: pamate | last post by:
hi, I want to show hide layers. I am able to show and hide layers but i am facing problem that, cant view the cursor in Mozilla,but i can type in input text box, its overlapping the layers. ...
18
by: Liquidtouch | last post by:
I have been searching on this for awhile and cant find anything and playing around with it got me no where. I will start with what I am after and then explain what I have. I have a table with 3...
13
by: rupak | last post by:
Hi! I have a multiple selection of checkboxes <input type='checkbox' name='coffee' id='col1' value='Address'/> <input type='checkbox' name='coffee' id='col2' value='Name'/> <input...
1
oranoos3000
by: oranoos3000 | last post by:
hi would you please help me i have a online shopping center that i show pictures of the my product in home page. in the InterExplorer pictures is shown correctly but in Firefox browser is shown...
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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
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
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.