需要的文件下載:
bootstrap-table:https://github.com/wenzhixin/bootstrap-table
bootstrap-table-fiex-column:https://github.com/wenzhixin/bootstrap-table-fixed-columns
參考來源:https://www.cnblogs.com/Kyaya/p/9004237.html
1.引入文件

2. bootstrap-table有兩種方式,html、js
<table id="table" class="table table-bordered table-hover"
data-toggle="table" //啟用bootstrap表格
data-classes="table table-hover"
data-show-columns="true" //是否顯示內容列下拉框。
data-striped="true" //設置為 <code>true</code> 會有隔行變色效果
data-show-toggle="true" //是否顯示切換視圖(table/card)按鈕。
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>
js方式
<table id="table"></table>
<script>
$("#table").bootstrapTable({
toolbar: "#toolbar",
striped: true, //是否顯示行間隔色
height:300,
sortable: false,//是否排序
search: true, //是否顯示表格搜索,此搜索是客戶端搜索,不會進服務端
strictSearch: true, //是否顯示刷新
showColumns: true, //是否顯示所有的列
showRefresh: true, //是否顯示刷新按鈕
minimumCountColumns: 2, //最少允許的列數
showToggle:true, //是否顯示詳細視圖和列表視圖的切換按鈕
cardView: false, //是否顯示詳細視圖
columns: [{
field: 'id',
title: 'Item ID'
}, {
field: 'name',
title: 'Item Name'
}, {
field: 'price',
title: 'Item Price'
}],
// data可以換成url
data: [{
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>
3.問題解決
固定表頭展示錯位
解決辦法:給 th 添加寬度 data-width="60px"
固定列也會錯位
解決辦法:所有內容不折行,展示在一行(感覺應該是line-height導致的差異)
固定表頭固定列重疊的表頭部分左右滾動的時候 沒有固定
解決辦法:重疊部分手動加了層級
當瀏覽器窗口變化是,表頭與表格不對齊,應該怎么辦?
$('#tableId').bootstrapTable(); // init via javascript
$(window).resize(function () {
$('#tableId').bootstrapTable('resetView');
});
