2012-11-28 9 views
24

Mam tabelę HTML zgodnie z poniższym kodem. Ma 16 kolumn. Ustawiam szerokość komórki za pomocą javascript. Gdy całkowita szerokość jest mniejsza niż 100%, działa dobrze.Komórki tabeli są ukrywane, gdy całkowita szerokość jest większa niż 100%

Gdy całkowita szerokość przekracza 100%, chcę wyświetlić horizontal scroll bar. Ale gdy zmieniłem szerokość zaznaczonej na żółto komórki z 5% na 30%, wszystkie pozostałe komórki zostaną ukryte zamiast pojawienia się paska przewijania.

PYTANIA:

  1. Dlaczego pasek przewijania nie pojawia się w chwili obecnej?
  2. Jak możemy sprawić, aby działała prawidłowo, wyświetlając scroll bar? (również dwie kolumny po żółtej kolumnie powinien visible; nie ukryte)

Uwaga: Emisja zauważyć w IE8.

Uwaga: Dla kodu należy wpisać http://jsfiddle.net/Lijo/dYSfN/2/. To nie pokazuje problemu. Do wizualizacji problemu, skopiuj kod do pliku HTML i otwórz za pomocą IE8

UPDATE

Podczas hiding a column, pozostałe kolumny szerokość automatycznie dostosowuje się do szerokości stołu w Firefox i Chrome, ale nie w IE8. W IE8 szerokość stołu shrinks.

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

szczegóły problemu

Kiedy Żółty Wyróżnione komórki szerokość wynosi 5%

enter image description here

Kiedy Żółty Wyróżnione komórki szerokość wynosi 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

Czy masz inne CSS wpływające na to, co nie zostało tu zamieszczone? Pytam, ponieważ nie widzę zachowania, które opisujesz, kiedy ustawiam kod i uruchamiam go na IE8. Co to jest CSS dla wszystkich elementów opakowania, w których ta tabela jest pochowana? – ScottS

+0

@ScottS Nie mam żadnych innych CSS. Cały kod został opublikowany tutaj. Z odpowiedzi Enrico wygląda na to, że jest to reprodukcjum – Lijo

+1

Kiedy ustawię stronę i wyświetlam w IE8 na 30%, to po prostu wymusza dostosowanie innych szerokości komórek tabeli, aby utrzymać tabelę na 100% szerokości. Jestem w systemie Windows 7 w trybie IE9, aby wyświetlić tryb przeglądarki IE8. Sądzę więc, że nie mogę pomóc. – ScottS

Odpowiedz

18

Próbowałem osiągnąć pożądany efekt, który miałeś przy kilku modyfikacjach i opiszę to tutaj.

  1. Musisz ustawić doctype. Deklaracja DOCTYPE jest obowiązkowa dla większości aktualnych języków znaczników i bez niej niemożliwe jest wiarygodne potwierdzenie dokumentu lub ustalenie, jakie zasady należy zastosować.

  2. Nie widać pasków przewijania, ponieważ szerokość tabeli nie jest ustawiona. Domyślnie div przyjmuje wartość 100%. Teraz ustaw właściwość overflow na div, a następnie ustaw szerokość tabeli na 120% (która przekracza wartość div 100%), aby wyświetlić paski przewijania.

  3. Szerokość tabeli wynosząca 120% nie oznacza, że ​​można podsumować całą szerokość kolumny do 120. Od table-layout:fixed musisz sumować sumę wszystkich szerokości kolumn do 100, nawet jeśli ustawisz szerokość stołu jako 120%.

  4. W tobie ustawiłeś szerokość wszystkich kolumn i wierszy, zamiast stosować szerokość tylko do th lub pierwszego wiersza, który będzie stosowany do wszystkich kolumn we wszystkich wierszach (cała tabela).

  5. Zmodyfikowany kod z użyciem if..if..if używać if..else if..else if

Uwaga: 4 i 5 są ulepszenia z istniejącego kodu.

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

Pełny Kod:

<!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

Po ustawieniu szerokości żółtej kolumny na 30% kolumny za żółtą kolumną nie są wyświetlane poprawnie. Jak możemy to naprawić? – Lijo

+3

Czy przeczytałeś trzecią pozycję, która mówi o sumie sum kolumn szerokości nie przekraczającej '100' .. Będzie działać, jeśli porównasz go do' 100% '. –

+0

Nie sądzę, że to jest powód. Działa dobrze w Chrome i Firefox nawet z 30%. Jakieś uzasadnienie tego? – Lijo

1

mi wybaczyć, jeśli się mylę, ale nie może to być rozwiązany poprzez dodanie overflow:scroll; jako zasada css dla div który zawiera tabelę?

+0

To nie działa. Próbowałem .gridTableBorder {overflow: scroll; obramowanie: 2 piksele zielone; } – Lijo

2

Nie jest to idealne rozwiązanie, ale ustawienie szerokości stołu na 120% i zmiana szerokości kolumny na procenty szerokości stołu (czyli sumują się do 100%) pozwala uzyskać pożądany efekt.

+0

Czy możesz wyjaśnić, dlaczego pasek przewijania nie pojawia się, gdy szerokość wynosi 100%? – Lijo

+1

Jeszcze nie, ale zaktualizuję odpowiedź, jeśli się zorientuję. – Enrico

1

Jeśli musimy zachować table-layout ustalone w IE8, a następnie poniżej potrzebuje obejście zostać dodana po ukrywanie kolumn :

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

To jest błąd w IE8, ale wygląda na to, że Microsoft nie odpowiedział na żadną rezolucję. Jeśli znajdziesz jakieś informacje, opublikuj je tutaj.

Ale próbowałem obejścia, nadal nie daje pożądanej wydajności.

Wystarczy dodania go tutaj, tak, że może to pomóc w pewnym więcej R & D.

Referencje:

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

Obejście:

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

Proszę spróbuj IE8 w IE8 :-) Proszę odnieś komentarz @ScottS również w pytaniu – Lijo

+0

Zaktualizowałem wpis. Daj mi znać, czy rozwiązał Twój problem. – Rups

+1

To nie pomoże. Potrzebuję naprawić układ tabeli. Wyjaśnij, dlaczego działa w przeglądarce Chrome i Firefox z układem tabeli (z pewnymi referencjami, aby udowodnić). – 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> 

Właśnie przejrzałem twój problem, zrobiłem trochę R & D na twoim kodzie i css.Właśnie usunąłem układ tabeli: fixed i word-wrap: break-word;

Co było problemem w IE8, a także w IE7. Na swoim zrzucie ekranu 2, w którym wysokość komórki jest zwiększona z powodu zawijania słów: słowo-przerywane.

W innych przeglądarkach działało dobrze. Po prostu skopiuj kod i sprawdź w przeglądarce. Jeśli twoje potrzeby są spełniane bez używania układu tabeli: poprawiona i zawijana właściwość css, to jest twoje rozwiązanie.

@Lijo, jeśli czegoś brakuje, daj mi znać.

+0

Nie mogę pominąć układu tabeli: fixed i word-wrap: break-word; – Lijo

Powiązane problemy