2012-11-28 7 views
24

次のコードのHTML表があります。これには16列あります。私はjavascriptを使用してセルの幅を設定しています。合計幅が100%未満の場合、正常に動作しています。合計幅が100%を超えると、表のセルが非表示になる

合計幅が100%を超える場合はhorizontal scroll barと表示したいとします。しかし、黄色の強調表示されたセルの幅を5%から30%に変更すると、残りのセルはすべてスクロールバーの表示ではなく非表示になります。

質問

  1. なぜスクロールバーが現在表示されませんか?
  2. scroll barを表示することで、どのように適切に動作させることができますか? (黄色の列べきvisible後も2列;隠されていない)

注:この問題は、IE8に記載されています。

注:コードについては、http://jsfiddle.net/Lijo/dYSfN/2/を参照してください。これは問題を示していません。問題を可視化するために、hiding a columnながら、残りの列が自動的にFirefoxとChromeでテーブルの幅に合わせて調整ではなく、IE8の幅htmlファイルにコードをコピーして、IE8

UPDATE

を使用して開いてください。 IE8ではテーブルの幅はshrinksです。

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

号詳細

黄色の強調表示セルの幅が5%

enter image description here

黄色の強調表示のセル幅である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> 
+0

ここに掲載されていないその他のCSSがありますか?あなたのコードをセットアップしてIE8で実行するときに、あなたが記述している動作が表示されないので、私は尋ねます。この表が埋め込まれているすべてのラッパー要素のCSSは何ですか? – ScottS

+0

@Scott私は他のCSSを持っていません。すべてのコードはここに掲載されています。エンリコの答えは、それがreproducbaleのように思われる – Lijo

+1

IE8で30%のページとビューを設定すると、テーブルを100%幅に保つように他のテーブルのセル幅を調整するだけです。 IE8ブラウザモードを表示するように設定されたIE9でWindows 7を表示しています。だから、私は何の助けもできないと思う。 – ScottS

答えて

18

私はいくつかの変更を加えて、あなたが望む効果を達成しようとしましたが、ここでそれを説明します。

  1. doctypeを設定する必要があります。 DOCTYPE宣言は最新のマークアップ言語では必須であり、文書を確実に検証したり、適用するルールを決定することは不可能です。

  2. テーブルの幅が設定されていないため、スクロールバーが表示されません。 divのデフォルトは100%です。ここでoverflowプロパティをdivに設定し、スクロールバーを表示するためにテーブルの幅を(divの100%を超える)に設定します。

  3. 表の幅が120%の場合、すべての列の幅を120に合計することはできません。 table-layout:fixed以降、表の幅を120%に設定しても、すべての列の幅を100に集計する必要があります。

  4. コードでは、すべての列と行の幅を設定する代わりに、幅がすべてthまたはすべての行(テーブル全体)のすべての列に適用される最初の行にのみ適用されます。 if..else if..else if

注使用するif..if..ifを使用してから、あなたのコードを修正

  • : 4と5は、既存のコードからの改善点です。

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

    全コード:

    <!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> 
    
  • +0

    黄色の列の幅を30%に設定した場合、黄色の列の後の列は正しく表示されません。どうすればそれを修正できますか? – Lijo

    +3

    列幅の総和が '100 'を超えないことを話している3番目の項目を読んだことがありますか?それを' 100% 'に集計するとうまくいくでしょう。 –

    +0

    私はそれが理由だとは思わない。 ChromeとFirefoxでは30%でもうまく動作しています。その背後にある根拠? – Lijo

    1

    私は間違っているが、これは以下のようにoverflow:scroll;を追加することで解決できない場合は、私を許してdivのCSSルールテーブルを含んでいますか?

    +0

    動作しません。私は.gridTableBorder {overflow:scroll;}を試みました。国境:2ピクセル緑色} – Lijo

    2

    これは完璧な解決策ではありませんが、テーブルの幅を120%に設定し、列の幅をテーブルの幅のパーセンテージに変更することで100%になります。

    +0

    幅が100%のときにスクロールバーが表示されない理由を説明できますか? – Lijo

    +1

    まだ分かりませんが、わかったら私の答えを更新します。 – Enrico

    1

    我々はIE8で修正されたように、テーブルレイアウトを維持する必要がある場合は、回避策の下の列を非表示にした後に追加する必要があります:

    : 
    : 
    $(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); 
    

    これはIE8のバグですが、Microsoftいかなる決議についても返答していない。何か情報を見つけたら、ここに投稿してください。

    しかし、私は回避策を試しましたが、依然として望ましい出力は得られません。それはいくつかのより多くのRで& D.を助けるかもしれないように

    ただ、ここにこれを追加しました

    参考文献:

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

    回避策:

    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

    +0

    IE8でIE8を試してください:-)質問にも@ScottSのコメントを参考にしてください – Lijo

    +0

    私は投稿を更新しました。それがあなたの問題を解決したかどうかを教えてください。 – Rups

    +1

    それは役に立たないでしょう。私はテーブルレイアウトを修正する必要があります。 ChromeとFirefoxでテーブルレイアウトのある理由を説明してください(いくつかの参考文献があります) – Lijo

    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> 
    

    私はちょうどあなたの問題を通過した、私はいくつかのR &あなたのコードとCSSで行っている。私はちょうどテーブルレイアウトを削除しました:固定とワードラップ:ブレークワード;

    IE7とIE8の両方で問題を提起していたものです。ワードラップによりセルの高さが増加するスクリーンショット2:ブレークワード。

    他のブラウザではうまくいきました。コードをコピーしてブラウザにチェックインするだけです。テーブルレイアウトを使用せずにニーズが満たされている場合:これよりも固定およびワードラップCSSプロパティがあなたのソリューションです。

    @Lijoもし私が何かが不足していたら私に知らせてください。

    +0

    私はtable-layoutを省略することはできません:fixedとword-wrap:break-word; – Lijo

    関連する問題