Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

hide and show columns in a table

Posted on 2008-06-20
7
Medium Priority
?
487 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…
Suggested Courses

772 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