Question

SHOW/HIDE TABLE ROWS

Asked by: archrajan

Hi..

I have been working on this lately and not quite successful..

I have a table (contents inside are generated by jsp)

at a time users can see  5 rows
when the click the + button they can see all available rows
and the + button changes to a - button.. now when they click the - button they shud see only 5 rows
and when they refresh the page the state shud be maintained..

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2005-01-07 at 10:10:34ID21265898
Tags

hide

,

table

,

row

,

show

Topic

JavaScript

Participating Experts
5
Points
500
Comments
23

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Hide a Row
    Dear friends, Is it possible to hide a row in Data Grid.. Regards.. Raj
  2. Hiding Rows not in Selection
    In Excel. I have two sheets: Summary and Detail Summary sheet contains various Hyperlinks that when clicked by the user, links and highlights a selection of rows on the Detail sheet. Question(s): How do I hide all other rows(rows not highlighted) on the Detail sheet afte...
  3. Hide Row
    when cell e1 has the following formula "=(b1*18)+(c1*22)" and results in "0", I want to hide the entire row. I have been struggling with this for about a week. I am no expert with VB, and can't figure this out. Any help would be greatly appreciated.
  4. Hiding Rows ( Group )
    Hi, This is my first question . I have a group of rows, which are used for calculation only and not display. I would like to add a macro to hide rows in-group. Can someone guide me please? Regards, Narendar
  5. HIDE SHOW ROWS
    Can some one give me an example with cookies to hide / show table rows and maintain them with page refresh
  6. How to maintain the same number of rows in a pivot table a…
    Hi, I have a pivot table which is used to query an external database. The table is updated daily. I am using excel as a reporting tool and therefore I need to access the output from the pivot table. I use the pivot table to group and sort the data. The problem is this: w...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: justinbilligPosted on 2005-01-07 at 10:20:28ID: 12986200

<script language="javascript">

function ToggleRowDisplay( objTargetButton )
{
      try
      {
            // Variables
            var strDisplay = "block";
            var objTableBody = document.getElementById( "idTableBody" );
            var strButtonValue = "-";
      
            // Toggle Display
            if( objTargetButton.value == "-" )
            {
                  // Hide
                  strDisplay = "none";
                  strButtonValue = "+";
            }
      
            // Toggle the table
            objTableBody.style.display = strDisplay;
            
            // Set button Value
            objTargetButton.value = strButtonValue;
      }
      catch( expError )
      {
            alert( expError.number + "   " + expError.description );
      }
}


</script>

<html>
      <body>
            <table border="0">
                  <thead>
                        <tr>
                              <th>Can Always See Me
                        </tr>
                        <tr>
                              <th>Can Always See Me
                        </tr>
                        <tr>
                              <th>Can Always See Me
                        </tr>
                        <tr>
                              <th>Can Always See Me
                        </tr>
                        <tr>
                              <th>Can Always See Me
                        </tr>
                  </thead>
                  
                  <tbody id="idTableBody" style="display:none;">
                        <tr>
                              <td>Can Sometimes See Me</td>
                        </tr>
                        <tr>
                              <td>Can Sometimes See Me</td>
                        </tr>
                        <tr>
                              <td>Can Sometimes See Me</td>
                        </tr>
                        <tr>
                              <td>Can Sometimes See Me</td>
                        </tr>
                        <tr>
                              <td>Can Sometimes See Me</td>
                        </tr>
                  </tbody>
            </table>

            <input type="button" onclick="ToggleRowDisplay( this )" value="+" name="btnToggle" style="width:20;">
      </body>
</html>
                        

 

by: justinbilligPosted on 2005-01-07 at 10:23:39ID: 12986233

as far as keeping state upon refresh you would need a cookie

take a look at the answer in this question

http://experts-exchange.com/Web/Web_Languages/JavaScript/Q_21254146.html


every time hte user clicked you would need to store the state the user was in, whenever the page loads, just check for hte existance of that cookie, whatever value is in it would tell you how to default your display

something like

<body onload="GetDisplayFromCookie( );">

then in get DisplayFromCookie( )

document.getElementById( "idTableBody" ).style.display = whatevervalueisincookie;

 

by: archrajanPosted on 2005-01-07 at 10:32:36ID: 12986323

i do not wish to use cookies? any other way??

 

by: justinbilligPosted on 2005-01-07 at 10:42:41ID: 12986405

Upon refresh ( if the user presses the f5 key or hits refresh ) i'm going to have to say no, the only possible way i could think is everytime the user clicks the button submit a form ( either the current page, or a hidden iframe ) and store the state of the table viewing a database. Sorry

 

by: ZvonkoPosted on 2005-01-07 at 10:45:20ID: 12986436

Aditional way is to reload the page with the current state in the URL as query string and read the query string from location.search property.

 

by: arantiusPosted on 2005-01-07 at 10:45:48ID: 12986440

Online demo: http://www.arantius.com/ee/Q_21265898.html

<html>
<head>
<script>

//cookie functions from http://www.webmasterbase.com/static/popjs.php
function getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg)
return getCookieVal (j);
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0)
break;
}
return null;
}

function SetCookie (name, value) {
var argv = SetCookie.arguments;
var argc = SetCookie.arguments.length;
var expires = (2 < argc) ? argv[2] : null;
var path = (3 < argc) ? argv[3] : null;
var domain = (4 < argc) ? argv[4] : null;
var secure = (5 < argc) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");
}

var rowShow=(document.all?'block':'table-row');

function toggleRows(prefix) {
    var el,i,state;
    for (i=0; el=document.getElementById(prefix+i); i++) {
        state=('none'==el.style.display?1:0);
        el.style.display=(state?rowShow:'none');
    }
    SetCookie('rowShowState'+prefix, state);
}

function setFromState(prefix) {
    var el,i,state=GetCookie('rowShowState'+prefix)*1;
    for (i=0; el=document.getElementById(prefix+i); i++) {
        el.style.display=(state?rowShow:'none');
    }
}
</script>
</head>
<body onload="javascript:setFromState('hideRow');">

<button onclick="javascript:toggleRows('hideRow');">Toggle rows</button><br>
<br>
<table>
    <tr><td>1</td><td>2</td><td>3</td></tr>
    <tr id="hideRow0"><td>4</td><td>5</td><td>6</td></tr>
    <tr id="hideRow1"><td>7</td><td>8</td><td>9</td></tr>
    <tr><td>10</td><td>11</td><td>12</td></tr>
</table>

<p>This script allows you to toggle groups of table rows on and off, and it will save state in a cookie.</p>

</body>
</html>

 

by: justinbilligPosted on 2005-01-07 at 10:56:05ID: 12986545

ha way to post almost hte exact same thing arantius


Zvonko would that work upon a refresh? how would you know to reload the page, i thought of that but with the onunload event, but what if the user is closing the browser

 

by: archrajanPosted on 2005-01-07 at 11:01:43ID: 12986611

I cannot give id's to each tr tags...

 

by: justinbilligPosted on 2005-01-07 at 11:03:00ID: 12986624

archrajan, i'm thinkin without cookies you wont be able to save state upon refresh, as far as my knowledge, maybe someone else can come up with something that i havent suggested

 

by: arantiusPosted on 2005-01-07 at 11:12:05ID: 12986723

So it took me 15 minutes of typing, is that a crime?  Were no posts when I started working.  Let's not fight.

Regarding saving the state, yes there's two ways.  Cookies and URL like Zvonko mentioned.

Archrajan if you do not give IDs, how do you separate the rows that should be hidden from the rows that should not?

 

by: archrajanPosted on 2005-01-07 at 11:16:58ID: 12986788

Anyway using DOM???

I shall post the code that i have.. just a moment

 

by: justinbilligPosted on 2005-01-07 at 11:24:58ID: 12986878

arantius, i was just joshin you man =)

also i dont see how you can store information in the URL upon refresh?

 

by: ZvonkoPosted on 2005-01-07 at 12:22:42ID: 12987549

Here is the example from justinbillig extended to refresh location parameters:


<html>
<head>
<script language="javascript">

function ToggleRowDisplay( objTargetButton ){
   var tabPar;
   var searchPar = location.search.replace(/(\?|\&)tbody\=[^\&]*/i,"");
   if(searchPar>""){
     tabPar = "&tbody=";
   } else {
     tabPar = "?tbody=";
   }
   if(objTargetButton.value=="+"){
     tabPar += "expand";
   } else {
     tabPar += "collapse";
   }
   location = location.href.split("?")[0]+searchPar+tabPar;
}
function init(){
  if(location.search){
    var tabPar = location.search.split("tbody=");
    if(tabPar.length>1){
      tabPar = (tabPar[1]+"&").split("&")[0];
      var tabBtn = document.forms[0].btnToggle;
      var tabBdy = document.getElementById("idTableBody");
      if(tabPar.toLowerCase()=="expand"){
        tabBtn.value="-";
        tabBdy.style.display="block";
      }
    }
  }
}
</script>
</head>
     <body onLoad="init()">
       <form>
          <table border="0">
               <thead>
                    <tr>
                         <th>Can Always See Me
                    </tr>
                    <tr>
                         <th>Can Always See Me
                    </tr>
                    <tr>
                         <th>Can Always See Me
                    </tr>
                    <tr>
                         <th>Can Always See Me
                    </tr>
                    <tr>
                         <th>Can Always See Me
                    </tr>
               </thead>
               
               <tbody id="idTableBody" style="display:none;">
                    <tr>
                         <td>Can Sometimes See Me</td>
                    </tr>
                    <tr>
                         <td>Can Sometimes See Me</td>
                    </tr>
                    <tr>
                         <td>Can Sometimes See Me</td>
                    </tr>
                    <tr>
                         <td>Can Sometimes See Me</td>
                    </tr>
                    <tr>
                         <td>Can Sometimes See Me</td>
                    </tr>
               </tbody>
          </table>

          <input type="button" onclick="ToggleRowDisplay( this )" value="+" name="btnToggle" style="width:20;">
          </form>
     </body>
</html>
             

 

by: raj3060Posted on 2005-01-09 at 11:13:23ID: 12997944

Simple and working:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<script>
function toggle(){
if(document.getElementById("tog1").innerHTML=="+"){
document.getElementById("tog1").innerHTML="-"
document.getElementById("tog2").style.display="block"
}
else if(document.getElementById("tog1").innerHTML=="-"){
document.getElementById("tog1").innerHTML="+"
document.getElementById("tog2").style.display="none"
}else document.getElementById("tog2").style.display="none"
}
</script>
<title>show hide row</title>
</head>
<body>
<form name="form1">
<table>
<tbody>
<tr>
<td id="tog1" style="cursor:hand;text-color:#EEEEFF;" onclick="toggle()">+</td><td>Row1</td>
</tr>
<tr>
<td></td><td>Row2</td>
</tr>
<tr>
<td></td><td>Row3</td>
</tr>
<tr>
<td></td><td>Row4</td>
</tr>
<tr>
<td></td><td>Row5</td>
</tr>
</tbody>
<tbody id="tog2" style="display:none">
<tr>
<td></td><td>Row6</td>
</tr>
<tr>
<td></td><td>Row7</td>
</tr>
</tbody>
</table></form>
</body>
</html>

 

by: archrajanPosted on 2005-01-09 at 11:29:08ID: 12997994

raj.. though we will lose the effect whn we refresh right?

 

by: ZvonkoPosted on 2005-01-09 at 12:55:44ID: 12998292

Did you check my URL parameter version?

 

by: archrajanPosted on 2005-01-09 at 13:04:04ID: 12998319

Zvonko i did check that...
but i am looking for more simpler version..
i will just let u know..
thanks a ton...

 

by: archrajanPosted on 2005-01-09 at 13:05:43ID: 12998329

zvonko the problem with that script is that if sometimes the user goes to a different page and comes back its lost right?

 

by: ZvonkoPosted on 2005-01-09 at 13:48:37ID: 12998528

If the user does close the window and open again by url, then of course are the url parameters lost.
But if he toggles several times the table, goes to his homepage and returns back by backspace button, then is the url parameter still there and the table state is restored.

Thanks for testing and for points.

 

by: ZvonkoPosted on 2005-01-09 at 13:54:56ID: 12998557

Oh, and if your question was: how to restore some state on a page?
Then you have three options: cookies, url parameters and hidden fields from server.
But the hidden fields are quiet more complicated then cookies or url parameters.
Because you have to send EVERY toggle action to server to maintain the state on server in some database field or Session var or text file without page reload.
I have done that with javascript Image() objects, but that does only complicate the scenario and makes it more errornous.

 

by: slwaPosted on 2005-05-10 at 22:09:47ID: 13975137

Hi..

I have a table (contents inside are generated by jsp)

at any time users can click any heading to view the available rows relating to that heading.

when the click the + button they can see all available rows
and the + button changes to a - button.. now when they click the - button they shud see only the headings no data rows shown.

and when they refresh the page the state shud be maintained..

 

by: raj3060Posted on 2005-05-11 at 06:36:47ID: 13977628

slwa,
You need to open a new question. This question is closed and someone else asked this.

 

by: slwaPosted on 2005-05-14 at 00:05:48ID: 14001464

sorry, raj3060 i'm already openned new question, i do this by mistake....thanks

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...