2013-04-24 84 views
5

我一直在爲HTML/CSS流體寬度,流體高度,固定表頭工作而沒有成功。我希望你能指點我一個JavaScript或jQuery解決方案。如何使用jQuery製作流體寬度,流體高度,固定標題表?

標準:

  1. 表必須填寫的瀏覽器窗口(至少集裝箱)
  2. 表必須填充可用容器的整個高度在瀏覽器窗口中
  3. TBODY必須滾動Y的整個寬度上如果有沒有足夠的房間中的所有行
  4. THEAD日和TBODY TD列必須排隊

純HTML/CSS是一個胸圍。如何jQuery或JavaScript?

這裏有一個小提琴:http://jsfiddle.net/JXWfC/6/

它最終將需要在IE7 +,以及Chrome和Firefox的現代版本。

這裏是我的基本頁面的一些標記:

<div class="container"> 
    <div class="sidebar"> 
     nav here 
    </div> 
    <div class="content"> 
     <table> 
      <thead> 
       <tr><th>column 1</th><th>2</th><th>three</th><th>this is column four</th></tr> 
      </thead> 
      <tbody> 
       <tr><td>data</td><td>can</td><td>have</td><td>different widths: not all the same</td></tr> 
       <tr><td>table</td><td>-</td><td>should</td><td>fill 100% width and 100% height</td></tr> 
       <tr><td>and</td><td>-</td><td>should</td><td>not require all td's to be same width</td></tr> 
       <tr><td>but</td><td>-</td><td>percentage</td><td>% widths are just fine</td></tr> 
       <tr><td>and</td><td>if</td><td>there's</td><td>too many rows, it should scroll y (overflow-y: scroll)</td></tr> 
       <tr><td>sample</td><td>1</td><td>row</td><td>Lorem ipsum dolor sit amit.</td></tr> 
       <tr><td>sample</td><td>1</td><td>row</td><td>Lorem ipsum dolor sit amit.</td></tr> 
       <tr><td>sample</td><td>1</td><td>row</td><td>Lorem ipsum dolor sit amit.</td></tr> 
       <tr><td>sample</td><td>1</td><td>row</td><td>Lorem ipsum dolor sit amit.</td></tr> 
      </tbody> 
     </table> 
    </div> 
</div> 
+0

爲什麼純HTML/CSS一個_bust_?您的標準似乎並不苛刻。 – adamb 2013-04-24 16:33:43

+0

@adamb,我無法使它工作。能給我看看麼?從我的研究來看,這是一個普遍的問題。是的,它看起來並不是太苛刻,但是有一些重大挑戰 - 1)高度總是挑戰,2)溢出滾動只在表格單元顯示時才起作用:阻止列的流體寬度對齊的塊(可以設置每列的靜態像素寬度,但不是流體百分比)。這就是我尋求js解決方案的原因。但是我很高興在這裏錯了。 ;) – Ryan 2013-04-24 19:04:44

+0

當你說你想要表格填滿整個高度時,你的意思是表格單元需要垂直展開嗎?例如,如果我的頁面高度爲900px,並且表格中只有3行,它們每個都應該是300px高?如果不是,你能澄清你需要什麼嗎? – DACrosby 2013-04-24 19:33:59

回答

2

這是不完整的,沒有任何東西而Chrome得到遏制,但希望它會讓你在正確的軌道上。

基本上這種方法是使容器position:relative;th標籤全部爲position:absolute;。這需要相當多的額外代碼才能正確定位,但是使標題始終保持不變並且表格單元格滾動是一個不錯的方法。

或者,您可以使標題與表格分開,但仍然需要設置寬度以便列排齊。一匹馬,真的。

http://jsfiddle.net/daCrosby/JXWfC/9/

<div id="hold"> 
    <div class="header"> 
     <h4>title goes here</h4> 
    </div> 
    <div class="container"> 
     <div class="sidebar">sidebar<br />nav<br />goes<br />here</div> 
     <div class="content" > 
      <table> 
       <thead> 
        <tr> 
         <th class="col1">column 1</th> 
         <th class="col2">2</th> 
         <th class="col3">three</th> 
         <th class="col4">this is column four</th> 
        </tr> 
       </thead> 
       <tbody> 
        <tr> 
         <td class="col1">data</td> 
         <td class="col2">can</td> 
         <td class="col3">have</td> 
         <td class="col4">different widths: not all the same</td> 
        </tr> 
       [ ... ] 

CSS

body{ 
    background:#000; 
} 
div { 
    display:inline-block; 
    vertical-align:top; 
    -moz-box-sizing: border-box; -webkit-box-sizing: border-box; box-sizing: border-box; 
} 
#hold{ 
    display:block; 
    padding:40px 10px; 
    width:95%; 
    margin:auto; 
} 
.content table, 
.header, 
.container{ 
    width:100%; 
    max-height:100%; 
} 
.header { 
    background:darkgray; 
    color:white; 
    text-align:center; 
} 
.container { 
    background:lightgray; 
} 
.sidebar { 
    width:20%; 
    background:green; 
    color:white; 
    float:left; 
} 
.content { 
    width:80%; 
    position:relative; 
} 
h4 { 
    margin:0; 
} 
table { 
    border-collapse: collapse; 
    border-spacing: 0; 
    background:red; 
    overflow-y:scroll; 
    height:300px; 
    display:block; 
} 
thead { 
    position:absolute; 
    top:1px; 
    left:0px; 
    right:0px; 
} 
tbody{ 
    padding:1.3em 0px 0px; 
    margin:0px; 
    display:block; 
} 
tr{ 
    width:100%; 
    display:block; 
    margin-top:-2px; 
} 
th { 
    background:#666; 
    color:#fff; 
    position:absolute; 
} 
td { 
    display:inline-block; 
    margin:0px; 
    padding:0px; 
    background:#ddd; 
} 
td.col2, 
td.col3, 
td.col4{ 
    margin-left:-4px; 
} 
.col1 { width:20%; } 
.col2 { width:10%; } 
.col3 { width:20%; } 
.col4 { width:50%; } 
th.col1 { left:0px; right:80%; } 
th.col2 { left:20%; right:70%; } 
th.col3 { left:30%; right:50%; } 
th.col4 { left:50%; right:0x; } 
+0

偉大的工作!該示例有效(至少在Chrome中)。我不確定絕對定位是否適合我在製作中,但我會試一試。沒有JavaScript的額外分數! – Ryan 2013-04-24 21:30:37

+0

另外,還有關於如何填充可用高度的任何提示?我懷疑這可能需要jQuery(可能'.height()'),但我不知道如何使它工作。 (我一直在玩'$(window).height() - somePixelHeight') – Ryan 2013-04-24 21:33:18

+0

Yay!這可能工作! http://jsfiddle.net/JXWfC/10/請繼續關注大綠色複選標記。我試圖讓它在生產中先行工作。 – Ryan 2013-04-24 21:40:33

0

這是你想要什麼? Working example

<div class="container" style="overflow:scroll;"> 
    <div class="sidebar">nav here</div> 
    <div class="content"> 
     <table> 
      <thead> 
       <tr> 
        <th width=25%>column 1</th> 
        <th width=25%>2</th> 
        <th width=25%>three</th> 
        <th width=25%>this is column four</th> 
       </tr> 
      </thead> 
      <tbody> 
       <tr> 
        <td>data</td> 
        <td>can</td> 
        <td>have</td> 
        <td>different widths: not all the same</td> 
       </tr> 
       <tr> 
        <td>table</td> 
        <td>-</td> 
        <td>should</td> 
        <td>fill 100% width and 100% height</td> 
       </tr> 
       <tr> 
        <td>and</td> 
        <td>-</td> 
        <td>should</td> 
        <td>not require all td's to be same width</td> 
       </tr> 
       <tr> 
        <td>but</td> 
        <td>-</td> 
        <td>percentage</td> 
        <td>% widths are just fine</td> 
       </tr> 
       <tr> 
        <td>and</td> 
        <td>if</td> 
        <td>there's</td> 
        <td>too many rows, it should scroll y (overflow-y: scroll)</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>1</td> 
        <td>row</td> 
        <td>Lorem ipsum dolor sit amit.</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>1</td> 
        <td>row</td> 
        <td>Lorem ipsum dolor sit amit.</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>1</td> 
        <td>row</td> 
        <td>Lorem ipsum dolor sit amit.</td> 
       </tr> 
       <tr> 
        <td>sample</td> 
        <td>1</td> 
        <td>row</td> 
        <td>Lorem ipsum dolor sit amit.</td> 
       </tr> 
      </tbody> 
     </table> 
    </div> 
</div> 
+0

這與我所尋找的更接近:http://jsfiddle.net/JXWfC/3/除了:(1)頭部不會像tbody滾動一樣可見,(2)表格不是容器的100%寬度(即它不會伸展到窗口的右側)。 – Ryan 2013-04-24 18:18:39

+0

http://jsfiddle.net/JXWfC/4/如果這匹配你想要的東西,看看這個。 另請參閱http://www.cssbakery.com/2010/12/css-scrolling-tables-with-fixed.html教程。 – 2013-04-24 19:14:00

+0

是的,你能否看到thead th與tbody td不一致?這裏是我更新的小提琴(沒有解決方案,只是在它上面):http://jsfiddle.net/JXWfC/5/ – Ryan 2013-04-24 19:22:47