473,786 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic Collapsible Tables for any number of tables

1 New Member
I'm newbie to javascript and I did an extensive search on this site and couple of others looking for directions.
I'm attempting to write some custom javascript for collapsing the tables and fit it in an existing application already provided. I found some code on the web that works great. In this example there are 3 fixed set of tables with 3 fixed/hardcoded "id".
I tried to fit this code into a FOR loop so it becomes dynamic and it would work for any number and not just 3 tables.

Expand|Select|Wrap|Line Numbers
  1. FOR (f, f>0, f++)
  2. <td style="background-color: #CCC"><a href="#" onclick="return toggleItem('collapse_myTbody1[f]')">Toggle 1</a></td></tr>
  3.  
But it is not working.
Has anyone done this before. Can anyone share some ideas or pointers?

Regards,
MHT
NY City.


[HTML]<html>
<head>
<title>My Doc</title>
<script language="javas cript">
function getItem(id)
{
var itm = false;
if(document.get ElementById)
itm = document.getEle mentById(id);
else if(document.all )
itm = document.all[id];
else if(document.lay ers)
itm = document.layers[id];

return itm;
}

function toggleAll(dowha t)
{
var tags = document.getEle mentsByTagName( 'tbody');
if(!tags)
return false;

for(var i = 0; i < tags.length; i++)
{
if(tags[i].className == 'collapse_obj')
{
if(dowhat == 'collapse')
tags[i].style.display = 'none';
else
tags[i].style.display = '';
}
}

return false;
}

function toggleItem(id)
{
itm = getItem(id);

if(!itm)
return false;

if(itm.style.di splay == 'none')
itm.style.displ ay = '';
else
itm.style.displ ay = 'none';

return false;
}
</script>
</head>
<body>

<a href="#" onclick="return toggleAll('coll apse')">Collaps e All</a>
<a href="#" onclick="return toggleAll('expa nd')">Expand All</a>

<table width="400">
<tbody>
<tr><td style="backgrou nd-color: #CCC"><a href="#" onclick="return toggleItem('col lapse_myTbody1' )">Toggle 1</a></td></tr>
</tbody>

<tbody class="collapse _obj" id="collapse_my Tbody1">
<tr><td>Test row1</td></tr>
<tr><td>Test row2</td></tr>
<tr><td>Test row3</td></tr>
<tr><td>Test row4</td></tr>
<tr><td>Test row5</td></tr>
</tbody>
</table>

<table width="400">
<tbody>
<tr><td style="backgrou nd-color: #CCC"><a href="#" onclick="return toggleItem('col lapse_myTbody2' )">Toggle 2</a></td></tr>
</tbody>

<tbody class="collapse _obj" id="collapse_my Tbody2">
<tr><td>Test row1</td></tr>
<tr><td>Test row2</td></tr>
<tr><td>Test row3</td></tr>
<tr><td>Test row4</td></tr>
<tr><td>Test row5</td></tr>
</tbody>
</table>

<table width="400">
<tbody>
<tr><td style="backgrou nd-color: #CCC"><a href="#" onclick="return toggleItem('col lapse_myTbody3' )">Toggle 3</a></td></tr>
</tbody>

<tbody class="collapse _obj" id="collapse_my Tbody3">
<tr><td>Test row1</td></tr>
<tr><td>Test row2</td></tr>
<tr><td>Test row3</td></tr>
<tr><td>Test row4</td></tr>
<tr><td>Test row5</td></tr>
</tbody>
</table>

</body>
</html>[/HTML]
Aug 30 '06 #1
1 7732
acoder
16,027 Recognized Expert Moderator MVP
Either use innerHTML, e.g.
Expand|Select|Wrap|Line Numbers
  1. for (f=0, f<max, f++) {
  2.     tableObj.innerHTML+='<td style="background-color: #CCC"><a href="#" onclick="return toggleItem(\'collapse_myTbody'+f+'\')">Toggle 1</a></td></tr>';
  3.  
or use the DOM methods, e.g.
Expand|Select|Wrap|Line Numbers
  1. for (f = 0; f < max; f++) {
  2.     var table = document.createElement("table");
  3.     var tbody = document.createElement("tbody");
  4.     ...
  5.     td.style.backgroundColor = "#CCC";
  6.     ...
  7.     tbody.appendChild(tr);
  8.     table.appendChild(tbody);
See this link for more information.
May 13 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
2074
by: Bootstrap bill | last post by:
I'm looking to create collapsible tables, similar to the outlines available in Microsoft Excel and Open Office spreadsheet. I'd prefer something cross platform. Is something like this available online? I've been searching for months, but haven't found anything remotely close. -- "It's easy enough to be pleasant, when life hums along like a song. But the man worth while is the man who can smile when
1
17673
by: Nathan Bloomfield | last post by:
Does anyone know if there is any documentation which relates to Access2k + ? or can anyone help adjust the code? I am having trouble converting the DAO references. TITLE :INF: How to Create a Dynamic Crosstab Report PRODUCT :Microsoft Access PROD/VER:1.00 1.10 OPER/SYS:WINDOWS
2
1431
by: geodev | last post by:
Hello, I’m currently writing an asp.net application that will be running on a Windows XP Professional workstation utilising IIS and MSDE Database. At a later date this application will need to run on a Windows Server 2003 IIS web farm utilising either MSDE Database or SQL Server Database. I’m developing this application utilising Microsoft’s Development Environment 2002 version.
0
1592
by: mj2736 | last post by:
I have a pair of database tables with a structure similar to the following: -------------------------------------------- Table: Category -------------------------------------------- CategoryID | CategoryName | ParentCategoryID 1 | 'A' | 0 2 | 'B' | 0
5
12263
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected to the database. SQLSTATE=57030. Background: I created a linked server to DB2 8.1 database which called GRR_DB2Server. In my stored procedure p_FetchRawData, I need to read some data from this linked server GRR_DB2Server and insert them into
7
4827
by: Kamal | last post by:
Hello all, I have a very simple html table with collapsible rows and sorting capabilities. The collapsible row is hidden with css rule (display:none). When one clicks in the left of the expandable row, the hidden row is made visible with css. The problem is when i sort the rows, the hidden rows get sorted as well which i don't want and want to be moved (while sorting) relative to their parent rows. The following is my complete html code...
9
2986
by: pbd22 | last post by:
Hi. This is just a disaster management question. I am using XMLHTTP for the dynamic loading of content in a very crucial area of my web site. Same as an IFrame, but using XMLHTTP and a DIV. I got the core of the javascript from here: http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm I noticed in the demo that sometimes the content takes a long
1
1644
by: mahesh123 | last post by:
I am using Collapsible Panel in my project.But in other form of my project i want to use collapsible panel inside other Collapsible panel.Is it possible. Both panels are to be dinamic.Is it possible can any one help me plz. My requirement is I am having different A's. Under each A Can have different B's. For each B can have diffrent C's.
0
7639
JamieHowarth0
by: JamieHowarth0 | last post by:
I'm about to set up a new website, powered by DotNetNuke (my favourite CMS at the moment, mainly because it's free), and I want to install a Counter-Strike game server onto the same machine. I also want a little module that will show visitors to my site how many people are logged onto the CS server and some basic stats. Sounds simple right? The problem is that when Counter-Strike server is installed, the database name is generated at random....
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.