Solved

hide and show columns in a table

Posted on 2008-06-20
7
463 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
jquery, dropdown 4 38
Compare site how does it work 4 52
Re-position the objects 7 51
Jquery autocomplete 10 27
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-…
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…
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…

762 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

16 Experts available now in Live!

Get 1:1 Help Now