これは、この同じ問題に対する私の解決策のデモのフィドルです。必要な列を修正できます。テーブルに「fixed-columns」クラスを追加し、固定したいthとtdに「fixed-column」を追加するだけです。次に、responseTables.init()を呼び出して残りを実行します。JSFイベントとの互換性を維持するために、元のテーブルのIDを変更することを選択しました。
HTMLは次のとおりです。
<div class="table-responsive">
<table class="table table-striped table-bordered table-hover fixed-columns">
<thead>
<tr>
<th class="fixed-column">#</th>
...
</tr>
</thead>
<tbody>
<tr>
<td class="fixed-column">1</td>
...
</tr>
...
</tbody>
</table>
</div>
これがJavascriptです:
var responsiveTables = {
init: function() {
$(document).find('.fixed-columns').each(function (i, elem) {
responsiveTables.fixColumns(elem);
});
},
fixColumns: function(table, columns) {
var $table = $(table);
$table.removeClass('fixed-columns');
var $fixedColumns = $table.clone().attr('id', $table.attr('id') + '-fixed').insertBefore($table).addClass('fixed-columns-fixed');
$fixedColumns.find('*').each(function (i, elem) {
if ($(this).attr('id') !== undefined) {
$table.find("[id='" + $(this).attr("id") + "']").attr('id', $(this).attr('id') + '-hidden');
}
if ($(this).attr('name') !== undefined) {
$table.find("[name='" + $(this).attr("name") + "']").attr('name', $(this).attr('name') + '-hidden');
}
});
if (columns !== undefined) {
$fixedColumns.find('tr').each(function (x, elem) {
$(elem).find('th,td').each(function (i, elem) {
if (i >= columns) {
$(elem).remove();
}
});
});
} else {
$fixedColumns.find('tr').each(function (x, elem) {
$(elem).find('th,td').each(function (i, elem) {
if (!$(elem).hasClass('fixed-column')) {
$(elem).remove();
}
});
});
}
$fixedColumns.find('tr').each(function (i, elem) {
$(this).height($table.find('tr:eq(' + i + ')').height());
});
}
};
responsiveTables.init();
そしてCSS:
.table-responsive > .fixed-columns-fixed {
position: absolute;
display: inline-block;
width: auto;
border-right: 2px solid #ddd;
background-color: #fff;
}