473,394 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

collapse and uncollapse tables

<table width="225" border="0">
<tr>
<td width="10" id="MyCell1"
onclick="ShowHide('MyRow1','MyCell1')"><img
src="images/plus3.gif"></td>
<td width="205">blahasdfadfadfasd</strong></a></div></td>
</tr>
<tr id="MyRow1">
<td>&nbsp;</td>
<td>
<table width="100%" border="0">
<tr>
<td>teadasdgadsf</a></td>
</tr>
</table>
</td></tr>
<tr>
<td width="10" id="MyCell2"
onclick="ShowHide('MyRow2','MyCell2')"><img
src="images/plus3.gif"></td>
<td width="205">blahasdfadfadfasd</strong></a></div></td>
</tr>
<tr id="MyRow2">
<td>&nbsp;</td>
<td>
<table width="100%" border="0">
<tr>
<td>teadasdgadsf</a></td>
</tr>
</table>
</td></tr>
<tr>
<td width="10" id="MyCell3"
onclick="ShowHide('MyRow3','MyCell3')"><img
src="images/plus3.gif"></td>
<td width="205">blahasdfadfadfasd</strong></a></div></td>
</tr>
<tr id="MyRow3">
<td>&nbsp;</td>
<td>
<table width="100%" border="0">
<tr>
<td>teadasdgadsf</a></td>
</tr>
</table>
</td></tr>
</table>
<script type="text/javascript">
var m_intShow = 0;
function ShowHide(strRowID,strCellID)
{
var objRow = document.getElementById(strRowID);

if (m_intShow == 0)
{
objRow.style.display = "block";
var objCell = document.getElementById(strCellID);
objCell.innerHTML = "<img src=images/minus2.gif>";
m_intShow = 1;
}
else
{
objRow.style.display = "none";
var objCell = document.getElementById(strCellID);
objCell.innerHTML = "<img src=images/plus3.gif>";
m_intShow = 0;
};
}

function runhide(){
var objs = new Array();
rowCount = this.asdfform.txtAID.value;
for(var i=1;i<=rowCount;i++){
(objs[i] = document.getElementById("MyRow"+i)).style.display =
"none";
}
}

runhide()
</script>

So everything works really good on load up, it loads the rows in the
tables. When the table is done, the javascript loads runhide() which
collapses all of the rows.

The javascript works fine when they click on the plus and it shows the
row, and when I click on the same plus it collapses. The problem
is...when I click the first cell, it shows the row, then If I want to
expand cell 2, I have to click it twice to get it to expand. Then when
I go to cell 1, I have to click twice to collapse it.

If anyone can correct my coding it would be appreciated, thanks.

Sep 28 '05 #1
7 4883

Samir wrote:
[snip]
<script type="text/javascript">
var m_intShow = 0;
function ShowHide(strRowID,strCellID)
{
var objRow = document.getElementById(strRowID);

if (m_intShow == 0)
{
objRow.style.display = "block";
var objCell = document.getElementById(strCellID);
objCell.innerHTML = "<img src=images/minus2.gif>";
m_intShow = 1;
}
else
{
objRow.style.display = "none";
var objCell = document.getElementById(strCellID);
objCell.innerHTML = "<img src=images/plus3.gif>";
m_intShow = 0;
};
}

The problem is here. You have a logic error. You have a global
variable called m_intShow. Now consider the steps you just went
through:

step 1. The value of m_intShow is 0. You click on a +. The value of
m_intShow is now 1.

step 2. You click on a different +. The value of m_intShow is 1,
therefore the display is still set to "none". The value of m_intShow is
now 0.

step 3. You click on the same + again. The value of m_intShow is 0,
therefore the display will now be set to "block".

That is the reason why you have to click twice.

[snip] }
}

runhide()
</script>

So everything works really good on load up, it loads the rows in the
tables. When the table is done, the javascript loads runhide() which
collapses all of the rows.

The javascript works fine when they click on the plus and it shows the
row, and when I click on the same plus it collapses. The problem
is...when I click the first cell, it shows the row, then If I want to
expand cell 2, I have to click it twice to get it to expand. Then when
I go to cell 1, I have to click twice to collapse it.

If anyone can correct my coding it would be appreciated, thanks.


Sep 28 '05 #2
"Samir" <dl*****@yahoo.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...

[snip]

So everything works really good on load up, it loads the rows in the
tables. When the table is done, the javascript loads runhide() which
collapses all of the rows.

The javascript works fine when they click on the plus and it shows the
row, and when I click on the same plus it collapses. The problem
is...when I click the first cell, it shows the row, then If I want to
expand cell 2, I have to click it twice to get it to expand. Then when
I go to cell 1, I have to click twice to collapse it.

If anyone can correct my coding it would be appreciated, thanks.


This is a variation on your theme.
Test as-is. Watch for word-wrap.

<html>
<head>
<title>hideshow.htm</title>
<script type="text/javascript">
var a_intShow = new Array(0,0,0,0);
function ShowHide(intRowID,intCellID) {
var objRow = document.getElementById("MyRow"+intRowID);
if (a_intShow[intRowID] == 0) {
objRow.style.display = "block";
var objCell = document.getElementById("MyCell"+intCellID);
objCell.innerHTML = "<img src='_minus.gif'>";
a_intShow[intRowID] = 1;
} else {
objRow.style.display = "none";
var objCell = document.getElementById("MyCell"+intCellID);
objCell.innerHTML = "<img src='_plus.gif'>";
a_intShow[intRowID] = 0;
}
}
function runhide() {
var objs = new Array();
rowCount = a_intShow.length;
for (var i=1;i<rowCount;i++) {
objs[i] = document.getElementById("MyRow"+i).style.display =
"none";
}
}
</script>
</head>
<body onload="runhide()">
<table width="225" border="1">
<tr>
<td width="10" id="MyCell1" onclick="ShowHide(1,1)"><img
src="_plus.gif"></td>
<td width="205">blahasdfadfadfasd></td>
</tr>
<tr id="MyRow1">
<td>&nbsp;</td>
<td>
<table width="100%" border="0">
<tr>
<td>teadasdgadsf</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="10" id="MyCell2" onclick="ShowHide(2,2)"><img
src="_plus.gif"></td>
<td width="205">blahasdfadfadfasd</td>
</tr>
<tr id="MyRow2">
<td>&nbsp;</td>
<td>
<table width="100%" border="0">
<tr>
<td>teadasdgadsf</td>
</tr>
</table>
</td>
</tr>
<tr>
<td width="10" id="MyCell3" onclick="ShowHide(3,3)"><img
src="_plus.gif"></td>
<td width="205">blahasdfadfadfasd</td>
</tr>
<tr id="MyRow3">
<td>&nbsp;</td>
<td>
<table width="100%" border="0">
<tr>
<td>teadasdgadsf</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
I changed the following:

1)
var a_intShow = new Array(0,0,0,0);
as added.

2)
rowCount = a_intShow.length;
was
rowCount = this.asdfform.txtAID.value;
as a form wasn't defined.

3)
objs[i] = document.getElementById("MyRow"+i).style.display =
"none";
was
(objs[i] = document.getElementById("MyRow"+i)).style.display =
"none";
though I don't understand why you use "objs".

4)
<body onload="runhide()">
was
runhide();
as the table wasn't declared before it was exceuted;
(at least after I moved the JS into the <head>).

I also removed a bunch of unnecessary
</strong></a></div>
and
</a.>
tags.
Sep 28 '05 #3
On 28/09/2005 20:39, Samir wrote:

[snipped markup]

Why you have so many tables I'll never know, but...

[snip]
objRow.style.display = "block";


....do not use the block value to 'show' rows or other table components.
Table-related elements have their own special display property values.
Using other values will break rendering behaviour in conforming browsers
(IE is broken in this regard).

Instead, assign an empty string to the display property. This
effectively clears the none value, showing the row in whatever way the
browser happens to expect.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Sep 28 '05 #4
McKirahan,

Thanks, The example I gave you, and you make it work perfect. I just
have one more question.

The reason I had rowCount = this.asdfform.txtAID.value was I forgot
this was also in the page too
<form name="asdfform">
<input type="text" name="txtAID" style="VISIBILITY: hidden"
value="<%=strCount%>">
</form>

I did a count in ASP and tells me how many rows I have, so I can
collapse at the load page.

The javascript you gave me works perfect!! except when I use it in my
actual page, it works for the first 3 rows and the rest is working but
not like the first three rows.
Any where to write the array to account for the rowCountValue??

Thanks for all your help so far.

Sep 28 '05 #5
Okay I think I got it, took some thinking but it's working so far!!

rowCount = this.asdfform.txtAID.value;
var a_intShow = new Array(rowCount);
for(var i=0;i<=rowCount;i++){
a_intShow[i] = 0
}

What do you think?

Sep 28 '05 #6
Samir wrote:
McKirahan,

Thanks, The example I gave you, and you make it work perfect. I just
have one more question.

The reason I had rowCount = this.asdfform.txtAID.value was I forgot
this was also in the page too
<form name="asdfform">
<input type="text" name="txtAID" style="VISIBILITY: hidden"
value="<%=strCount%>">
</form>

I did a count in ASP and tells me how many rows I have, so I can
collapse at the load page.
The number of rows in the table is given by the length of the table's
rows collection. You don't need the server to tell you how many there are.

var numberOfRows = someTableRef.rows.length;

The javascript you gave me works perfect!! except when I use it in my
actual page, it works for the first 3 rows and the rest is working but
not like the first three rows.
Any where to write the array to account for the rowCountValue??


You don't need to. All you need to do is show or hide cells that have
an index greater than zero in whatever row was clicked on. And the
hideShow function just needs to toggle between 'none' and ''.

That last bit is very important - the default value of a TD's display
attribute is not 'block', it is 'table-cell'. Since IE is broken in
this regard, you won't notice, but you will in other browsers.

<URL:http://www.w3.org/TR/REC-CSS2/tables.html#q5>

Switching between '' and 'none' allows it to return to whatever value
the browser likes without you having to know whether that was 'block' or
'table-cell' or some other value.

There have been several examples of how to do it posted in the last few
weeks, here's an example based on your posted code:

<table width="225" border="1" id="tableA">
<tr>
<td width="10"><img src="minus.gif"
onclick="showHideCells(this)"></td>
<td width="205">blahasdfadfadfasd</strong></a></td>
</tr>
<tr>
<td width="10"><img src="minus.gif"
onclick="showHideCells(this)"></td>
<td width="205">blahasdfadfadfasd</strong></a></td>
</tr>
<tr>
<td width="10"><img src="minus.gif"
onclick="showHideCells(this)"></td>
<td width="205">blahasdfadfadfasd</strong></a></td>
</tr>
</table>
<script type="text/javascript">

function showHideCells( el )
{
// Change the image between + and -
if (el.src.match('plus')){
el.src = el.src.replace(/plus/,'minus');
} else {
el.src = el.src.replace(/minus/,'plus');
}

// Find the parent td
while (el.parentNode && 'td' != el.nodeName.toLowerCase()){
el = el.parentNode;
}

// Run across all siblings and show/hide them
while (el.nextSibling){
el = el.nextSibling;
if (el.style){
el.style.display = ('none' == el.style.display)? '' : 'none';
}
}
}

// Initially hide the cells using showHideCells()
function runHide(id)
{
if ( ! document.getElementById
|| ! document.getElementsByTagName
|| ! document.body.style ) {
return;
}
var el = document.getElementById(id);
var rows = el.rows;
var i = rows.length;
while (i--){
showHideCells( rows[i].getElementsByTagName('img')[0]);
}
}

runHide('tableA');

</script>

--
Rob
Sep 29 '05 #7
RobG,

Thanks for the replay, your coding seems to work but not really the way
I had the example.

Your coding shows the the 3 rows and shows the first cell of each row
and hides the 2nd cell until it is clicked.

What I am doing in the ASP page;

I do a query to a database table, say the table has data for company
and in this table I have over 50. and each company has a related table
which stores what products they have available, one company can have
one, and other have many, so I guess it's call a one to many.

I can write it to work on ASP but it involves the user to reload, I
figure javascript would be a lot more efficient in showing and hiding
the table rows.

So basically the code looks like this. I also added some code to show
and hide for onlclicks which kinda works.

<table width="225" border="0">
<%
'Show all accounts on load
strQ = ""
strQ = strQ & "SELECT * from comptbl where compid != '' Order By
compID"
'Execute query<br>
strCount = 0
Set oRS = objConn.Execute(strQ)
If not oRS.EOF then
Do While NOT oRS.EOF
strCount = strCount + 1
'View accounts
%>
<tr>
<td width="10" id="MyCell<%=strCount%>"
onclick="ShowHide('<%=strCount%>','<%=strCount%>') "><img
src="images/plus3.gif"></td>
<td width="205"><div align="left"><strong><a
href="Details.asp?id=<%=oRS("compID")%>"
target="_main"><%=oRS("compID")%>
</strong></a></div></td>
</tr>
<tr id="MyRow<%=strCount%>">
<td>&nbsp;</td>
<td>
<%
sql = ""
rs = ""
sql = sql & "SELECT * from compProd where compID = '" & oRS("compID")
& "'"
sql = sql & " Order By name;"
Set rs = objConn.Execute(sql)
If not rs.EOF then
Do While NOT rs.EOF
%>
<table width="100%" border="0">
<tr>
<td><a href="Details.asp?pid=<%=rs("compID")%>" target="_main">
<%=rs("pid")%>, <%=rs("pName")%></a></td>
</tr>
</table>
<%
rs.MoveNext
Loop
rs.Close
Set rs=Nothing
End if
%>
</td></tr>
<%
oRS.MoveNext
Loop
oRS.Close
Set oRS = Nothing
End if
%>
</table>

<form name="asdfform">
<input type="text" name="txtAID" style="VISIBILITY: hidden"
value="<%=strCountclients%>">

<script type="text/javascript">
rowCount = this.asdfform.txtAID.value;
var a_intShow = new Array(rowCount);
for(var i=0;i<=rowCount;i++){
a_intShow[i] = 0
}

function ShowHide(intRowID,intCellID) {
var objRow = document.getElementById("MyRow"+intRowID);
if (a_intShow[intRowID] == 0) {
objRow.style.display = "block";
var objCell = document.getElementById("MyCell"+intCellID);
objCell.innerHTML = "<img src='images/minus2.gif'>";
a_intShow[intRowID] = 1;
} else {
objRow.style.display = "none";
var objCell = document.getElementById("MyCell"+intCellID);
objCell.innerHTML = "<img src='images/plus3.gif'>";
a_intShow[intRowID] = 0;
}
}

function runhide(){
var objs = new Array();
rowCount = this.asdfform.txtAID.value;
for(var i=1;i<=rowCount;i++){
(objs[i] =
document.getElementById("MyRow"+i)).style.display= "none";
}
}

function showusers(){
var objs = new Array();
rowCount = this.asdfform.txtAID.value;
for(var i=1;i<=rowCount;i++){
(objs[i] =
document.getElementById("MyRow"+i)).style.display= "block";
}
}

function showaccounts(){
var objs = new Array();
rowCount = this.asdfform.txtAID.value;
for(var i=1;i<=rowCount;i++){
(objs[i] =
document.getElementById("MyRow"+i)).style.display= "none";
}
}
</script>

So if you have any suggestions that would be great!

Thanks.

Sep 29 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jukka K. Korpela | last post by:
I have noticed that the meaning of visibility: collapse has been discussed on different forums, but with no consensus on what it really means. Besides, implementations differ. The specification...
7
by: Daniel Jung | last post by:
Hi ======== Problem: ======== I want images in table rows connect vertically. All images are 32 px in height. =====
3
by: ireneatngs | last post by:
Hi, I have example html below which contains a couple of hidden divs. However, some of the table borders within these hidden divs are actually displayed when they should not be. In my...
1
by: Holmespundit | last post by:
Stylists... I'm having problems collapsing a column. Specifically, I want to collapse the last column of a table. My initial test with a large data set worked, but I cannot make it work with a...
3
by: Erwin Moller | last post by:
Hi all, Is it possible to collapse certain columns in a rendered table? I have been fiddling around with CSS and visibiliy: collapse, but I cannot get it working in NN6 and IE6 and firefox. ...
0
by: invinfo | last post by:
I read some NG posts on this and also looked at W3C and others. http://www.w3.org/TR/CSS2/tables.html http://aerie.chirp.com.au/css/bordercollapse/ I could not make border-style: hidden...
1
by: Vaclav Jedlicka | last post by:
Hi I need a datagrid on a page, but it is rendered with the style "border-collapse:collapse;". I do not need this style. It interferes with the settings in my CSS file. I tried to supress it...
4
by: Gönen EREN | last post by:
how can i collapse or expand a node of treeview control programmaticly? thanks.
0
by: Shadow Lynx | last post by:
When using ASP.NET 2.0's built-in TreeView on a page with <BASE target = "AnythingBut_Self"></BASE> in the HEAD, the expand/collapse buttons fail to function. The reason for this is that the...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
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...
0
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...

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.