【问题标题】:how to add scrollbar to the table body when table rows reaches more than 5?当表格行数超过 5 时,如何在表格正文中添加滚动条?
【发布时间】:2018-05-18 23:27:36
【问题描述】:

假设我正在从数据库中读取表格内容并打印表格行。有时我的数据库中可能有超过 5 行的数据,有时我的数据库中的数据可能少于 5 行。

我想要的是当表格行超过 5 行时应该使用滚动条,如果少于 5 行则不应该使用滚动条。

我浏览过许多网站,他们修复了表格主体的高度,如果内容超过该高度,它就会使用滚动条。

所以我不希望表格主体高度固定,因为当表格主体中的内容较少时,它会采用固定的表格主体大小,看起来很奇怪。 提前致谢。

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>

<body style="background-color:#F0F3F4">
<div class="container">
  <br/><br/><br/><br/>
  <div class="well" style="background-color:#FDFEFE" ><br/>
  <div class="row">
    <div class="col-xs-12">
      <div class="col-xs-8">
        <p class="col-md-10">This is to test the functionality of this table</p>
      </div>
      <div class="col-xs-4">
        <input class="form-control" id="myInput" type="text" placeholder="Search for a test....">
      </div>
    </div>
  </div>
  <br/>
  <table class="table">
    <thead class="table table-bordered" style="background-color:#EBF5FB">
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody scrollbars="yes" id="myTable" class="dyn-height">
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>john@example.com</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>mary@mail.com</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>july@greatstuff.com</td>
      </tr>
    </tbody>
  </table>

  <p>Note that we start the search in tbody, to prevent filtering the table headers.</p>
  </div>
</div>

<script>
$(document).ready(function(){
  $("#myInput").on("keyup", function() {
    var value = $(this).val().toLowerCase();
    $("#myTable tr").filter(function() {
      $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
    });
  });
});
</script>

</body>
</html>

我的桌子如下图所示。

【问题讨论】:

    标签: html twitter-bootstrap-3


    【解决方案1】:

    可滚动的表格并不是最有趣的,所以我添加了一个可以让你继续前进的 sn-p。

    $(document).ready(function(){
     if(  $('#results tr').length >= 5 ) {
          $('#myTable').addClass('add-scroll');
      }
    });
    .add-scroll {
      overflow-y: scroll;
      height: 100px;
    }
    
    #results tr td {
       width: 33%;
    }
    <!DOCTYPE html>
    <html lang="en">
    <head>
      <title>Bootstrap Example</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    
    <body style="background-color:#F0F3F4">
    <div class="container">
      <br/><br/><br/><br/>
      <div class="well" style="background-color:#FDFEFE" ><br/>
      <div class="row">
        <div class="col-xs-12">
          <div class="col-xs-8">
            <p class="col-md-10">This is to test the functionality of this table</p>
          </div>
          <div class="col-xs-4">
            <input class="form-control" id="myInput" type="text" placeholder="Search for a test....">
          </div>
        </div>
      </div>
      <br/>
      <table class="table">
        <thead class="table table-bordered" style="background-color:#EBF5FB">
          <tr>
          <td>
            <table width="100%" height="40">
            <tr>
            <th width="33%">Firstname</th>
            <th width="33%">Lastname</th>
            <th width="33%">Email</th>
             </tr>
            </table>
          </td>
            
          </tr>
        </thead>
        <tbody scrollbars="yes" class="dyn-height">
          <tr>
          <td>
          <div id="myTable">
         <table width="100%" id="results">
                 <tr>
            <td>John</td>
            <td>Doe</td>
            <td>john@example.com</td>
          </tr>
          <tr>
            <td>Mary</td>
            <td>Moe</td>
            <td>mary@mail.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@greatstuff.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@greatstuff.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@greatstuff.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@greatstuff.com</td>
          </tr>  
         </table>  
       </div>
          </td>
          </tr>
        </tbody>
      </table>
    
      <p>Note that we start the search in tbody, to prevent filtering the table headers.</p>
      </div>
    </div>
    
    </body>

    【讨论】:

    • 这可能会添加一个滚动条,但它不会使内容可滚动......不限制元素的高度,这不会达到预期的效果。
    • 这对我不起作用..:(我给了 id 和 class name 一样的 tbody 仍然不工作
    • 接收到的数据是否可以在 javascript 中迭代的数组中?身份证正确吗?您可以使用 jQuery $('#MyTableBodyID').addClass('add-scroll'),是否将类添加到元素中?您是否已将 css 导入相关页面?
    • @Testing 我添加了一个工作代码 sn-p 应该可以解决您的问题。
    • @Mike 我们不能在表格外添加滚动条,因为当浏览器尺寸较小时,它与对齐方式不匹配
    【解决方案2】:

    你必须计算行数,然后当行数大于 5 时添加类。

    $(document).ready(function(){
      var rowCount = $('tbody tr').length;
      if(rowCount > 5){
        console.log(rowCount);
        $('table').addClass('do-scroll');
      }
    });
    .do-scroll{
      width: 100%;
      height: 220px; 
      display: -moz-grid;
      overflow-y: scroll;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    
    <body style="background-color:#F0F3F4">
    <div class="container">
      <br/><br/><br/><br/>
      <div class="well" style="background-color:#FDFEFE" ><br/>
      <div class="row">
        <div class="col-xs-12">
          <div class="col-xs-8">
            <p class="col-md-10">This is to test the functionality of this table</p>
          </div>
          <div class="col-xs-4">
            <input class="form-control" id="myInput" type="text" placeholder="Search for a test....">
          </div>
        </div>
      </div>
      <br/>
      <table class="table">
        <thead class="table table-bordered" style="background-color:#EBF5FB">
          <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Email</th>
          </tr>
        </thead>
        <tbody scrollbars="yes" id="myTable" class="dyn-height">
          <tr>
            <td>John</td>
            <td>Doe</td>
            <td>john@example.com</td>
          </tr>
          <tr>
            <td>Mary</td>
            <td>Moe</td>
            <td>mary@mail.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@greatstuff.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@greatstuff.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@greatstuff.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@greatstuff.com</td>
          </tr>
          <tr>
            <td>July</td>
            <td>Dooley</td>
            <td>july@greatstuff.com</td>
          </tr>
        </tbody>
      </table>
    
      <p>Note that we start the search in tbody, to prevent filtering the table headers.</p>
      </div>
    </div>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-09
      • 1970-01-01
      • 1970-01-01
      • 2017-02-03
      • 2023-03-25
      • 1970-01-01
      • 2013-07-01
      • 1970-01-01
      相关资源
      最近更新 更多