带有水平滚动条的数据表固定标题

时间:2019-03-25 17:54:26

标签: javascript html css datatables

我有一个数据表,我想实现固定的标头和水平滚动。固定的页眉和水平滚动条各自可以单独工作,但是一旦我尝试使用它们,它们将无法工作。

没有滚动条,我有固定的标题,没有固定头,我有滚动条。两者兼有,滚动条没有固定的标题。如何同时使用两者。我已经读到它们是不兼容的功能,但是必须通过使用HTML,CSS和/或JS解决此问题。非常感谢这里的任何帮助!

https://jsfiddle.net/Ntuttle3/jrz5npLb/9/

HTML

<html>

  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="icon" href="../../../../favicon.ico">
    <title>Album example for Bootstrap</title>
    <!-- Bootstrap core CSS -->
    <link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/fixedheader/3.1.5/css/fixedHeader.dataTables.min.css">
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/fixedheader/3.1.5/js/dataTables.fixedHeader.min.js"></script>
  </head>

  <body>
    <header>
      <nav class="navbar navbar-expand-lg navbar-dark bg-dark">
        <a class="navbar-brand" href="/"> <img src="#" height="30" width="30" class="d-inline-block align-top"> Website</a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button>
        <div class="collapse navbar-collapse" id="navbarSupportedContent">
          <ul class="navbar-nav mr-auto">
            <li class="nav-item"> <a class="nav-link" href="#">Office</a> </li>
          </ul>
        </div>
      </nav>
    </header>
    <main>
      <br>
      <br>

      <table id="dtBasicExample">
        <thead>
          <tr>
            <th>Header1</th>
            <th>Header2</th>
            <th>Header3</th>
            <th>Header4</th>
            <th>Header5</th>
            <th>Header6</th>
            <th>Header7</th>
            <th>Header8</th>
            <th>Header9</th>
            <th>Header10</th>
            <th>Header11</th>
            <th>Header12</th>
            <th>Header13</th>
            <th>Header14</th>
            <th>Header15</th>
            <th>Header16</th>
            <th>Header17</th>
            <th>Header18</th>
            <th>Header19</th>
            <th>Header20</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
          <tr>
            <td>Column 1</td>
            <td>Column 2</td>
            <td>Column 3</td>
            <td>Column 4</td>
            <td>Column 5</td>
            <td>Column 6</td>
            <td>Column 7</td>
            <td>Column 8</td>
            <td>Column 9</td>
            <td>Column 10</td>
            <td>Column 11</td>
            <td>Column 12</td>
            <td>Column 13</td>
            <td>Column 14</td>
            <td>Column 15</td>
            <td>Column 16</td>
            <td>Column 17</td>
            <td>Column 18</td>
            <td>Column 19</td>
            <td>Column 20</td>
          </tr>
        </tbody>
      </table>

      <br>
    </main>
    <div class="footer">
      <div class="container">
        <div id="footer-center">Copyright © My Comapany
          <div id="footer-right"><a href="#" class="text-muted">Back to top</a></div>
          <div style="clear: both"></div>
        </div>
      </div>
    </div>
  </body>
</html>

CSS

html,
body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
}

.footer {
  background-color: #003366;
  text-align: center;
  color: white;
  height: 30px;
}

#footer-center {
  color: white;
}

#footer-right {
  float: right;
  color: blue;
}

.navbar-nav li:hover>.dropdown-menu {
  display: block;
  content: none;
}

.dropdown-menu.show {
  background-color: #f5f6f7;
}

header {
  height: 75px;
  background: red;
  left: 0;
  right: 0;
  width: 100%;
  position: fixed;
  z-index: 500;
}

nav {
  background-color: #262626;
  height: 60px;
  width: 100%;
  position: fixed;
  top: 0;
  -webkit-box-shadow: 0px 0px 8px 0px #000000;
  -moz-box-shadow: 0px 0px 8px 0px #000000;
}

thead tr th,
tbody tr td {
  width: 150 px;
  min-width: 150 px;
  max-width: 150 px;
}

table th {
  position: -webkit-sticky;
  background: #fff;
  text-align: center;
}

table {
  font-size: 14px;
}

main {
  margin: auto;
  flex: 1;
  padding-top: 75;
  margin: 0 auto;
  width: 90%;
}

tr:nth-child(even) {
  background-color: #f2f2f2;
}

td {
  text-align: center;
}

table tr td:first-child {
  text-align: left;
  padding-left: 25px;
}

table.GeneratedTable {
  width: 100%;
  background-color: #ffffff;
  border-collapse: collapse;
  border-color: #ffcc00;
  border-style: solid;
  color: #000000;
}

table.GeneratedTable td,
table.GeneratedTable th {
  border-color: #ffcc00;
  border-style: solid;
  padding: 10px;
}

table.GeneratedTable thead {
  background-color: #ffcc00;
}

JavaScript

  $(document).ready(function() {
    $('#dtBasicExample').DataTable({
      "searching": true,
      "paging": true,
      "pageLength": 50,
      "info": false,
      "scrollX": true,
      "fixedHeader": {
        "headerOffset": 75
      }
    })
  });

3 个答案:

答案 0 :(得分:0)

根据the documentation,目前无法实现:

  

请注意,FixedHeader当前与启用了DataTables滚动功能(scrollX / scrollY)的表不兼容。有关完整的兼容性详细信息,请参阅兼容性表。

他们还提供了feature compatibility table,它可以帮助您确定它是否满足其他需求,或者这是否是一个破坏交易者,而这并不是您的正确解决方案。

答案 1 :(得分:0)

我仍然是新手,但我认为您想实现这一目标。

$(document).ready(function() {
    $('#dtBasicExample').DataTable({
      "searching": true,
      "paging": true,
      "pageLength": 50,
      "info": false,
      "scrollX": true,
      scrollCollapse: true,
      "scrollY":'60vh'
    })
  });

答案 2 :(得分:0)

迟到的答案,但如果您将 div container 更改为 container-fluid,它将起作用。