Solved

hide and show columns in a table

Posted on 2008-06-20
7
464 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

867 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now