views:

150

answers:

4

I have a web page where I have a table nested inside of a TD tag (don't flame me for this, I have a good reason for doing it this way). When the page loads, I want to expand the height of the nested table to be the height of the TD cell that contains it. Currently I do it with code like this:

$(document).ready(function()
{
    $('.TakeOffItemGroupTable').each(function()
    {
        $(this).height($(this).closest('td').height()); 
    });
}

This works, but if there are a lot of tables to resize on the page, it can take ~20 seconds for IE8 to do it (FF takes a second or two, of course). That's because $(this).height($(this).closest('td').height()); takes:

  • 1ms in Chrome
  • 18ms in Firefox
  • 330ms in IE8

Is there some other way that I can have the nested table always take on the height of its container?

Things I have tried:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
<html>
<head>
<title>Untitled</title>
</head>
<body>
  <table border="1" >
    <tr>
       <td width="100px">JKLSD FASJDFKLSA DFKLADFJL KASDJFKLSAD JFSAKLDF</td>
       <td style="height: 100%;">
           <table style="height:100%;" border="1">
            <tr>
              <td>
                    I should be 100% tall!
              </td>
            </tr>
           </table>
       </td>
    </tr>
  </table>
</body>
</html>

This works in Firefox but not in IE.

+1  A: 

If you give the outer td a pixel height, it should work. I guess currently the td is already at 100% of it's container's height.

Tatu Ulmanen
That works, but I can't give the outer TD a pixel height (it need to dynamically resize itself).
Jon Kruger
+5  A: 

I think with a correct HTML Doctype your 2nd example will work in IE as well, without any Javascript.

The following works for me in FF, and IE 6, 7, and 8:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
</head>
<body>
  <table style="height: 100%" border="1" >
    <tr><td>
       <table style="height:100%" border="1">
        <tr>
          <td>
                I am 100% tall!
          </td>
        </tr>
       </table>
    </td></tr>
  </table>
</body>
</html>
Pekka
That didn't work.
Jon Kruger
Does for me. See my edit.
Pekka
Your example does work, but using that DOCTYPE makes the rest of the CSS on my site go crazy.
Jon Kruger
What doctype are you using right now? You should have one to product valid HTML, and if you don't give one every browser will try to figure one out. It might be a good idea to introduce one and sort out the CSS stuff one by one.
Pekka
I currently have:`<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">`
Jon Kruger
Ahh, okay. That's a different thing then. Do any of the tips here help? http://apptools.com/examples/tableheight.php
Pekka
I saw that page but it didn't help... that page is referring to setting the height of a top-level element (directly underneath the `<body>` tag).
Jon Kruger
It *should* work if all elements have an explicit 100% height down to the `table` element.
Pekka
+1  A: 

Why can't you just specify the table to be 100% height in the HTML markup? This should just work without javascript.

<td height="100%">
  <table height="100%">
  ...
  </table>
</td>
Shawn Steward
That doesn't work.
Jon Kruger
+1  A: 

i know everyone loves jquery, but maybe simpler js will help?

      var t = document.getElementById("nestedTable");
      t.style.height = t.parentNode.offsetHeight.toString() + "px";

adjust for border/padding/margin as necessary.

lincolnk
I tried that... the problem is that document.getElementById() is slow in IE when you have a lot of stuff in the DOM. jQuery is just calling document.getElementById() under the covers anyway.
Jon Kruger