Current location: Hot Scripts Forums » General Web Coding » JavaScript » Help : show/hide table


Help : show/hide table

Reply
  #1 (permalink)  
Old 12-27-04, 11:05 PM
k4nth k4nth is offline
New Member
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Help : show/hide table

Hey guys, i want to know how to do show/hide table like in vbulletin with cookie support. so it remembers when you come back or refresh page. i know it can be done using javascript but i haven't got a clue. this is what i have done so far.

<script language="JavaScript" type="text/JavaScript">

function showit(box)
{
document.getElementById(box).style.display="block" ;
}

function closeit(box)
{
document.getElementById(box).style.display="none";
}

</script>


<div id="div1">
some stuff here
</div>


<a href="#top" onclick="closeit('div1');">Hide</a>
<a href="#top" onclick="showit('div1');">Show</a>

instead of the text (Hide/Show) i want to place an image which changes from plus to minus when you click. hope someone can help me out. thanking you in advance.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 12-28-04, 06:07 PM
4thorder's Avatar
4thorder 4thorder is offline
Newbie Coder
 
Join Date: Dec 2004
Location: USA MI
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Please add some detail

I understand the basic requirement but this is totally dependent on layout. This is due to the fact that the show hide icon needs to be always present. Therefore, this will effect layout.

Likewise, Hiding or showing elements can be tricky if you dont have the exact layout down. This is due to "inline" style. It "removes" the content from display consideration which means that the HTML for the page changes temperarily until you stick it back in there. This is not the case with hiding "block" but then you have big gapping spaces where the HTML is but just not visible.

You mention that you want it to be like the vbulletin show/hide. What does this mean? Where is this feature?

For example are you going to have rows that are hidden or shown wihtin a given table?

Give me an example of the layout (HTML) that you require AND where the content will reside within that layout and we will see what can be done.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 12-28-04, 08:43 PM
k4nth k4nth is offline
New Member
 
Join Date: Dec 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
if you look below this post, there is a section called 'Similar Threads'. i want to do the same thing. hide the content by clicking on the + and - buttons.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 12-29-04, 12:13 AM
4thorder's Avatar
4thorder 4thorder is offline
Newbie Coder
 
Join Date: Dec 2004
Location: USA MI
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
Post opps ok thanks

Well I have an answer for you but I havent taken the time to make it crossbrowser compatible - so IE only - youll have to retrofit the netscape stuff in yourself.

This will work for as many tables that you have on the page AS LONG AS you follow the basic TABLE HTML structure that I have:

<TABLE>
<TR>
<TH width="95%"></TH>
<TH width="5%"></TH>
</TR>
<TR><TD>CONTENT</TD></TR>
</TABLE>

Script also allows for tables inside of tables as long as you place the internal table where the content should go for the outer table.

Finally, you can set the page to auto expand all or auto collapse all when the page initally loads.

here is an example: http://www.4thorder.us/Scripts/ONDEC...seTableRow.htm

Code:
<HTML>
<HEAD>
<SCRIPT>
// :::::::::::::::::::::::::
// :::: Global Variables :::
// :::::::::::::::::::::::::
var firstLoad=0
var GlobalECState=0 // 0=collapsed, 1=expanded; used for inital state of ALL branches
// :::::::::::::::::::::::::
// :::: Global Functions :::
// :::::::::::::::::::::::::
function InitializePage()
{Icons(); attachEventhandlers();}
// Attach event handlers to all images within container
function attachEventhandlers()
{
IMGCollection=document.getElementsByTagName("IMG");
if (IMGCollection!=null)
{ for (l=0; l<IMGCollection.length; l++)
{IMGCollection[l].onmouseup=onMouseUpHandler;}
}
}
function Icons()
{
// Collect all tables
TABLECollection=document.getElementsByTagName("TABLE");
// Loop through and insert icon in all TH elements
if (TABLECollection!=null)
{for (i=0; i<TABLECollection.length; i++)
{ 
// Grab second TH element and second TR element 
THCollection=TABLECollection[i].getElementsByTagName("TH");
THElement=THCollection[1];
TRCollection=TABLECollection[i].getElementsByTagName("TR");
TRElement=TRCollection[1]; 
 
if(firstLoad==0)
 {// Yes: FIRST LOAD OF PAGE -- insert image
  if(GlobalECState==0)
  {// Global ECState is COLLAPSED (+) (0) 
  THElement.innerHTML="<IMG border='0' src='+.gif' width='13' height='13'>" 
 
  IMGCollection=TABLECollection[i].getElementsByTagName("IMG");
  IMGCollection[0].setAttribute("ECState",0);
 
  TRElement.style.display='none';
  }
  else
  {// Global ECState is EXPANDED (-) (1)
  THElement.innerHTML="<IMG border='0' src='-.gif' width='13' height='13'>"
 
  IMGCollection=TABLECollection[i].getElementsByTagName("IMG");
  IMGCollection[0].setAttribute("ECState",1);
 
  TRElement.style.display='inline'; 
  }
 }
else
 {// No: FIRST LOAD OF PAGE - change image
  // Grab ECState of image and expand or collapse branch
 IMGCollection=TABLECollection[i].getElementsByTagName("IMG");
 State=IMGCollection[0].getAttribute("ECState");
 if(State==0)
  {// ECState is COLLAPSED (+) (0)
  // Change Image
  IMGCollection[0].setAttribute("src","+.gif");
  // Hide DIV
  TRElement.style.display='none';
  }
 else
  {// ECState is EXPANDED (-) (1)
  // Change Image
  IMGCollection[0].setAttribute("src","-.gif");
  // Show DIV
  TRElement.style.display='inline';
  }
 }
}
}
if(firstLoad==0){firstLoad=1;}
}
// ::::::::::::::::::::::::
// :::: Event Handlers ::::
// ::::::::::::::::::::::::
function onMouseUpHandler()
{
if(window.event.button==1)
{
// Grab Target Element
  imgObj=window.event.srcElement;
 
// Toggle ECState
State=imgObj.getAttribute("ECState");
if(State==0)
{imgObj.setAttribute("ECState",1);}
else{imgObj.setAttribute("ECState",0);}
}
Icons();
}
</SCRIPT> 
</HEAD>
<BODY onload="InitializePage()">
<TABLE border="1" cellpadding="2" style="border-collapse: collapse" width="100%">
 <TR>
  <TH width="95%">EXAMPLE OF ONE TABLE INSIDE OTHER</TH>
  <TH width="5%"></TH>
 </TR>
 <TR>
  <TD colspan="2">
   <TABLE border="1" cellpadding="2" style="border-collapse: collapse" width="100%">
 <TR>
  <TH width="95%">EXAMPLE OF SIMPLE LAYOUT</TH>
  <TH width="5%"></TH>
 </TR>
 <TR>
  <TD colspan="2">EXAMPLE OF SIMPLE LAYOUT</TD>
 </TR>
   </TABLE>
  </TD>
 </TR>
</TABLE>
<BR>
<TABLE border="1" cellpadding="2" style="border-collapse: collapse" width="100%">
 <TR>
  <TH width="95%"></TH>
  <TH width="5%"></TH>
 </TR>
 <TR>
  <TD colspan="2">CONTENT HERE</TD>
 </TR>
</TABLE>
 
</BODY>
</HTML>
here are links to the + - image go ahead and download them - I made these as well

http://www.4thorder.us/Scripts/ONDEC...tableRow/-.gif
http://www.4thorder.us/Scripts/ONDEC...tableRow/+.gif

Happy coding
http://www.4thorder.us/Scripts

Last edited by 4thorder; 12-29-04 at 01:07 AM. Reason: added example
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 01-10-05, 10:30 AM
4thorder's Avatar
4thorder 4thorder is offline
Newbie Coder
 
Join Date: Dec 2004
Location: USA MI
Posts: 37
Thanks: 0
Thanked 0 Times in 0 Posts
DEMO moved

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Auto Dealer Script nuzzle PHP 17 04-14-10 09:34 PM
Problem with a sort table js function tdubyou JavaScript 0 05-03-04 10:19 AM
auto table resize derick_2k JavaScript 4 04-26-04 03:32 PM
need help in sorting table niyax JavaScript 1 03-24-04 04:39 AM
Newbie MySQL fccolon PHP 2 03-16-04 11:54 AM


All times are GMT -5. The time now is 01:08 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.