Fix First Column of a Bootstrap Table
Ok.. Remove all your js
code and you can do this with some CSS tricks as below:
DEMO
CSS
.table > thead:first-child > tr:first-child > th:first-child {
position: absolute;
display: inline-block;
background-color: red;
height: 100%;
}
.table > tbody > tr > td:first-child {
position: absolute;
display: inline-block;
background-color: red;
height: 100%;
}
.table > thead:first-child > tr:first-child > th:nth-child(2) {
padding-left: 40px;
}
.table > tbody > tr > td:nth-child(2) {
padding-left: 50px !important;
}
Bootstrap4 Responsive Table - Fixed first column
table { font-family: arial, sans-serif; border-collapse: collapse; width: 100%;}
td, th { border: 1px solid #dddddd; text-align: left; padding: 8px;}th{background-color:black;color:white;}th:first-child, td:first-child{ position:sticky; left:0px; } td:first-child { background-color:grey; }
<table> <tr> <th>TIME</th> <th>Company</th> <th>Company</th> <th>Company</th> <th>Company</th> <th>Company</th> <th>Company</th> <th>Contact</th> <th>Country</th> </tr> <tr> <td>11:40 </td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Germany</td> </tr> <tr> <td>11:40 </td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Centro comercial Moctezuma</td> <td>Francisco Chang</td> <td>Mexico</td> </tr> <tr> <td>11:40 </td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Ernst Handel</td> <td>Roland Mendel</td> <td>Austria</td> </tr> <tr> <td>11:40 </td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Island Trading</td> <td>Helen Bennett</td> <td>UK</td> </tr> <tr> <td>11:40 </td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Laughing Bacchus Winecellars</td> <td>Yoshi Tannamuri</td> <td>Canada</td> </tr> <tr> <td>11:40 </td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Alfreds Futterkiste</td> <td>Maria Anders</td> <td>Magazzini Alimentari Riuniti</td> <td>Giovanni Rovelli</td> <td>Italy</td> </tr></table>
Can I lock the first column in a Twitter Bootstrap Table?
Here's a demo of a possible solution, based on my earlier comment:
DEMO: Fixed Column on Bootstrap Table
Note that this isn't really tested or even a complete solution for fixing a column, but rather a proof of concept for you to build on.
Here is the relevant markup. This example uses a striped, bordered, condensed Bootstrap table
<div id="scroller">
<table class="table table-striped table-bordered table-condensed">
<thead>
...
</thead>
<tbody>
...
</tbody>
</table>
</div>
And the required jQuery:
$('#scroller table').each(function(){
var table = $(this),
fixedCol = table.clone(true),
fixedWidth = table.find('th').eq(0).width(),
tablePos = table.position();
// Remove all but the first column from the cloned table
fixedCol.find('th').not(':eq(0)').remove();
fixedCol.find('tbody tr').each(function(){
$(this).find('td').not(':eq(0)').remove();
});
// Set positioning so that cloned table overlays
// first column of original table
fixedCol.addClass('fixedCol');
fixedCol.css({
left: tablePos.left,
top: tablePos.top
});
// Match column width with that of original table
fixedCol.find('th,td').css('width',fixedWidth+'px');
$('#scroller').append(fixedCol);
});
and the needed CSS:
#scroller {
width: 300px;
overflow-x: scroll;
}
#scroller table {
/* just a quick hack to make the table overflow the containing div
your method may differ */
width: 600px;
}
#scroller .table.fixedCol {
width: auto;
position: absolute;
/* below styles are specific for borderd Bootstrap tables
to remove rounded corners on cloned table */
-webkit-border-top-right-radius: 0px;
-webkit-border-bottom-right-radius: 0px;
-moz-border-radius-topright: 0px;
-moz-border-radius-bottomright: 0px;
border-top-right-radius: 0px;
border-bottom-right-radius: 0px;
}
.table.fixedCol th,
.table.fixedCol td {
/* background is set to white to hide underlaying column
of original table */
background: white;
}
How do I create an HTML table with a fixed/frozen left column and a scrollable body?
If you want a table where only the columns scroll horizontally, you can position: absolute
the first column (and specify its width explicitly), and then wrap the entire table in an overflow-x: scroll
block. Don't bother trying this in IE7, however...
Relevant HTML & CSS:
table {
border-collapse: separate;
border-spacing: 0;
border-top: 1px solid grey;
}
td,
th {
margin: 0;
border: 1px solid grey;
white-space: nowrap;
border-top-width: 0px;
}
div {
width: 500px;
overflow-x: scroll;
margin-left: 5em;
overflow-y: visible;
padding: 0;
}
.headcol {
position: absolute;
width: 5em;
left: 0;
top: auto;
border-top-width: 1px;
/*only relevant for first row*/
margin-top: -1px;
/*compensate for top border*/
}
.headcol:before {
content: 'Row ';
}
.long {
background: yellow;
letter-spacing: 1em;
}
<div>
<table>
<tr>
<th class="headcol">1</th>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
</tr>
<tr>
<th class="headcol">2</th>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
</tr>
<tr>
<th class="headcol">3</th>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
</tr>
<tr>
<th class="headcol">4</th>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
</tr>
<tr>
<th class="headcol">5</th>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
</tr>
<tr>
<th class="headcol">6</th>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
<td class="long">QWERTYUIOPASDFGHJKLZXCVBNM</td>
</tr>
</table>
</div>
bootstrap 3 responsive table with fixed first column
If you're only targeting mobile devices then this may work for you: you can clone the first column in your table and apply position:absolute
so it appears "in front" when you scroll the rest of the table.
For this you'd need some basic jquery code and a custom CSS class:
jQuery
$(function(){
var $table = $('.table');
//Make a clone of our table
var $fixedColumn = $table.clone().insertBefore($table).addClass('fixed-column');
//Remove everything except for first column
$fixedColumn.find('th:not(:first-child),td:not(:first-child)').remove();
//Match the height of the rows to that of the original table's
$fixedColumn.find('tr').each(function (i, elem) {
$(this).height($table.find('tr:eq(' + i + ')').height());
});
});
CSS
.table-responsive>.fixed-column {
position: absolute;
display: inline-block;
width: auto;
border-right: 1px solid #ddd;
background-color: #fff; /* bootstrap v3 fix for fixed column background color*/
}
@media(min-width:768px) {
.table-responsive>.fixed-column {
display: none;
}
}
Here's a working demo for this approach
Related Topics
How Is It Possible for an Iframe to Access Its Parents Dom
Center an Image Vertically and Horizontally Using CSS
Transitionend Event Fires Twice
How to Show Just the First Line of Text of a Div and Expand on Click
Is There an Alternative Method to Use Onbeforeunload in Mobile Safari
Uncaught Syntaxerror: Unexpected Token :
How to Initialize an Array's Length in JavaScript
Google Map API Uncaught Typeerror: Cannot Read Property 'Offsetwidth' of Null
Jquery Jump or Scroll to Certain Position, Div or Target on the Page from Button Onclick
Getelementsbyclassname to Change the Style of Elements When Event Occurs
Toggle Divs Without Using JavaScript
Fcm - Programmatically Send Push Notification to User Segments
Vue 2 - Mutating Props Vue-Warn