Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

hide and show columns in a table

Posted on 2008-06-20
7
468 Views
Last Modified: 2010-04-21
i have a hide/show column working in an example. when col # is entered and show or hide button is clicked it hides/shows the column.
i need to do it for multiple columns. if i click on the "show/hide last 10 columns", it should toggle hide and show. pl help.
<html>
<head>
<script> 
  function show_hide_column(col_no, do_show) {
    var stl;
    if (do_show) stl = 'block'
    else         stl = 'none';
    var tbl  = document.getElementById('id_of_table');
    var rows = tbl.getElementsByTagName('tr');
    for (var row=0; row<rows.length;row++) {
    var cels = rows[row].getElementsByTagName('td')
    cels[col_no].style.display=stl;
    }
  }
 
</script>
</head>
<body>
<table id='id_of_table' border=1>
  <tr>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 2</td>
    <td> two</td>
    <td> deux</td>
    <td> zwei</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 3</td>
    <td> three</td>
    <td> trois</td>
    <td> drei</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 4</td>
    <td> four</td>
    <td>quattre</td>
    <td> vier</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 5</td>
    <td> five</td>
    <td> cinq</td>
    <td>f&uuml;nf</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 6</td>
    <td> six</td>
    <td> six</td>
    <td> sechs</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
</table>
<form>
  Enter column no:
  <input type='text' name=col_no size="3">
  &nbsp;
  <input type='button' onClick='javascript:show_hide_column(col_no.value,  true);' value='show'>
  &nbsp;
  <input type='button' onClick='javascript:show_hide_column(col_no.value, false);' value='hide'>
  <br>
  <br>
  <input type='button' onClick='javascript:show_hide_column(col_no.value, false);' value='show/hide last 10 columns'>
</form>
</body>
</html>

Open in new window

0
Comment
Question by:vidhubala
  • 3
  • 3
7 Comments
 
LVL 11

Expert Comment

by:asafadis
ID: 21833224
Try this:

I haven't tested this... but it should give you some direction!
// Add a 'number_of_columns' parameter
function show_hide_column(do_show, col_no, number_of_columns) {
 
  // If no 'number_of_columns' is provided, it defaults to 1
  if(number_of_columns == null) number_of_columns = 1;
  
  // Start at the 'col_no' column, and keep hiding the columns on the right
  for(var col=0; col<number_of_columns; col++) {
    var tbl  = document.getElementById('id_of_table');
    var rows = tbl.getElementsByTagName('tr');
 
    for (var row=0; row<rows.length; row++) {
      var cels = rows[row].getElementsByTagName('td')
 
      // Make sure the column exists.
      // Say you have 10 columns, you want to hide 4 of them starting at 8. Cannot compute!
      if(cels[col_no + col]) {
 
        // Notice the '+ col'
        cels[col_no + col].style.display = ((do_show) ? 'block' : 'none');
 
      } else {
 
        // Once you hit the limit... just give up!
        return false;
      }
    }
  }
}

Open in new window

0
 
LVL 82

Expert Comment

by:hielo
ID: 21833325
Try this:
<html>
<head>
<script> 
  function show_hide_column(col_no, do_show) {
  
  if( /^\d+$/.test(col_no) )//make sure that col_no is a number
  {
  	return;//otherwise ignore request
  }
    var stl;
    if (do_show) stl = 'block'
    else         stl = 'none';
    var tbl  = document.getElementById('id_of_table');
    var rows = tbl.getElementsByTagName('tr');
    for (var row=0; row<rows.length;row++) {
    	var cels = rows[row].getElementsByTagName('td')
	for( var col=col_no; col < cels.length; ++col) 
    		cels[col].style.display=stl;
    }
  }
 
</script>
</head>
<body>
<table id='id_of_table' border=1>
  <tr>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 2</td>
    <td> two</td>
    <td> deux</td>
    <td> zwei</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 3</td>
    <td> three</td>
    <td> trois</td>
    <td> drei</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 4</td>
    <td> four</td>
    <td>quattre</td>
    <td> vier</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 5</td>
    <td> five</td>
    <td> cinq</td>
    <td>f&uuml;nf</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 6</td>
    <td> six</td>
    <td> six</td>
    <td> sechs</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
</table>
<form>
  Enter column no:
  <input type='text' name=col_no size="3">
   
  <input type='button' onClick='javascript:show_hide_column(col_no.value,  true);' value='show'>
   
  <input type='button' onClick='javascript:show_hide_column(col_no.value, false);' value='hide'>
  <br>
  <br>
  <input type='button' onClick='javascript:show_hide_column(col_no.value, false);' value='show/hide last 10 columns'>
</form>
</body>
</html>

Open in new window

0
 

Author Comment

by:vidhubala
ID: 21833362
hi heilo

it didnt work. also i dont need the textbox input as i already know the value. its last 10 columns.

thanks

<html>
<head>
<script> 
  function show_hide_column(col_no, do_show) {
  
  if( /^\d+$/.test(col_no) )//make sure that col_no is a number
  {
  	return;//otherwise ignore request
  }
    var stl;
    if (do_show) stl = 'block'
    else         stl = 'none';
    var tbl  = document.getElementById('id_of_table');
    var rows = tbl.getElementsByTagName('tr');
    for (var row=0; row<rows.length;row++) {
    	var cels = rows[row].getElementsByTagName('td')
	for( var col=col_no; col < cels.length; ++col) 
    		cels[col].style.display=stl;
    }
  }
 
</script>
</head>
<body>
<table id='id_of_table' border=1>
  <tr>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 2</td>
    <td> two</td>
    <td> deux</td>
    <td> zwei</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 3</td>
    <td> three</td>
    <td> trois</td>
    <td> drei</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 4</td>
    <td> four</td>
    <td>quattre</td>
    <td> vier</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 5</td>
    <td> five</td>
    <td> cinq</td>
    <td>f&uuml;nf</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 6</td>
    <td> six</td>
    <td> six</td>
    <td> sechs</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
</table>
<form>
   <input type='button' onClick='javascript:show_hide_column(10-20, false);' value='show/hide last 10 columns'>
</form>
</body>
</html>

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:vidhubala
ID: 21833376
hi asa,

i tried urs.. its not working. pl take a look.
<html>
<head>
<script> 
  function show_hide_columnOld(col_no, do_show) {
    var stl;
    if (do_show) stl = 'block'
    else         stl = 'none';
    var tbl  = document.getElementById('id_of_table');
    var rows = tbl.getElementsByTagName('tr');
    for (var row=0; row<rows.length;row++) {
    var cels = rows[row].getElementsByTagName('td')
    cels[col_no].style.display=stl;
    }
  }
 
 
 
// Add a 'number_of_columns' parameter
function show_hide_column(do_show, col_no, number_of_columns) {
 
  // If no 'number_of_columns' is provided, it defaults to 1
  if(number_of_columns == null) number_of_columns = 1;
  
  // Start at the 'col_no' column, and keep hiding the columns on the right
  for(var col=0; col<number_of_columns; col++) {
    var tbl  = document.getElementById('id_of_table');
    var rows = tbl.getElementsByTagName('tr');
 
    for (var row=0; row<rows.length; row++) {
      var cels = rows[row].getElementsByTagName('td')
 
      // Make sure the column exists.
      // Say you have 10 columns, you want to hide 4 of them starting at 8. Cannot compute!
      if(cels[col_no + col]) {
 
        // Notice the '+ col'
        cels[col_no + col].style.display = ((do_show) ? 'block' : 'none');
 
      } else {
 
        // Once you hit the limit... just give up!
        return false;
      }
    }
  }
}
</script>
</head>
<body>
<table id='id_of_table' border=1>
  <tr>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 2</td>
    <td> two</td>
    <td> deux</td>
    <td> zwei</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 3</td>
    <td> three</td>
    <td> trois</td>
    <td> drei</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 4</td>
    <td> four</td>
    <td>quattre</td>
    <td> vier</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 5</td>
    <td> five</td>
    <td> cinq</td>
    <td>f&uuml;nf</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
  <tr>
    <td> 6</td>
    <td> six</td>
    <td> six</td>
    <td> sechs</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
    <td> 1</td>
    <td> one</td>
    <td> un</td>
    <td> eins</td>
  </tr>
</table>
<form>
  Enter column no:
  <input type='text' name=col_no size="3">
  &nbsp;
  <input type='text' name=number_of_columns size="3">
  &nbsp;
  <input type='button' onClick='javascript:show_hide_columnOld(col_no.value,  true);' value='show'>
  &nbsp;
  <input type='button' onClick='javascript:show_hide_columnOld(col_no.value, false);' value='hide'>
  <br>
  <br>
  <input type='button' onClick='javascript:show_hide_column(false, col_no.value, number_of_columns.value);' value='show/hide last 10 columns'>
</form>
</body>
</html>

Open in new window

0
 
LVL 82

Accepted Solution

by:
hielo earned 500 total points
ID: 21833494
>>i already know the value. its last 10 columns
then all you need is:

<script> 
 
  function show_hide_column() {
 
    var tbl  = document.getElementById('id_of_table');
    var theRows = tbl.getElementsByTagName('tr');
   
    for (var r=0; r<theRows.length;r++) {
    	var cels = theRows[r].getElementsByTagName('td')
 
	for( var c=10; c < cels.length; ++c) 
    	{
		cels[c].style.display= cels[c].style.display=='none' ? '' : 'none';
	}
    }
  }
 
</script>

Open in new window

0
 

Author Closing Comment

by:vidhubala
ID: 31469241
works fine. thanks.
0
 
LVL 82

Expert Comment

by:hielo
ID: 21833654
You are welcome!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question