I have a large scroll-able table I built with Twitter bootstrap but would like prevent the first column from scrolling. Is this possible?
<div class="row">
<div class="span12" style="height: auto !important;overflow-x: scroll;">';
<table class="table table-striped table-bordered table-condensed">
<thead>
<th>Col1</th>
<th>Col2</th>
<th>Col3</th>
</thead>
<tbody>
<tr>
<td>data 1</td>
<td>data 2</td>
<td>data 3</td>
</tr>
</tbody>
</table>
</div>
</div>
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With