How do I create an HTML table with a fixed/frozen left column and a scrollable body?

后端 未结 25 1578
有刺的猬
有刺的猬 2020-11-21 22:27

I need a simple solution. I know it\'s similar to some other questions, like:

  • HTML table with fixed headers and a fixed column?
  • How can I lock the fir
相关标签:
25条回答
  • 2020-11-21 23:09

    Opera was buggy for all of the previous answers when I tested them on my mac. If you scroll through the table the fixed column disappears after you pass the first unfixed column. I went ahead and wrote the code below. It works in all the browsers I have locally installed. I don't know how ie handles it though.

    Just keep that in mind that if you intend to skip rows in one table and not the other or change the heights of the rows you might need to adjust this code.

    <table class = "fixedColumns">
        <tr><td> row 1 </td></tr>
        <tr><td> row 2 </td></tr>
    </table>
    <table class = "scrollableTable">
        <tr><td> col 1 </td> <td> col 2 </td><td> col 3 </td><td> col 4 </td></tr>
        <tr><td> col 1 </td> <td> col 2 </td><td> col 3 </td><td> col 4 </td></tr>
    </table>
    
    <style type = "text/css" >
        .fixedColumns
        {
            vertical-align:top;
            display: inline-block;
        }
        .scrollableTable
        {
            display: inline-block;
            width:50px;
            white-space: nowrap;
            overflow-x: scroll;
        }
    </style>
    
    0 讨论(0)
  • 2020-11-21 23:10

    For me this was the only one that worked perfectly (thanks to Paul O'Brien!): https://codepen.io/paulobrien/pen/gWoVzN

    Here's the snippet:

    // requires jquery library
    jQuery(document).ready(function() {
      jQuery(".main-table").clone(true).appendTo('#table-scroll').addClass('clone');   
     });
      .table-scroll {
        position:relative;
        max-width:600px;
        margin:auto;
        overflow:hidden;
        border:1px solid #000;
      }
    .table-wrap {
    	width:100%;
    	overflow:auto;
    }
    .table-scroll table {
    	width:100%;
    	margin:auto;
    	border-collapse:separate;
    	border-spacing:0;
    }
    .table-scroll th, .table-scroll td {
    	padding:5px 10px;
    	border:1px solid #000;
    	background:#fff;
    	white-space:nowrap;
    	vertical-align:top;
    }
    .table-scroll thead, .table-scroll tfoot {
    	background:#f9f9f9;
    }
    .clone {
    	position:absolute;
    	top:0;
    	left:0;
    	pointer-events:none;
    }
    .clone th, .clone td {
    	visibility:hidden
    }
    .clone td, .clone th {
    	border-color:transparent
    }
    .clone tbody th {
    	visibility:visible;
    	color:red;
    }
    .clone .fixed-side {
    	border:1px solid #000;
    	background:#eee;
    	visibility:visible;
    }
    .clone thead, .clone tfoot{background:transparent;}
    <div id="table-scroll" class="table-scroll">
      <div class="table-wrap">
        <table class="main-table">
          <thead>
            <tr>
              <th class="fixed-side" scope="col">&nbsp;</th>
              <th scope="col">Header 2</th>
              <th scope="col">Header 3</th>
              <th scope="col">Header 4</th>
              <th scope="col">Header 5</th>
              <th scope="col">Header 6</th>
              <th scope="col">Header 7</th>
              <th scope="col">Header 8</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <th class="fixed-side">Left Column</th>
              <td>Cell content<br>
                test</td>
              <td><a href="#">Cell content longer</a></td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
            </tr>
            <tr>
              <th class="fixed-side">Left Column</th>
              <td>Cell content</td>
              <td>Cell content longer</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
            </tr>
            <tr>
              <th class="fixed-side">Left Column</th>
              <td>Cell content</td>
              <td>Cell content longer</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
            </tr>
            <tr>
              <th class="fixed-side">Left Column</th>
              <td>Cell content</td>
              <td>Cell content longer</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
            </tr>
            <tr>
              <th class="fixed-side">Left Column</th>
              <td>Cell content</td>
              <td>Cell content longer</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
            </tr>
            <tr>
              <th class="fixed-side">Left Column</th>
              <td>Cell content</td>
              <td>Cell content longer</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
              <td>Cell content</td>
            </tr>
          </tbody>
          <tfoot>
            <tr>
              <th class="fixed-side">&nbsp;</th>
              <td>Footer 2</td>
              <td>Footer 3</td>
              <td>Footer 4</td>
              <td>Footer 5</td>
              <td>Footer 6</td>
              <td>Footer 7</td>
              <td>Footer 8</td>
            </tr>
          </tfoot>
        </table>
      </div>
    </div>
    
    <p>See <a href="https://codepen.io/paulobrien/pen/LBrMxa" target="blank">position Sticky version </a>with no JS</p>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>

    0 讨论(0)
  • 2020-11-21 23:11

    You can use sticky position. Here is a sample code. This is HTML/CSS solution. No js is required.

    .view {
      margin: auto;
      width: 600px;
    }
    
    .wrapper {
      position: relative;
      overflow: auto;
      border: 1px solid black;
      white-space: nowrap;
    }
    
    .sticky-col {
      position: -webkit-sticky;
      position: sticky;
      background-color: white;
    }
    
    .first-col {
      width: 100px;
      min-width: 100px;
      max-width: 100px;
      left: 0px;
    }
    
    .second-col {
      width: 150px;
      min-width: 150px;
      max-width: 150px;
      left: 100px;
    }
    <div class="view">
      <div class="wrapper">
        <table class="table">
          <thead>
            <tr>
              <th class="sticky-col first-col">Number</th>
              <th class="sticky-col second-col">First Name</th>
              <th>Last Name</th>
              <th>Employer</th>
            </tr>
          </thead>
          <tbody>
            <tr>
              <td class="sticky-col first-col">1</td>
              <td class="sticky-col second-col">Mark</td>
              <td>Ham</td>
              <td>Micro</td>
            </tr>
            <tr>
              <td class="sticky-col first-col">2</td>
              <td class="sticky-col second-col">Jacob</td>
              <td>Smith</td>
              <td>Adob Adob Adob AdobAdob Adob Adob Adob Adob</td>
            </tr>
            <tr>
              <td class="sticky-col first-col">3</td>
              <td class="sticky-col second-col">Larry</td>
              <td>Wen</td>
              <td>Goog Goog Goog GoogGoog Goog Goog Goog Goog Goog</td>
            </tr>
          </tbody>
        </table>
      </div>
    </div>

    Bootply code: https://www.bootply.com/g8pfBXOcY9

    0 讨论(0)
  • 2020-11-21 23:11

    This is an interesting jQuery plugin that creates fixed headers and/or columns. Toggle fixed column to be true on the demo page to see it in action.

    0 讨论(0)
  • 2020-11-21 23:11

    I took Earmon Nerbonne's answer and edited it to work with tables that fill the whole width.

    http://jsfiddle.net/DYgD6/6/

    <!DOCTYPE html>
    <html><head><title>testdoc</title>
    <style type="text/css">
                body {
            font:16px Calibri;
        }
        table {
            border-collapse:separate;
            border-top: 3px solid grey;
        }
        td {
            margin:0;
            border:3px solid grey;
            border-top-width:0px;
            white-space:nowrap;
        }
        #outerdiv {
            position: absolute;
            top: 0;
            left: 0;
            right: 5em;
        }
        #innerdiv {
            width: 100%;
            overflow-x:scroll;
            margin-left: 5em;
            overflow-y:visible;
            padding-bottom:1px;
        }
        .headcol {
            position:absolute;
            width:5em;
            left:0;
            top:auto;
            border-right: 0px none black;
            border-top-width:3px;
            /*only relevant for first row*/
            margin-top:-3px;
            /*compensate for top border*/
        }
        .headcol:before {
            content:'Row ';
        }
        .long {
            background:yellow;
            letter-spacing:1em;
        }
    </style></head><body>
      <div id="outerdiv">
       <div id="innerdiv">
        <table>
            <tr>
                <td class="headcol">1</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
            </tr>
            <tr>
                <td class="headcol">2</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
            </tr>
            <tr>
                <td class="headcol">3</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
            </tr>
            <tr>
                <td class="headcol">4</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
            </tr>
            <tr>
                <td class="headcol">5</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
            </tr>
            <tr>
                <td class="headcol">6</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
            </tr>
            <tr>
                <td class="headcol">7</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
            </tr>
            <tr>
                <td class="headcol">8</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
            </tr>
            <tr>
                <td class="headcol">9</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
                <td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
            </tr>
        </table>
    </div></div>
    </body></html>
    

    The width of the fixed column still needs to be a set value though.

    0 讨论(0)
  • 2020-11-21 23:11

    $(document).ready(function() {
        var table = $('#example').DataTable( {
            scrollY:        "400px",
            scrollX:        true,
            scrollCollapse: true,
            paging:         true,
            fixedColumns:   {
                leftColumns: 3
            }
        } );
    } );
    <head>
    	<title>table</title>
    	
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/fixedcolumns/3.2.4/css/fixedColumns.dataTables.min.css">
    <script type="text/javascript" src="http://cdn.datatables.net/1.10.2/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/fixedcolumns/3.2.4/js/dataTables.fixedColumns.min.js"></script>
    
    
    <style>
           th, td { white-space: nowrap; }
        div.dataTables_wrapper {
            width: 900px;
            margin: 0 auto;
        }
    </style>
    
    </head>
    <table id="example" class="stripe row-border order-column" style="width:100%">
            <thead>
                <tr>
                    <th>First name</th>
                    <th>Last name</th>
                    <th>Position</th>
                    <th>Office</th>
                    <th>Age</th>
                    <th>Start date</th>
                    <th>Salary</th>
                    <th>Extn.</th>
                    <th>E-mail</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>Tiger</td>
                    <td>Nixon</td>
                    <td>System Architect</td>
                    <td>Edinburgh</td>
                    <td>61</td>
                    <td>2011/04/25</td>
                    <td>$320,800</td>
                    <td>5421</td>
                    <td>t.nixon@datatables.net</td>
                </tr>
                <tr>
                    <td>Garrett</td>
                    <td>Winters</td>
                    <td>Accountant</td>
                    <td>Tokyo</td>
                    <td>63</td>
                    <td>2011/07/25</td>
                    <td>$170,750</td>
                    <td>8422</td>
                    <td>g.winters@datatables.net</td>
                </tr>
                <tr>
                    <td>Ashton</td>
                    <td>Cox</td>
                    <td>Junior Technical Author</td>
                    <td>San Francisco</td>
                    <td>66</td>
                    <td>2009/01/12</td>
                    <td>$86,000</td>
                    <td>1562</td>
                    <td>a.cox@datatables.net</td>
                </tr>
                <tr>
                    <td>Cedric</td>
                    <td>Kelly</td>
                    <td>Senior Javascript Developer</td>
                    <td>Edinburgh</td>
                    <td>22</td>
                    <td>2012/03/29</td>
                    <td>$433,060</td>
                    <td>6224</td>
                    <td>c.kelly@datatables.net</td>
                </tr>
                <tr>
                    <td>Airi</td>
                    <td>Satou</td>
                    <td>Accountant</td>
                    <td>Tokyo</td>
                    <td>33</td>
                    <td>2008/11/28</td>
                    <td>$162,700</td>
                    <td>5407</td>
                    <td>a.satou@datatables.net</td>
                </tr>
                <tr>
                    <td>Brielle</td>
                    <td>Williamson</td>
                    <td>Integration Specialist</td>
                    <td>New York</td>
                    <td>61</td>
                    <td>2012/12/02</td>
                    <td>$372,000</td>
                    <td>4804</td>
                    <td>b.williamson@datatables.net</td>
                </tr>
                <tr>
                    <td>Herrod</td>
                    <td>Chandler</td>
                    <td>Sales Assistant</td>
                    <td>San Francisco</td>
                    <td>59</td>
                    <td>2012/08/06</td>
                    <td>$137,500</td>
                    <td>9608</td>
                    <td>h.chandler@datatables.net</td>
                </tr>
                <tr>
                    <td>Rhona</td>
                    <td>Davidson</td>
                    <td>Integration Specialist</td>
                    <td>Tokyo</td>
                    <td>55</td>
                    <td>2010/10/14</td>
                    <td>$327,900</td>
                    <td>6200</td>
                    <td>r.davidson@datatables.net</td>
                </tr>
                <tr>
                    <td>Colleen</td>
                    <td>Hurst</td>
                    <td>Javascript Developer</td>
                    <td>San Francisco</td>
                    <td>39</td>
                    <td>2009/09/15</td>
                    <td>$205,500</td>
                    <td>2360</td>
                    <td>c.hurst@datatables.net</td>
                </tr>
                <tr>
                    <td>Sonya</td>
                    <td>Frost</td>
                    <td>Software Engineer</td>
                    <td>Edinburgh</td>
                    <td>23</td>
                    <td>2008/12/13</td>
                    <td>$103,600</td>
                    <td>1667</td>
                    <td>s.frost@datatables.net</td>
                </tr>
                <tr>
                    <td>Jena</td>
                    <td>Gaines</td>
                    <td>Office Manager</td>
                    <td>London</td>
                    <td>30</td>
                    <td>2008/12/19</td>
                    <td>$90,560</td>
                    <td>3814</td>
                    <td>j.gaines@datatables.net</td>
                </tr>
                 <tr>
                    <td>Sakura</td>
                    <td>Yamamoto</td>
                    <td>Support Engineer</td>
                    <td>Tokyo</td>
                    <td>37</td>
                    <td>2009/08/19</td>
                    <td>$139,575</td>
                    <td>9383</td>
                    <td>s.yamamoto@datatables.net</td>
                </tr>
                <tr>
                    <td>Thor</td>
                    <td>Walton</td>
                    <td>Developer</td>
                    <td>New York</td>
                    <td>61</td>
                    <td>2013/08/11</td>
                    <td>$98,540</td>
                    <td>8327</td>
                    <td>t.walton@datatables.net</td>
                </tr>
                <tr>
                    <td>Finn</td>
                    <td>Camacho</td>
                    <td>Support Engineer</td>
                    <td>San Francisco</td>
                    <td>47</td>
                    <td>2009/07/07</td>
                    <td>$87,500</td>
                    <td>2927</td>
                    <td>f.camacho@datatables.net</td>
                </tr>
                <tr>
                    <td>Serge</td>
                    <td>Baldwin</td>
                    <td>Data Coordinator</td>
                    <td>Singapore</td>
                    <td>64</td>
                    <td>2012/04/09</td>
                    <td>$138,575</td>
                    <td>8352</td>
                    <td>s.baldwin@datatables.net</td>
                </tr>
                <tr>
                    <td>Zenaida</td>
                    <td>Frank</td>
                    <td>Software Engineer</td>
                    <td>New York</td>
                    <td>63</td>
                    <td>2010/01/04</td>
                    <td>$125,250</td>
                    <td>7439</td>
                    <td>z.frank@datatables.net</td>
                </tr>
                <tr>
                    <td>Zorita</td>
                    <td>Serrano</td>
                    <td>Software Engineer</td>
                    <td>San Francisco</td>
                    <td>56</td>
                    <td>2012/06/01</td>
                    <td>$115,000</td>
                    <td>4389</td>
                    <td>z.serrano@datatables.net</td>
                </tr>
                <tr>
                    <td>Jennifer</td>
                    <td>Acosta</td>
                    <td>Junior Javascript Developer</td>
                    <td>Edinburgh</td>
                    <td>43</td>
                    <td>2013/02/01</td>
                    <td>$75,650</td>
                    <td>3431</td>
                    <td>j.acosta@datatables.net</td>
                </tr>
                <tr>
                    <td>Cara</td>
                    <td>Stevens</td>
                    <td>Sales Assistant</td>
                    <td>New York</td>
                    <td>46</td>
                    <td>2011/12/06</td>
                    <td>$145,600</td>
                    <td>3990</td>
                    <td>c.stevens@datatables.net</td>
                </tr>
                <tr>
                    <td>Hermione</td>
                    <td>Butler</td>
                    <td>Regional Director</td>
                    <td>London</td>
                    <td>47</td>
                    <td>2011/03/21</td>
                    <td>$356,250</td>
                    <td>1016</td>
                    <td>h.butler@datatables.net</td>
                </tr>
                <tr>
                    <td>Lael</td>
                    <td>Greer</td>
                    <td>Systems Administrator</td>
                    <td>London</td>
                    <td>21</td>
                    <td>2009/02/27</td>
                    <td>$103,500</td>
                    <td>6733</td>
                    <td>l.greer@datatables.net</td>
                </tr>
                <tr>
                    <td>Jonas</td>
                    <td>Alexander</td>
                    <td>Developer</td>
                    <td>San Francisco</td>
                    <td>30</td>
                    <td>2010/07/14</td>
                    <td>$86,500</td>
                    <td>8196</td>
                    <td>j.alexander@datatables.net</td>
                </tr>
                <tr>
                    <td>Shad</td>
                    <td>Decker</td>
                    <td>Regional Director</td>
                    <td>Edinburgh</td>
                    <td>51</td>
                    <td>2008/11/13</td>
                    <td>$183,000</td>
                    <td>6373</td>
                    <td>s.decker@datatables.net</td>
                </tr>
                <tr>
                    <td>Michael</td>
                    <td>Bruce</td>
                    <td>Javascript Developer</td>
                    <td>Singapore</td>
                    <td>29</td>
                    <td>2011/06/27</td>
                    <td>$183,000</td>
                    <td>5384</td>
                    <td>m.bruce@datatables.net</td>
                </tr>
                <tr>
                    <td>Donna</td>
                    <td>Snider</td>
                    <td>Customer Support</td>
                    <td>New York</td>
                    <td>27</td>
                    <td>2011/01/25</td>
                    <td>$112,000</td>
                    <td>4226</td>
                    <td>d.snider@datatables.net</td>
                </tr>
            </tbody>
        </table>

    This can be easily done with the help of datatables. People who are new to data tables, please refer to https://datatables.net/ .Its a plugin and offers a lot of features.In the the code given, header is fixed,first 3 columns are fixed and several other features are also there.

    0 讨论(0)
提交回复
热议问题