24

I have a HTML table as given in the following code. This has 16 columns. I am setting the cell width using javascript. When the total width is less than 100%, it is working fine.

When the total width exceeds 100%, I want to display a horizontal scroll bar. But when I changed the width of the yellow highlighted cell from 5% to 30%, all the remaining cells get hidden instead of appearance of a scroll bar.

QUESTIONS:

  1. Why the scrollbar is not appearing at present ?
  2. How can we make it to work appropriately by showing scroll bar? (Also the two columns after the yellow column should visible; not hidden)

Note: The issue is noted in IE8.

Note: Refer http://jsfiddle.net/Lijo/dYSfN/2/ for code. This does not show the issue. For visualizing the issue, please copy the code into a html file and open using IE8

UPDATE

While hiding a column, remaining columns width automatically adjusts to table width in Firefox and Chrome but not in IE8. In IE8 the table width shrinks.

http://srikanthgade.blogspot.in/2010/09/ie8-standards-table-layout-fixed.html#!/2010/09/ie8-standards-table-layout-fixed.html

Issue Details

When Yellow Highlighted Cell Width is 5%

enter image description here

When Yellow Highlighted Cell Width is 30%

enter image description here

STYLE

   .gridTableBorder
   {
        overflow:scroll;
        border: 2px solid green;
   }

    /*GridView Tables*/
    .resultGridTable
    {
        table-layout: fixed; /*Needed along with word wrap */
    }

    .resultGridTable th
    {
        background-color: #A7A7A6;
        color: #ffffff;
        padding: 2px 5px 2px 5px;
        font: bold 9pt Arial;
        border: 1px solid red;
        word-wrap: break-word;
    }

    .resultGridTable td
    {
        padding: 0px 5px 0px 5px;
        font: normal 9pt Arial;
        word-wrap: break-word;
        border: 1px solid blue;
    }

JAVASCRIPT

    $(document).ready(function () {


        //Width Setting
        var numberOfColumns = 16;
        $('.resultGridTable th, .resultGridTable td').each(function (i) {


            if (i % numberOfColumns == 0) {
                $(this).css('width', '1%');
            }

            if (i % numberOfColumns == 1) {
                $(this).css('width', '10%');
            }

            if (i % numberOfColumns == 2) {
                $(this).css('width', '9%');

            }

            if (i % numberOfColumns == 3) {
                $(this).css('width', '8%');
                $(this).css('background-color', 'orange');
            }

            if (i % numberOfColumns == 4) {
                $(this).css('width', '6%');
            }
            if (i % numberOfColumns == 5) {
                $(this).css('width', '8%');
            }
            if (i % numberOfColumns == 6) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 7) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 8) {
                $(this).css('width', '5%');
            }

            ///
            if (i % numberOfColumns == 9) {
                $(this).css('width', '7%');
            }
            if (i % numberOfColumns == 10) {
                $(this).css('width', '8%');
                $(this).css('background-color', 'orange');
            }

            if (i % numberOfColumns == 11) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 12) {
                $(this).css('width', '5%');
            }

            if (i % numberOfColumns == 13) {
                $(this).css('width', '30%');
                $(this).css('background-color', 'Yellow');
            }

            if (i % numberOfColumns == 14) {
                $(this).css('width', '7%');
            }

            if (i % numberOfColumns == 15) {
                $(this).css('width', '7%');
            }

        }
        );


        //Hide Is Summary Row Column
        var selectedElements = $("tr").find("th:first, td:first");
        $(selectedElements).hide();


    }
    );

HTML

<body>
<form method="post" action="LocalTaxReport.aspx" id="form1">
<div id="wrapper">
    <div id="container">
        <div id="centralContainer">
            <div id="mainContainer">
                <div id="contentHolderDiv" class="contentHolderDiv">
                    <div id="resultContainer" class="resultContainerDiv">
                        <div id="gridDiv" class="gridTableBorder">
                            <div>
                                <table class="resultGridTable" cellspacing="0" id="detailContentPlaceholder_grdLocalTaxReport"
                                    style="border-collapse: collapse;">
                                    <tr>
                                        <th scope="col">
                                            IsSummaryRow
                                        </th>
                                        <th scope="col">
                                            Associate
                                        </th>
                                        <th scope="col">
                                            My Amount
                                        </th>
                                        <th scope="col">
                                            Federal Withholding
                                        </th>
                                        <th scope="col">
                                            Social Security
                                        </th>
                                        <th scope="col">
                                            Medicaring
                                        </th>
                                        <th scope="col">
                                            State Tax
                                        </th>
                                        <th scope="col">
                                            County Tax
                                        </th>
                                        <th scope="col">
                                            City Tax
                                        </th>
                                        <th scope="col">
                                            Total
                                        </th>
                                        <th scope="col">
                                            State
                                        </th>
                                        <th scope="col">
                                            State Code
                                        </th>
                                        <th scope="col">
                                            County
                                        </th>
                                        <th scope="col">
                                            County Code
                                        </th>
                                        <th scope="col">
                                            City
                                        </th>
                                        <th scope="col">
                                            City Code
                                        </th>
                                    </tr>
                                    <tr>
                                        <td>
                                            False
                                        </td>
                                        <td>
                                            Mary Dryden
                                        </td>
                                        <td>
                                            $3450
                                        </td>
                                        <td>
                                            $32
                                        </td>
                                        <td>
                                            $5
                                        </td>
                                        <td>
                                            $2
                                        </td>
                                        <td>
                                            $10
                                        </td>
                                        <td>
                                            $1
                                        </td>
                                        <td>
                                            $2
                                        </td>
                                        <td>
                                            $3400
                                        </td>
                                        <td>
                                            Arkansas
                                        </td>
                                        <td>
                                            AR
                                        </td>
                                        <td>
                                            Benton
                                        </td>
                                        <td>
                                            04567
                                        </td>
                                        <td>
                                            Bentonville
                                        </td>
                                        <td>
                                            23156
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div class="clear">
                    </div>
                </div>
            </div>
            <div class="clear">
            </div>
        </div>
    </div>
</div>
</form>

 <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>

</body>
LCJ
  • 22,196
  • 67
  • 260
  • 418
  • Do you have other CSS affecting this that is not posted here? I ask, because I do not see the behavior you describe when I set up your code and run it on IE8. What is the CSS for all the wrapper elements this table is buried in? – ScottS Nov 30 '12 at 14:15
  • @ScottS I don't have any other CSS. All the code is posted here. From the answer of Enrico it seems like it is reproducbale – LCJ Nov 30 '12 at 14:18
  • 1
    When I set up a page and view in IE8 at 30%, it just forces adjusts other table cell widths to keep the table at 100% width. I'm on Windows 7 viewing in IE9 set to show IE8 browser mode. So I guess I cannot be of any help. – ScottS Nov 30 '12 at 14:26

5 Answers5

18

I tried to achieve the desired effect you had with few modification and I am going to describe it here.

  1. You need to set the doctype. A DOCTYPE Declaration is mandatory for most current markup languages and without one it is impossible to reliably validate a document or determine what rules to apply.

  2. You are not seeing the scroll bars because the width of table is not set. The div by default takes 100%. Now set the overflow property to the div and then set the width of the table to 120% (that exceeds the div's 100%) to see the scroll bars.

  3. Table width of 120% doesn't mean you can sum of all column width to 120. Since the table-layout:fixed, You need to tally sum of all columns width to 100 even if you set the table width as 120%.

  4. In you code you were setting the width of all columns and rows, instead apply width only to th or the first row which will apply with to all columns in all rows (entire table).

  5. Modified your code from using if..if..if to use if..else if..else if

Note: 4 and 5 are improvements from the existing code.

DEMO: http://jsfiddle.net/FP7MF/2/embedded/result/

Full Code:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1">
    <title>My Report </title>
    <style type="text/css">

        /*GridView Tables*/
        .resultGridTable
        {
            table-layout: fixed; 
            width: 130%;
        }

        .resultGridTable th
        {
            background-color: #A7A7A6;
            color: #ffffff;
            padding: 2px 5px 2px 5px;
            font: bold 9pt Arial;
            border: 1px solid red;
            word-wrap: break-word;
        }

        .resultGridTable td
        {
            padding: 0px 5px 0px 5px;
            font: normal 9pt Arial;
            word-wrap: break-word;
            border: 1px solid blue;
        }

        #gridDiv div { overflow: auto;  }

    </style>
</head>
<body>
    <form>
        <div id="wrapper">
            <div id="container">
                <div id="centralContainer">
                    <div id="mainContainer">
                        <div id="contentHolderDiv" class="contentHolderDiv">
                            <div id="resultContainer" class="resultContainerDiv">
                                <div id="gridDiv" class="gridTableBorder">
                                    <div>
                                        <table class="resultGridTable" cellspacing="0" id="detailContentPlaceholder_grdLocalTaxReport"
                                             style="border-collapse: collapse;">
                                           <tr>
                                              <th scope="col">IsSummaryRow</th>
                                              <th scope="col">Associate</th>
                                              <th scope="col">My Amount</th>
                                              <th scope="col">Federal Withholding</th>
                                              <th scope="col">Social Security</th>
                                              <th scope="col">Medicaring</th>
                                              <th scope="col">State Tax</th>
                                              <th scope="col">County Tax</th>
                                              <th scope="col">City Tax</th>
                                              <th scope="col">Total</th>
                                              <th scope="col">State</th>
                                              <th scope="col">State Code</th>
                                              <th scope="col">County</th>
                                              <th scope="col">County Code</th>
                                              <th scope="col">City</th>
                                              <th scope="col">City Code</th>
                                           </tr>
                                           <tr>
                                               <td>False</td>
                                               <td>Mary Dryden</td>
                                               <td>$3450</td>
                                               <td>$32</td>
                                               <td>$5</td>
                                               <td>$2</td>
                                               <td>$10</td>
                                               <td>$1</td>
                                               <td>$2</td>
                                               <td>$3400</td>
                                               <td>Arkansas</td>
                                               <td>AR</td>
                                               <td>Benton</td>
                                               <td>04567</td>
                                               <td>Bentonville</td>
                                               <td>23156</td>
                                            </tr>
                                        </table>
                                    </div>
                                </div>
                            </div>
                            <div class="clear"></div>
                        </div>
                    </div>
                    <div class="clear"></div>
               </div>
            </div>
        </div>
    </form>
    <script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>

    <script type="text/javascript">


        $(document).ready(function () {


            //Width Setting
            var numberOfColumns = 16;
            $('.resultGridTable th').each(function (i) {

                if (i % numberOfColumns == 0) {
                    $(this).css('width', '1%');
                } else if (i % numberOfColumns == 1) {
                    $(this).css('width', '10%');
                } else if (i % numberOfColumns == 2) {
                    $(this).css('width', '9%');
                } else if (i % numberOfColumns == 3) {
                    $(this).css({'width': '8%', 'background-color': 'orange'});
                } else if (i % numberOfColumns == 4) {
                    $(this).css('width', '6%');
                } else if (i % numberOfColumns == 5) {
                    $(this).css('width', '8%');
                } else if (i % numberOfColumns == 6) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 7) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 8) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 9) {
                    $(this).css('width', '7%');
                } else if (i % numberOfColumns == 10) {
                    $(this).css({'width': '8%', 'background-color': 'orange'});
                } else if (i % numberOfColumns == 11) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 12) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 13) {
                    $(this).css({'width': '8%', 'background-color': 'Yellow'});
                } else if (i % numberOfColumns == 14) {
                    $(this).css('width', '5%');
                } else if (i % numberOfColumns == 15) {
                    $(this).css('width', '5%');
                }

            });

            //Hide Is Summary Row Column
            var selectedElements = $("tr").find("th:first, td:first");
            $(selectedElements).hide();
        });

    </script>
</body>
</html>
Selvakumar Arumugam
  • 79,297
  • 15
  • 120
  • 134
  • When I made the width of yellow column as 30%, the columns after the yellow column are not displayed properly. How can we fix that? – LCJ Dec 01 '12 at 14:36
  • 3
    Did you read the 3rd item which talks about the total sum of columns width not exceeding `100`.. It will work if you tally it to `100%`. – Selvakumar Arumugam Dec 01 '12 at 15:01
  • I don't think that is the reason. It is working fine in Chrome and Firefox even with 30%. Any rationale behind that? – LCJ Dec 02 '12 at 06:31
  • 3
    @Lijo Every browser has their own rules and parsing technique. With table layout `fixed` and to make it work in IE. You need to tally it to `100`. Yes, lot of things works in FF and Chrome, but not in IE and this is one of them. 1. http://blogs.msdn.com/b/ie/archive/2005/02/10/370721.aspx 2. http://msdn.microsoft.com/en-us/library/ms533002%28v=vs.85%29.aspx – Selvakumar Arumugam Dec 02 '12 at 14:25
2

It's not a perfect solution, but setting the table width to 120% and changing the column widths to be percentages of the table width (so they sum up to 100%) achieves the desired effect.

Enrico
  • 10,377
  • 8
  • 44
  • 55
1

forgive me if i am wrong, but can't this be solved by adding overflow:scroll; as a css rule for the div that contains the table?

mr_lewjam
  • 1,180
  • 1
  • 9
  • 20
  • It doesn't work. I tried .gridTableBorder { overflow:scroll; border: 2px solid green; } – LCJ Nov 30 '12 at 10:41
1

If we need to keep the table-layout as fixed in IE8, then below workaround needs to be added after hiding the columns:

:
:
$(selectedElements).hide();

// Add the workaround after the above line (or when hiding of columns is done).
$(".resultGridTable").attr("style","display:inline-table;");
window.setTimeout(function(){$(".resultGridTable").attr("style","");},0);

This is a bug in IE8, but it seems Microsoft has not responded on any resolution. If you find any information, please post it here.

But I have tried the workaround, still it does not give the desired output.

Just added it here, so that it might help in some more R&D.

References:

http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/f265c5b1-a45d-4949-85b2-86a97a884dc1/

Workaround:

IE8 isn't resizing tbody or thead when a column is hidden in a table with table-layout:fixed

http://srikanthgade.blogspot.in/2010/09/ie8-standards-table-layout-fixed.html

Community
  • 1
  • 1
Rups
  • 629
  • 1
  • 8
  • 19
  • Please try IE8 in IE8 :-) Please refer @ScottS comment also in the question – LCJ Dec 02 '12 at 11:11
  • I have updated the post. Let me know whether it solved your issue. – Rups Dec 02 '12 at 11:56
  • 1
    That won't help. I need the table-layout to be fixed. Please explain why it works in Chrome and Firefox with table-layout (with some references to prove) – LCJ Dec 02 '12 at 13:50
  • Found some references, might help in further R&D. – Rups Dec 03 '12 at 04:57
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
.gridTableBorder
   {
        overflow:scroll;
        border: 2px solid green;
   }

    /*GridView Tables*/
    .resultGridTable
    {
        /*table-layout: fixed;*/ /*Needed along with word wrap */
    }

    .resultGridTable th
    {
        background-color: #A7A7A6;
        color: #ffffff;
        padding: 2px 5px 2px 5px;
        font: bold 9pt Arial;
        border: 1px solid red;
        /*word-wrap: break-word;*/
    }

    .resultGridTable td
    {
        padding: 0px 5px 0px 5px;
        font: normal 9pt Arial;
        /*word-wrap: break-word;*/
        border: 1px solid blue;
    }
</style>
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.1.js"></script>
<script type="text/javascript">

$(document).ready(function () {


        //Width Setting
        var numberOfColumns = 16;
        $('.resultGridTable th, .resultGridTable td').each(function (i) {


            if (i % numberOfColumns == 0) {
                $(this).css('width', '1%');
            }

            if (i % numberOfColumns == 1) {
                $(this).css('width', '10%');
            }

            if (i % numberOfColumns == 2) {
                $(this).css('width', '9%');

            }

            if (i % numberOfColumns == 3) {
                $(this).css('width', '8%');
                $(this).css('background-color', 'orange');
            }

            if (i % numberOfColumns == 4) {
                $(this).css('width', '6%');
            }
            if (i % numberOfColumns == 5) {
                $(this).css('width', '8%');
            }
            if (i % numberOfColumns == 6) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 7) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 8) {
                $(this).css('width', '5%');
            }

            ///
            if (i % numberOfColumns == 9) {
                $(this).css('width', '7%');
            }
            if (i % numberOfColumns == 10) {
                $(this).css('width', '8%');
                $(this).css('background-color', 'orange');
            }

            if (i % numberOfColumns == 11) {
                $(this).css('width', '5%');
            }
            if (i % numberOfColumns == 12) {
                $(this).css('width', '5%');
            }

            if (i % numberOfColumns == 13) {
                $(this).css('width', '30%');
                $(this).css('background-color', 'Yellow');
            }

            if (i % numberOfColumns == 14) {
                $(this).css('width', '7%');
            }

            if (i % numberOfColumns == 15) {
                $(this).css('width', '7%');
            }

        }
        );


        //Hide Is Summary Row Column
        var selectedElements = $("tr").find("th:first, td:first");
        $(selectedElements).hide();


    }
    );
</script>
</head>

<body>

<form method="post" action="LocalTaxReport.aspx" id="form1">
<div id="wrapper">
    <div id="container">
        <div id="centralContainer">
            <div id="mainContainer">
                <div id="contentHolderDiv" class="contentHolderDiv">
                    <div id="resultContainer" class="resultContainerDiv">
                        <div id="gridDiv" class="gridTableBorder">
                            <div>
                                <table class="resultGridTable" cellspacing="1" id="detailContentPlaceholder_grdLocalTaxReport" style="border-collapse: collapse;">
                                    <tr>
                                        <th scope="col">
                                            IsSummaryRow
                                        </th>
                                        <th scope="col">
                                            Associate
                                        </th>
                                        <th scope="col">
                                            My Amount
                                        </th>
                                        <th scope="col">
                                            Federal Withholding
                                        </th>
                                        <th scope="col">
                                            Social Security
                                        </th>
                                        <th scope="col">
                                            Medicaring
                                        </th>
                                        <th scope="col">
                                            State Tax
                                        </th>
                                        <th scope="col">
                                            County Tax
                                        </th>
                                        <th scope="col">
                                            City Tax
                                        </th>
                                        <th scope="col">
                                            Total
                                        </th>
                                        <th scope="col">
                                            State
                                        </th>
                                        <th scope="col">
                                            State Code
                                        </th>
                                        <th scope="col">
                                            County
                                        </th>
                                        <th scope="col">
                                            County Code
                                        </th>
                                        <th scope="col">
                                            City
                                        </th>
                                        <th scope="col">
                                            City Code
                                        </th>
                                    </tr>
                                    <tr>
                                        <td>
                                            False
                                        </td>
                                        <td>
                                            Mary Dryden
                                        </td>
                                        <td>
                                            $3450
                                        </td>
                                        <td>
                                            $32
                                        </td>
                                        <td>
                                            $5
                                        </td>
                                        <td>
                                            $2
                                        </td>
                                        <td>
                                            $10
                                        </td>
                                        <td>
                                            $1
                                        </td>
                                        <td>
                                            $2
                                        </td>
                                        <td>
                                            $3400
                                        </td>
                                        <td>
                                            Arkansas
                                        </td>
                                        <td>
                                            AR
                                        </td>
                                        <td>
                                            Benton
                                        </td>
                                        <td>
                                            04567
                                        </td>
                                        <td>
                                            Bentonville
                                        </td>
                                        <td>
                                            23156
                                        </td>
                                    </tr>
                                </table>
                            </div>
                        </div>
                    </div>
                    <div class="clear">
                    </div>
                </div>
            </div>
            <div class="clear">
            </div>
        </div>
    </div>
</div>
</form>

</body>
</html>

I have just go through your problem, i have done some R&D on your code and css. I have just removed the table-layout:fixed and word-wrap:break-word;

Which was giving issue in IE8 as well as in IE7. In your screen shot 2 in which cell height is increased due to word-wrap:break-word.

In other browsers it was working fine. Just copy the code and check in your browser. If your needs are fulfilling without using table-layout:fixed and word-wrap css property than this is your solution.

@Lijo if i am missing something please let me know.

w3uiguru
  • 5,864
  • 2
  • 21
  • 25