2010-08-04 125 views
52

我一直在想辦法用固定的第一列(和水平溢出的表的其餘部分)的表我看到一個職位,有一個類似的問題。但固定列位似乎沒有解決。幫幫我?水平滾動的HTML表格(第一列固定)

+0

我我想稍微擴展一下這個問題:我的一個朋友想製作一張不會擴展到100%以上頁面的表格,但是如果他們需要更多的內容水平空間比那裏是。實際上,這應該與MS Excel中的凍結列類似。那可能嗎? – 2010-09-04 11:09:00

+0

看到我對類似問題的回答:http://stackoverflow.com/a/17557830/1763149 – 2013-07-09 20:49:17

回答

23

我有一個類似表樣式像這樣:

<table style="width:100%; table-layout:fixed"> 
    <tr> 
     <td style="width: 150px">Hello, World!</td> 
     <td> 
      <div> 
       <pre style="margin:0; overflow:scroll">My preformatted content</pre> 
      </div> 
     </td> 
    </tr> 
</table> 
+3

這個答案真的會受益於這裏的演示。 – 2016-06-21 13:28:47

+0

[JSFIDDLE demo](https://jsfiddle.net/dx65qwa3/),這樣就更清楚這裏發生了什麼 – 2016-12-02 17:35:34

+6

那隻能滾動一個單元格... – w00t 2017-01-11 15:28:33

2

看看這個jQuery插件:

http://fixedheadertable.com

它增加了垂直(固定報頭行)或水平(固定的第一列)滾動到現有的HTML表格。有一個演示可以檢查這兩種情況的滾動。

+5

注意:這依賴於jquery中過時的功能,並且從未更新過1年 – 2013-03-03 21:19:42

+0

@HaykSaakian看起來這個項目現在正在更新?今年說,和jQuery版本1.7是必需的。 – Metropolis 2015-05-19 22:02:23

9

使用jQuery DataTables插件,它支持固定的標題和列。 此示例將固定列支持HTML表 「榜樣」:

http://datatables.net/extensions/fixedcolumns/

對於兩個固定欄目:

http://www.datatables.net/release-datatables/extensions/FixedColumns/examples/two_columns.html

+2

這兩個鏈接都已經死了... – Dave 2014-05-20 10:38:41

+0

修復左側欄是通過使用leftColumns初始化參數完成的。此鏈接工程http://www.datatables.net/extensions/fixedcolumns/(今日) – jpkeisala 2015-05-16 08:23:37

95

如何:

table { 
 
    table-layout: fixed; 
 
    width: 100%; 
 
    *margin-left: -100px; /*ie7*/ 
 
} 
 
td, th { 
 
    vertical-align: top; 
 
    border-top: 1px solid #ccc; 
 
    padding: 10px; 
 
    width: 100px; 
 
} 
 
.fix { 
 
    position: absolute; 
 
    *position: relative; /*ie7*/ 
 
    margin-left: -100px; 
 
    width: 100px; 
 
} 
 
.outer { 
 
    position: relative; 
 
} 
 
.inner { 
 
    overflow-x: scroll; 
 
    overflow-y: visible; 
 
    width: 400px; 
 
    margin-left: 100px; 
 
}
<div class="outer"> 
 
    <div class="inner"> 
 
    <table> 
 
     <tr> 
 
     <th class=fix></th> 
 
     <th>Col 1</th> 
 
     <th>Col 2</th> 
 
     <th>Col 3</th> 
 
     <th>Col 4</th> 
 
     <th class="fix">Col 5</th> 
 
     </tr> 
 
     <tr> 
 
     <th class=fix>Header A</th> 
 
     <td>col 1 - A</td> 
 
     <td>col 2 - A (WITH LONGER CONTENT)</td> 
 
     <td>col 3 - A</td> 
 
     <td>col 4 - A</td> 
 
     <td class=fix>col 5 - A</td> 
 
     </tr> 
 
     <tr> 
 
     <th class=fix>Header B</th> 
 
     <td>col 1 - B</td> 
 
     <td>col 2 - B</td> 
 
     <td>col 3 - B</td> 
 
     <td>col 4 - B</td> 
 
     <td class=fix>col 5 - B</td> 
 
     </tr> 
 
     <tr> 
 
     <th class=fix>Header C</th> 
 
     <td>col 1 - C</td> 
 
     <td>col 2 - C</td> 
 
     <td>col 3 - C</td> 
 
     <td>col 4 - C</td> 
 
     <td class=fix>col 5 - C</td> 
 
     </tr> 
 
    </table> 
 
    </div> 
 
</div>

您可以在此jsbin測試一下:http://jsbin.com/uxecel/4/edit

+0

簡單但有效的解決方案,我的查詢! – 2013-03-06 15:21:57

+0

這基本上適用於我,但我想從可見的滾動div的最右側列開始。我的專欄標題是日期,最近的日期在最右側,我希望在首次顯示

時可以看到它。有沒有辦法以編程方式做到這一點?有沒有辦法以編程方式向任一方向移動水平滾動條?我希望在表格頂部始終提供「向左滾動」和「向右滾動」按鈕,因爲瀏覽器支持的滾動條位於底部,並不總是立即可見。 – Kras2013-08-01 19:21:35

+0

史詩。大桌子節省大量的性能! – 2015-08-19 16:40:11

3

基於skube的做法,我覺得最少的一組CSS的我需要的是:

.horizontal-scroll-except-first-column { 
    width: 100%; 
    overflow: auto; 
    margin-left: 20em; 
} 

.horizontal-scroll-except-first-column > table > * > tr > th:first-child, 
.horizontal-scroll-except-first-column > table > * > tr > td:first-child { 
    position: absolute; 
    width: 20em; 
    margin-left: -20em; 
} 

.horizontal-scroll-except-first-column > table > * > tr > th, 
.horizontal-scroll-except-first-column > table > * > tr > td { 
    /* Without this, if a cell wraps onto two lines, the first column 
    * will look bad, and may need padding. */ 
    white-space: nowrap; 
} 

與HTML:

<div class="horizontal-scroll-except-first-column"> 
    <table> 
     ... 
    </table> 
</div> 
+0

偉大和易於理解,很好 – Ced 2018-02-28 15:23:14