Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Hide/Show Jsp generated tables using Javascript? (screenshot) 1

Status
Not open for further replies.

rudy23

Programmer
Feb 12, 2004
31
US
Hi Guys. Am relatively new to Javascript. Below is a screenshot of what my Client wants.


These are 3 Individual reports. The have been created by using JSP’s which query the database and generate these reports. What he wants is for reports to collapse/expand when the user clicks on the Hide Details Button using Javascript without repainting the page. Can this be done as the JSP’s themselves are pretty complex and resource intensive. Will the Javascript have to Envelope the JSP code ? I guess so.Is there any good robust javascript solution or should I just stick to repainting the whole page when he clicks the Hide Details Link. I wouldn’t mind buying a script if need be.

The data for all reports is available when the page loads and hence we wont have to make any database calls to Hide/Show.

Any guidance, Help reference greatly appreciated.
 
find the area that has the information in it, give it an id
eg: myArea

and use

document.getElementById("myArea").style.display='none';

hth

simon
 
You can use CSS and some simple Javascript to do this with ease.

Wrap the data you want to show/hide in a <div> (say) and give that div an id...

Code:
<div id="tableData1">.....</div>

You can now target that div using it's unique id... put this on the show/hide text:

Code:
<a href="javascript:showHide('tableData1');">Show/Hide</a>

And then write a simple javascript function like this:

Code:
function showHide(_myDiv)
{
  var myDivToChange = document.getElementById(_myDiv);
  if (myDivToChange.style.display == 'block')
  {
    myDivToChange.style.display = 'none';
  } else {
    myDivToChange.style.display = 'block';
  }
}

Now... this code has no attempt to manage error checking etc... but it ought to do exactly what you are after.

Any questions, specific follow-ups etc... post here and I'm sure we can help you.

Jeff

 
Hey Jeffy that works just the way I wanted it to. Cant believe the solution could be this simple. Thanks a lot man.

Are there any scenarios where I have to take extra precautions or soemthing like that cos u said. " this code has no attempt to manage error checking etc..."

Also the <div> tags can envelope only a table right. not a <tr> right?

Let me know

Thanks again
 
Here is a working test harness showing this working - it works fine in IE 5.17 for the Mac (if that's any help) *grin*

Code:
<html>
<head>
<title>Tek-Tips Rock</title>
<script type="text/javascript">
function showHide(_myDivName)
{
  var myObj = document.getElementById(_myDivName);
  myObj.style.display = myObj.style.display == 'none' ? 'block' : 'none';
}
</script>
</head>
<body>

<table border="1" width="300" cellpadding="0" cellspacing="0">
  <tr><th><a href="javascript://;" onclick="this.innerHTML=this.innerHTML=='Hide'?'<em>Show</em>':'Hide';showHide('myDataDiv_1');">Hide</a></th></tr>
  <tr><td>Header</td></tr>
  <div id="myDataDiv_1" style="display:block;">
    <tr><td>Line 1</td></tr>
    <tr><td>Line 2</td></tr>
    <tr><td>Line 3</td></tr>
  </div>
<tr><td>Footer</td></tr>
</table>

<table border="1" width="300" cellpadding="0" cellspacing="0">
  <tr><th><a href="javascript://;" onclick="this.innerHTML=this.innerHTML=='Hide'?'<em>Show</em>':'Hide';showHide('myDataDiv_2');">Hide</a></th></tr>
  <tr><td>Header</td></tr>
  <div id="myDataDiv_2" style="display:block;">
    <tr><td>Line 1</td></tr>
    <tr><td>Line 2</td></tr>
    <tr><td>Line 3</td></tr>
  </div>
  <tr><td>Footer</td></tr>
</table>

</body>
</html>

Cheers,
Jeff
 
Thanks man.

But other than changing show to hide and hide to show the code doesnt seem to work. U sure u pasted the right one.
 
Once I removed the weird invisible characters from the copy-paste, it worked fine in my copy of IE at home. I'll do the same from work on a Windows IE tomorrow morning and let you know the result... hopefully that's all there is to it.

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top