How to expand and collapse multiple rows in table using Java script

Hi Friends,

How can you collapse and expand multiple rows dynamically. Variables should not be hard coded.
Can some one explain me with example?

I have a table in following way.

+Title 1
+Title 2
+Title 3
+Title 4

Now on clicking Title 1 i should see the following
- Title 1
--Sub Row1
--Sub Row 2

now on clicking Title 2 i should see the following
- Title 2
--Sub Row 1
--Sub Row 2
--Sub Row 3
--Sub Row 4

now on clicking Title 3 i should see the following
- Title 3
--Sub Row 1
--Sub Row 2

now on clicking Title 4 i should see the following
- Title 4
--Sub Row 1
--Sub Row 2
 
kanguyenAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

contactkarthiCommented:
0
gambit_642Commented:
contactkarthi:
That is to dynamically hide or show existing predefined rows.

It sounds like he wants to dynamically create the rows on a click event:

2 Flavers (DOM and innerHTML)
http://www.oreillynet.com/pub/a/javascript/2003/05/06/dannygoodman.html
0
gambit_642Commented:
And this one is a bit easier but only using DOM
http://www.howtocreate.co.uk/tutorials/javascript/domtables
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

kanguyenAuthor Commented:
sorry guys, that's is not what I look for.
from the example I had given above,
i.e the data already populated into the table with sub rows collapse where row ids are the same, now what I'd like is when I click on the plus sign the sub rows will expand under that category eg Title1 and the plus sign will become minus sign.

can the experts give example code please.

Thanks in advance.
0
gambit_642Commented:
Here is a quick example I through together using innerHTML

Typically, your tables aren't stored in js variables, but this is just an example:
Use Ajax to retrieve the table and values, then js to change the +/- button and insert the table into your visible HTML.
<head>
<script language="javascript" type="text/javascript">
function toggle_visablity(object, button)
{
	object=document.getElementById(object);
	button=document.getElementById(button);
	
	if(button.innerHTML == '+')
		{button.innerHTML = '-';}
	else
		{button.innerHTML = '+';}
	
	var table =	'<table border="1" bordercolor="blue" cellspacing="0" width="300">'+
					'<tr>'+
						'<td>Hi, Here is a row</td>'+
					'</tr><tr>'+
						'<td>Here is another 1</td>'+
					'</tr>'+
				'</table>';
		
		if(object.innerHTML == '')
			{object.innerHTML = table;}
		else
			{object.innerHTML = '';}
 
}
</script>
</head>
<body>
<span onMouseOver="this.style.cursor='pointer'" id="toggle1" onClick="toggle_visablity('table1', 'toggle1');">+</span> Expand this table
<div id="table1"></div>
<span onMouseOver="this.style.cursor='pointer'" id="toggle2" onClick="toggle_visablity('table2', 'toggle2');">+</span> Expand Second Table
<div id="table2"></div>
 
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Computer101Commented:
Forced accept.

Computer101
EE Admin
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java EE

From novice to tech pro — start learning today.