Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scrollbar on bootstrap table

I have table that renders inside a panel which is inside a modal. As the table is relatively large, I'd like to restrict it's rows to say 5 so that the modal does not become scrollable. I looked through SO and google and everywhere I see that I need to set the overflow-y:auto or overflow-y:scroll to get it to work, however in my case, it does not. I also set a random height of 400px and position:absolute. This got the table to be scrollable but now the panel closes with the <thead> and the body of the table renders outside the panel. What's the fix to this?

My code snippet is:

<div class="modal fade">
   <div class="modal-dialog " >
      <div class="modal-content">
         <div class="modal-body">
            <div class="panel panel-default">
               <div class="panel-body">
                  <table class="table table-bordered>
                     <thead>
                         [........]
                     </thead>
                     <tbody style="overflow-y:auto; height:400px; position:absolute>
                     [.......]
                     </tbody>
                   </table>

[...the rest of the </div>s...]


EDIT

Thanks for the responses. Is there a way to narrow down the scroll bar to the <tbody> alone so that the <thead> remains static?

like image 431
blueren Avatar asked Sep 29 '16 10:09

blueren


People also ask

How do I add a scrollbar to a table in bootstrap?

Instead put style on the div with class panel-body as overflow-y:scroll; height:200px; see demo below in answers. Do you have a missing end quote on the table class in your source too, or is that just an artifact from cutting down the code here in the question.

How do I make my bootstrap table scrollable vertically?

Here is the task to make vertically scrollable in a bootstrap row. It can be done by the following approach: Approach: Making all the div element in next line using position: absolute; property (arrange all div column-wise).

How do I add a scrollbar to a table in HTML?

Suppose we want to add a scroll bar option in HTML, use an “overflow” option and set it as auto-enabled for adding both horizontal and vertical scroll bars. If we want to add a vertical bar option in Html, add the line “overflow-y” in the files.


2 Answers

Wrap it in table-responsive and set a max-height:

.table-responsive {
    max-height:300px;
}

http://www.codeply.com/go/S6MgKWqFvj

like image 150
Zim Avatar answered Sep 20 '22 14:09

Zim


Here is the demo

#collapse1{
overflow-y:scroll;
height:200px;
  <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/1.12.4/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<div class="container">
  <h2>Collapsible List with table</h2>
  <div class="panel-group">
    <div class="panel panel-default">
      <div class="panel-heading">
        <h4 class="panel-title">
          <a data-toggle="collapse" href="#collapse1">Collapsible list group with table</a>
        </h4>
      </div>
      <div id="collapse1" class="panel-collapse collapse">
        <table class="table">
    <thead>
      <tr>
        <th>Firstname</th>
        <th>Lastname</th>
        <th>Email</th>
      </tr>
    </thead>
    <tbody  >
      <tr>
        <td>John</td>
        <td>Doe</td>
        <td>[email protected]</td>
      </tr>
 <tr>
        <td>John</td>
        <td>Doe</td>
        <td>[email protected]</td>
      </tr>
 <tr>
        <td>John</td>
        <td>Doe</td>
        <td>[email protected]</td>
      </tr>
 <tr>
        <td>John</td>
        <td>Doe</td>
        <td>[email protected]</td>
      </tr>
 <tr>
        <td>John</td>
        <td>Doe</td>
        <td>[email protected]</td>
      </tr>
 <tr>
        <td>John</td>
        <td>Doe</td>
        <td>[email protected]</td>
      </tr>
 <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>
      </div>
    </div>
  </div>
</div>
like image 21
Anil Panwar Avatar answered Sep 19 '22 14:09

Anil Panwar