Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add scrollbar to the table body when table rows reaches more than 5?

Lets say I am reading the table contents from database and printing the table rows. Sometimes I may have more than 5 rows data in the database sometimes I may have less than 5 rows data in the database.

What I want is when the table rows exceeds 5 rows it should take scrollbar, if it is less than 5 rows it should not take scrollbar.

I have gone through many websites where they fix the table body height and if the content exceeds that height it takes the scrollbar.

So I don't want table body height to be fixed because when there is less content in the table body it will take the fixed table body size it look odd. Thanks in advance.

<!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>[email protected]</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</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>

My table look like below image. enter image description here

like image 512
Prajna Hegde Avatar asked Sep 06 '25 02:09

Prajna Hegde


2 Answers

Scrollable tables aren't the most fun to play around with, so I've added a snippet that should get you moving along.

$(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>[email protected]</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</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>

You have to count the number of rows then add class when the rows are greater than 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>[email protected]</td>
      </tr>
      <tr>
        <td>Mary</td>
        <td>Moe</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</td>
      </tr>
      <tr>
        <td>July</td>
        <td>Dooley</td>
        <td>[email protected]</td>
      </tr>
    </tbody>
  </table>

  <p>Note that we start the search in tbody, to prevent filtering the table headers.</p>
  </div>
</div>
like image 36
suzan Avatar answered Sep 07 '25 23:09

suzan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!