bootstrap表格头部的固定方法:首先引入jquery和bootstrap;然后添加固定列代码为“ $("#table").bootstrapTable('destroy').bootstrapTable({...})”即可。

推荐:《bootstrap教程》
bootstrap-table固定表头固定列
1.引入
bootstrap依赖于jquery
bootstrap-table依赖于bootstrap,所以都需要引入

2. bootstrap-table有两种方式,html、js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | <table id= "table" class = "table table-bordered table-hover"
data-toggle= "table"
data-classes= "table table-hover"
data-show-columns= "true"
data-striped= "true"
data-show-toggle= "true"
data-search= "true"
data-show-refresh= "true"
data-toolbar= "#toolbar"
data-height= "500" >
<thead>
<tr>
<th>表格 ID</th>
<th>表格 Name</th>
<th>表格 Price</th>
<th>表格 Price</th>
<th>表格 Price</th>
<th>表格 Price</th>
<th>表格 Price</th>
<th>表格 Price</th>
<th>表格 Price</th>
<th>表格 Price</th>
<th>表格 Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>Item 1</td>
<td> $1 </td>
<td>Item 1</td>
<td> $1 </td>
<td>Item 1</td>
<td> $1 </td>
<td>Item 1</td>
<td> $1 </td>
<td>Item 1</td>
<td> $1 </td>
</tr>
<tr>
<td>2</td>
<td>Item 2</td>
<td> $2 </td>
<td>Item 1</td>
<td> $1 </td>
<td>Item 1</td>
<td> $1 </td>
<td>Item 1</td>
<td> $1 </td>
<td>Item 1</td>
<td> $1 </td>
</tr>
</table>
|
Copier après la connexion
js方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <table id= "table" ></table><script>
$( "#table" ).bootstrapTable({
toolbar: "#toolbar" ,
striped: true,
sortable: false,
field: 'id',
title: 'Item ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}],
id: 1,
name: 'Item 1',
price: ' $1 '
}, {
id: 2,
name: 'Item 2',
price: ' $2 '
}, {
id: 3,
name: 'Item 3',
price: ' $3 '
}, {
id: 4,
name: 'Item 4',
price: ' $4 '
}, {
id: 5,
name: 'Item 5',
price: ' $5 '
}, {
id: 6,
name: 'Item 6',
price: ' $6 '
}, {
id: 7,
name: 'Item 7',
price: ' $7 '
}, {
id: 8,
name: 'Item 8',
price: ' $8 '
}, {
id: 9,
name: 'Item 9',
price: ' $9 '
}, {
id: 10,
name: 'Item 10',
price: ' $10 '
}]
})</script>
|
Copier après la connexion
固定列代码
1 2 3 4 | $( "#table" ).bootstrapTable('destroy').bootstrapTable({
fixedColumns: true,
fixedNumber: 1
}
|
Copier après la connexion
效果展示:

3.问题解决
固定表头展示错位
解决办法:给 th 添加宽度 data-width="60px"
固定列也会错位
解决办法:所有内容不折行,展示在一行(感觉应该是line-height导致的差异)
固定表头固定列重叠的表头部分左右滚动的时候 没有固定
解决办法:重叠部分手动加了层级
当浏览器窗口变化是,表头与表格不对齐,应该怎么办?
1 2 3 4 5 | $('#tableId').bootstrapTable();
$(window).resize( function () {
$('#tableId').bootstrapTable('resetView');
});
|
Copier après la connexion
4.下载地址
bootstrap-table:http://bootstrap-table.wenzhixin.net.cn/zh-cn/
bootstrap-table-fixed-columns:https://github.com/wenzhixin/bootstrap-table-fixed-columns
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!