1

I am using bootstrap table in my react application. But the table is not scrollable now. Whenever content gets loaded into it, it increases it height to accommodate the content. I want to make i scrollable. This is my table component

<div className='flex-vertical'>
                <Table className='flags-table' responsive hover>
                    <thead>
                    <tr>
                        <th></th>
                        <th> Time In</th>
                        <th> Time Out</th>
                        <th> Type</th>
                        <th> Category</th>
                    </tr>
                    </thead>
                    <tbody>
                    {
                        that.props.tag_fetch_reducer.tags.map((x, i) => (
                            <tr key={i} onClick={this.handleRowClick.bind(this, i)}>
                                <td>
                                    <div className='red-box'></div>
                                </td>
                                <td> {this.secondsToHms(x.time)} </td>
                                <td> {this.secondsToHms(x.stopTime)} </td>
                                <td> {x.tagname} </td>
                                <td contentEditable="false"> {x.category}</td>
                            </tr>
                        ))
                    }
                    </tbody>
                </Table>

and this is stylus part for this much code

.flex-vertical
    display flex
    flex-direction column
    height 100%

.flags-table
    .red-box
      width 16px
      height 16px
      margin 3px 5px
      background-color red

After implementing solution from comments I am getting this

enter image description here

I did this

.flex-vertical
    display flex
    flex-direction column
    height 100%
Table ,tr td{

}
tbody {
  display:block;
  height:50px;
  overflow:auto;
}
thead, tbody tr {
  display:table;
  width:100%;
  table-layout:fixed;/* even columns width , fix width of table too*/
}
thead {
  width: calc( 100% - 1em )/* scrollbar is average 1em/16px width, remove it from thead width */
}

enter image description here

EdG
  • 2,243
  • 6
  • 48
  • 103
  • can you turne this into plain html and tell us about your colums width , if there's any colspan or rowspan , ... if none of these and col of same/even width, then this can give you some hint : http://stackoverflow.com/questions/23989463/how-to-set-tbody-height-with-overflow-scroll/23989771#23989771 . flex could also be used nowdays – G-Cyrillus Feb 11 '17 at 19:06
  • Let me google for your http://stackoverflow.com/a/10420189/1815058 – uladzimir Feb 11 '17 at 19:14
  • @GCyrillus I implemented your solution. It works bit I am just able to see a small height table as the height is fixed to 50 px. What can I do so that it cover the alloted space – EdG Feb 11 '17 at 19:20
  • you need to set a similar height to the content you want to see filled, here you setted 50px so it will not grow more than this :( ( or did i misunderstand something ? flex would make it much easier :) – G-Cyrillus Feb 11 '17 at 19:26
  • Can you please write an answer using flex? – EdG Feb 11 '17 at 19:29
  • yes, here is also a codepen to play with http://codepen.io/gc-nomade/pen/zNyOpr – G-Cyrillus Feb 11 '17 at 19:56
  • I inserted your code but height is still not fixed – EdG Feb 11 '17 at 19:59
  • Have a look at updates – EdG Feb 11 '17 at 20:00
  • ?? you kept the display:block approach, i made an answer about flex which looked more efficient in your case. Did this worked or do you still have issues ? if yes, can you share your code within a jsfiddle or codepen that demonstrates your issue ? are you also using the flex version of bootstrap ? – G-Cyrillus Feb 11 '17 at 20:59
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135470/discussion-between-apurvg-and-gcyrillus). – EdG Feb 11 '17 at 21:08

1 Answers1

1

here is an example with flex : ( http://codepen.io/gc-nomade/pen/zNyOpr )

html,
body,
.flex-vertical {
  height: 100%;
  margin: 0;
}
.flex-vertical {
  display: flex;
  flex-flow: column;
}
.flags-table {
  display: flex;
  flex-flow: column;
  flex: 1;
  overflow: hidden;
}
tbody {
  overflow-y: scroll;
  background: turquoise
}
.red-box {
  width: 16px;
  height: 16px;
  margin: 3px 5px;
  background-color: red;
  margin: auto;
}
thead {
  display: block;
  margin-right: 1.075em;
  flex-shrink: 0;
  background: tomato;
}
tr {
  display: flex;
  width: 100%;
}
th,
td {
  border: 1px solid;
  min-width: 25px;
  display: flex;
}
th ~th,
td ~td {
  display: table-cell;
  flex: 1 1 22%;
  margin: 1px;
  word-wrap: break-word;
}
h1,
footer {
  margin: 1px;
  padding: 5px;
  background: orange
}
<div class='flex-vertical'>
  <h1>test</h1>
  <Table class='flags-table' responsive hover>
    <thead>
      <tr>
        <th></th>
        <th>Time In</th>
        <th>Time Out</th>
        <th>Type</th>
        <th>Category</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr>
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
    </tbody>
  </Table>
  <footer>
    footer
    </footer

inspired from How to set tbody height with overflow scroll : and How can I make my flexbox layout take 100% vertical space?


edit

if you the flex version of bootstrap, then you can also use classes

html,
body,
.flex-vertical {
  display:flex;
  flex-direction:column;
  height: 100%;
}

.flags-table {
  flex: 1;
  overflow: hidden;
}
tbody {
  flex:1;
  overflow-y: scroll;
  background: turquoise
}
.red-box {
  width: 16px;
  height: 16px;
  margin: 3px 5px;
  background-color: red;
  margin: auto;
}
thead {
  display: block;
  margin-right: 1.075em;
  flex-shrink: 0;
  background: tomato;
}
tr {
  width: 100%;
}
th,
td {
  border: 1px solid;
  min-width: 25px;
  display: flex;
}
th ~th,
td ~td {
  display: table-cell;
  flex: 1 1 22%;
  margin: 1px;
  word-wrap: break-word;
}
h1,
footer {
  margin: 1px;
  padding: 5px;
  background: orange
}
<link href="https://v4-alpha.getbootstrap.com/dist/css/bootstrap.min.css" rel="stylesheet"/>
<div class='flex-vertical'>
  <h1>test</h1>
  <Table class='flags-table flex-vertical' responsive hover>
    <thead>
      <tr class="d-flex">
        <th></th>
        <th>Time In</th>
        <th>Time Out</th>
        <th>Type</th>
        <th>Category</th>
      </tr>
    </thead>
    <tbody>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
      <tr class="d-flex">
        <td>
          <div class='red-box'></div>
        </td>
        <td>{this.secondsToHms(x.time)}</td>
        <td>{this.secondsToHms(x.stopTime)}</td>
        <td>{x.tagname}</td>
        <td contentEditable="false">{x.category}</td>
      </tr>
    </tbody>
  </Table>
  <footer>
    footer
    </footer
Community
  • 1
  • 1
G-Cyrillus
  • 101,410
  • 14
  • 105
  • 129