User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 422,420 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 5,199 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 4186 | Replies: 3
Reply
Join Date: Jun 2008
Posts: 1
Reputation: l_kris06 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
l_kris06 l_kris06 is offline Offline
Newbie Poster

collapse/expand table row

  #1  
Jun 4th, 2008
Hi All,

I have a PHP application, that displays information in a table format, each rows contains specifica information which are dynamically fetched from the MySQL database. i am looking for a way to make these individual rows display more information, meaning i want to implement a functionality where in when i click on a row it expands to show more information, and when i click on the row again, it should collapse. I knw its possible in Javascript, but am very new to it, and i dont knw how to go about doing it. I would really appreiciate if somebody could give help me out. Thanks.

~Kris
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: collapse/expand table row

  #2  
Jun 5th, 2008
Originally Posted by l_kris06 View Post
Hi All,

I have a PHP application, that displays information in a table format, each rows contains specifica information which are dynamically fetched from the MySQL database. i am looking for a way to make these individual rows display more information, meaning i want to implement a functionality where in when i click on a row it expands to show more information, and when i click on the row again, it should collapse. I knw its possible in Javascript, but am very new to it, and i dont knw how to go about doing it. I would really appreiciate if somebody could give help me out. Thanks.

~Kris


You'll need to use the DOM methods:
http://www.howtocreate.co.uk/tutoria...ript/dombasics

The simplest thing to do is to have the markup in the HTML but hide it using the style property or CSS. Then to expand it, just display it and vise versa.

eg:

<a href="#" id="expander">Expand Me</a>
<div id="expandable" style="display:none;">This is some text that is hidden</div>

Then you have the JS:

// get a reference to the expander element
var expander = document.getElementById('expander');
// set an event handler to handle when the expander link is clicked
expandar.onclick = function() {
  // get a reference to the expandable div
  var expandable = document.getElementById('expandable');
  // display it if it isn't visible, or hide it 
  if (expandable.style.display = 'none') {
    expandable.style.display = 'block';
  } else {
    expandable.style.display = 'none';
  }
};

You can do the same for table rows. You just need to select them with methods such as document.getElementById() or document.getElementsByTagName() etc,. then change their style properties or an attribute such as "class" in order to apply a different CSS rule to the element. This gives the effect of the expand collapse etc..
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,851
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 344
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Lazy, Useless & Apathetic

Re: collapse/expand table row

  #3  
Jun 5th, 2008
A typo there:
if (expandable.style.display = 'none') {
should be:
if (expandable.style.display == 'none') {
I don't accept change. I don't deserve to live.

Happiness corrupts people.

Failing to value the lives of others cheapens your own.
Reply With Quote  
Join Date: Sep 2005
Posts: 689
Reputation: digital-ether has a spectacular aura about digital-ether has a spectacular aura about 
Rep Power: 6
Solved Threads: 41
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Practically a Master Poster

Re: collapse/expand table row

  #4  
Jun 6th, 2008
Originally Posted by ~s.o.s~ View Post
A typo there:
if (expandable.style.display = 'none') {
should be:
if (expandable.style.display == 'none') {


Thanks for spotting that.

I didn't mention in my post, but the javascript should be executed after the DOM is loaded. This is normally implied if you're familiar with DOM scripting but it I can remember times when I'd spend hours trying to figure out what was going wrong and the case was that the DOM was not fully loaded. So here is what a full working example would look like.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
		<title>Expand a Table via DOM and Style property</title>
		<script type="text/javascript">
			window.onload = function() {
				var table = document.getElementById('expandable_table');
				if (table) {
					var trs = table.getElementsByTagName('tr');
					for(var i = 0; i < trs.length; i++) {
						var a = trs[i].getElementsByTagName('td')[0].getElementsByTagName('a')[0];
						a.onclick = function() {
							var span = this.parentNode.getElementsByTagName('span')[0];
							span.style.display = span.style.display == 'none' ? 'block' : 'none';
							this.firstChild.nodeValue = span.style.display == 'none' ? 'More' : 'Less';
						};
					}
				}
			};
		</script>
	</head>
	<body>
		
		<h2>Expandable Table</h2>
		<table id="expandable_table">
			<tbody>
				<tr><td>TD 1 <a href="#">More</a><span style="display:none;">TD 1 Extended Description</span></td></tr>
				<tr><td>TD 2 <a href="#">More</a><span style="display:none;">TD 2 Extended Description</span></td></tr>
				<tr><td>TD 3 <a href="#">More</a><span style="display:none;">TD 3 Extended Description</span></td></tr>
				<tr><td>TD 4 <a href="#">More</a><span style="display:none;">TD 4 Extended Description</span></td></tr>
			</tbody>
		</table>
		
	</body>
</html>
Last edited by digital-ether : Jun 6th, 2008 at 11:05 am.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 9:38 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC