views:

275

answers:

3

Copy/paste this html code snippet and try it out in IE7. When you toggle the hidden columns it leaves a gap between the columns. In Firefox it works fine, the columns touch when minimized. Haven't tried IE8 yet, would be curious to hear how it works there. Any ideas? I've tried a bunch of things in the CSS like table-layout:fixed but no luck.

Note: Not looking for a different toggling method because the table I'm really dealing with is 50+ columns wide and 4000+ rows so looping/jquery techniques are too slow.

Here's the code - if someone can re-post a working version of it I'll instantly give them the check and be forever in your debt!

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
<script>
function toggle() {
   var tableobj = document.getElementById("mytable");
   if (tableobj.className == "") {
      tableobj.className = "hide1 hide2";
   }
   else {
      tableobj.className = "";
   }
}
</script>
<style>
   table { border-collapse: collapse; }
   td, th { border: 1px solid silver; }
   .hide1 .col1 { display: none; }
   .hide2 .col2 { display: none; }
</style>
</head>
<body>
<input type="button" value="toggle" onclick="toggle();" />
<table id="mytable">
<tr>
   <th>A</th>
   <th colspan="2">B</th>
   <th colspan="2" class="col1">B1</th>
   <th colspan="2">C</th>
   <th colspan="2" class="col2">C1</th>
</tr>
<tr>
   <td>123</td>
   <td>456</td>
   <td>789</td>
   <td class="col1">123</td>
   <td class="col1">456</td>
   <td>789</td>
   <td>123</td>
   <td class="col2">456</td>
   <td class="col2">789</td>
</tr>
</table>
</body>
</html>
A: 

Try this doctype declaration:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
edl
How would that make difference? His doctype is entirely fine. Why switching from HTML to XHTML?
BalusC
Sorry. edited. If you have an incorrectly declared doctype, IE goes into quirks mode i believe.
edl
Thanks for catching that, I left out the "!" in the doctype. Added it in but it made no difference.
Art Peterson
Sorry that didn't fix your problem. It fixes it on IE8 and I don't have IE7 to try it on. Good luck!
edl
+1  A: 

Don't yet have an explanation of why IE is doing this, but here's what's happening and here's how to work around it.

1) If you set the table class to 'hide1 hide2' in the html, then the table will render properly (no gap). Therefore, the problem seems to be related to the way that IE handles changes to a table via styles.

2) The gap between the columns is the width of the spanned column header.

3) If you eliminate column spanning (and the extra columns), then everything works fine.

I've found two workarounds. The first is to use code to toggle the display, but you've rejected that option.

The alternative is to eliminate the colspans. There are a variety of ways to do that. One is to convert the group of cells to be spanned into an embedded table (that is, instead of two TD elements, you'll have one TD which contains a TABLE with one TR and two TDs). Or you can use SPANs for cleaner code (with, say, a BORDER-RIGHT for all cells but the last).

jdigital
Elimination of the colspans is not a good workaround because the widths of the SPANs or the TDs in the embedded TABLE will vary depending on the value in the cell - so the border will not line up down the page like a table.What do you mean by using code to toggle the display - I rejected one particular implementation because it was significantly slower but perhaps there is a way that will give similar results to this method?
Art Peterson
Thanks for the attempt - that only works for one row. Like I already mentioned try to add a few more TR data rows with varying length values in the TDs and see how the border line does not line up down the page.
Art Peterson
To do this in code, keep what you already have, and then augment the toggle function to loop thru all the TH elements that want to hide and explicitly set their style.display to 'none' (or to an empty string when you want to make the columns visible again). The good news is that you only need to do the TH cells, not the TDs, so this might be efficient enought.
jdigital
BTW, with jquery, you can use $('th.col1').hide() to hide all TH cells with class col1 and you can use the show() method to show them (no need for you to search or loop when you can have jquery do it for you).
jdigital
I just tried using jquery to hide the TH cells with class col1 but it simply hid the TH cells but left the TD cells displayed.
Art Peterson
You need to keep the rest of your code in place (i.e., you should still toggle the styles using classname = 'hide1 hide2'). I have posted the code as another answer.
jdigital
+1  A: 

Here's a solution that uses JQuery to toggle the column headers (see my other answer for the rationale). Apart from the JQuery stuff, the rest of the html page is the same.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
<head>
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js"  type="text/javascript"></script>    

<script>
function toggle() {
   var tableobj = document.getElementById("mytable");
   if (tableobj.className == "") {
      tableobj.className = "hide1 hide2";
      $('th[class^=col]').hide();
   }
   else {
      tableobj.className = "";
      $('th[class^=col]').show();
   }
}
</script>
<style>
   table { border-collapse: collapse; }
   td, th { border: 1px solid silver; }
   .hide1 .col1 { display: none; }
   .hide2 .col2 { display: none; }
</style>
</head>
<body>
<input type="button" value="toggle" onclick="toggle();" />
<table id="mytable">
<tr>
   <th>A</th>
   <th colspan="2">B</th>
   <th colspan="2" class="col1">B1</th>
   <th colspan="2">C</th>
   <th colspan="2" class="col2">C1</th>
</tr>
<tr>
   <td>123</td>
   <td>456</td>
   <td>789</td>
   <td class="col1">123</td>
   <td class="col1">456</td>
   <td>789</td>
   <td>123</td>
   <td class="col2">456</td>
   <td class="col2">789</td>
</tr>
</table>
</body>
</html>
jdigital
Wow - this looks very promising! Testing it out will get back to you.
Art Peterson
This definitely fixes the issue, and since it only has to loop through the THs there is no slow-down. You are awesome, thank you thank you thank you!
Art Peterson
Glad that this worked out for you. I've come to the conclusion that this is a bug in IE7. If there are any performance issues, even a slight lag on a slow machine, you might want to conditionalize the jquery calls so that they're used only if needed.
jdigital