bootstrap table 表头固定 、冻结列、横向纵向滚动条

前提:引入对应的js,css

<link rel="stylesheet" type="text/css" href="/kaoqin/js/bootstrap/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/kaoqin/js/bootstrap-table/bootstrap-table.min.css">
<link rel="stylesheet" type="text/css" href="/kaoqin/js/bootstrap-table/bootstrap-table-fixed-columns.css">
<script src="/kaoqin/js/bootstrap/js/bootstrap.min.js"></script>
<script src="/kaoqin/js/bootstrap-table/bootstrap-table.min.js"></script>
<script src="/kaoqin/js/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="/kaoqin/js/bootstrap-table/bootstrap-table-fixed-columns.js"></script>

1 添加样式,设置列宽由表格宽度和列宽度设定

.table{
    table-layout: fixed;
}

2 bootstrapTable初始化的时候 field设置宽度

{ field : '', title : '' ,align: 'center', valign: 'middle',width:120}

3 bootstrapTable初始化的时候 height设置高度(表头设置有两种方法,这里使用的是在bootstrapTble设置height ,这种方法有缺陷,表格高度固定,这就需要写额外的js去动态设置table的高度)

var h = $(window).height() - 100;
$('#table').bootstrapTable("destroy")
    .bootstrapTable(
        {
            method : 'get', // 服务器数据的请求方式 get or post
            url : "/attendance/record/recordList", // 服务器数据的加载地址
            height:h,
 

    tips:动态设置高度可以使用 $(window).resize();

4 bootstrapTable初始化的时候 设置fixedColumns 和fixedNumber

fixedColumns: true,
fixedNumber: 5

5 table 标签外的div 添加class=table-responsive

<div class="table-responsive" style="z-index: 1;">
   <table class="table" id="table" style="min-width:100px;"></table>
</div>

6 动态设置冻结列的高度(通过看fix-columns.js源码可以知道,冻结列是通过在table前加div,并是div置于table对应的div之上来实现的)

$(window).resize(function () {
    var h = $(window).height();
    var w = $(window).width();
    var $fixedTableBody = $("#table").closest("div"),
        $fixedTableBodyColumns = $fixedTableBody.prev(),
        $FixedtableContainer = $fixedTableBody.closest("div");
    $FixedtableContainer.height(h - 200);
    $fixedTableBodyColumns.height(h - 253);
});

最后贴出部分代码(bootstarpTalbe初始)

    var columns = [];
    var t1 = { field : 'name', title : '姓名' ,align: 'center', valign: 'middle',width:120};
    var t2 = { field : 'username', title : '账号' ,align: 'center', valign: 'middle',width:120};
    var t3 = { field : 'deptName', title : '部门' ,align: 'center', valign: 'middle',width:120};
    var t4 = { field : 'groupName', title : '考勤组' ,align: 'center', valign: 'middle',width:120};
    columns.push(t1);columns.push(t2);columns.push(t3);columns.push(t4);
    var h = $(window).height() - 100;
    $('#table').bootstrapTable("destroy")
        .bootstrapTable(
            {
                method : 'get', // 服务器数据的请求方式 get or post
                url : "/attendance/record/recordList", // 服务器数据的加载地址
                height:h,
                iconSize : 'outline',
                striped : true, // 设置为true会有隔行变色效果
                dataType : "json", // 服务器返回的数据类型
                pagination : true, // 设置为true会在底部显示分页条
                // queryParamsType : "limit",
                // //设置为limit则会发送符合RESTFull格式的参数
                singleSelect : false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageList: [ 5, 10, 20],
                pageSize : 5, // 如果设置了分页,每页数据条数
                pageNumber : 1, // 如果设置了分布,首页页码
                //search : true, // 是否显示搜索框
                showColumns : false, // 是否显示内容下拉框(选择显示的列)
                sidePagination : "server", // 设置在哪里进行分页,可选值为"client" 或者 "server"
                queryParams : function(params) {
                    return {
                        //说明:传入后台的参数包括offset开始索引,limit步长,sort排序列,order:desc或者,以及所有列的键值对
                        limit: params.limit,
                        offset:params.offset
                    };
                },
                columns:columns,
                fixedColumns: true,
                fixedNumber: 5
            });
    $("#table").colResizable();
};

  • 2
    点赞
  • 41
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Bootstrap 中,可以使用固定表头滚动表体的插件来实现这个效果。以下是实现步骤: 1. 引入必要的文件: ``` <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap/4.3.1/css/bootstrap.min.css"> <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/bootstrap/4.3.1/js/bootstrap.min.js"></script> ``` 2. 创建一个表格,并在表格头部添加 `thead` 标签: ``` <table class="table table-bordered table-striped"> <thead> <tr> <th>头1</th> <th>头2</th> <th>头3</th> </tr> </thead> <tbody> <tr> <td>行11</td> <td>行12</td> <td>行13</td> </tr> <tr> <td>行21</td> <td>行22</td> <td>行23</td> </tr> <!-- more rows --> </tbody> </table> ``` 3. 使用 JavaScript 初始化表格,并调用插件: ``` $(function() { $('.table').fixedHeaderTable({ fixedColumn: true }); }); ``` 其中,`fixedHeaderTable` 是插件名称,`fixedColumn: true` 表示需要固定表头。 4. 根据需要调整样式: ``` .table-fixed-header { overflow-y: auto; max-height: 400px; } .table-fixed-header thead th, .table-fixed-header tbody td { width: 100px; min-width: 100px; max-width: 100px; } ``` 其中,`.table-fixed-header` 是插件自动生成的类名,可以根据需要进行修改。 完整代码如下: ``` <!DOCTYPE html> <html> <head> <title>Bootstrap Table 表头固定表体滚动</title> <meta charset="utf-8"> <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap/4.3.1/css/bootstrap.min.css"> <style> .table-fixed-header { overflow-y: auto; max-height: 400px; } .table-fixed-header thead th, .table-fixed-header tbody td { width: 100px; min-width: 100px; max-width: 100px; } </style> </head> <body> <div class="container"> <table class="table table-bordered table-striped table-fixed-header"> <thead> <tr> <th>头1</th> <th>头2</th> <th>头3</th> </tr> </thead> <tbody> <tr> <td>行11</td> <td>行12</td> <td>行13</td> </tr> <tr> <td>行21</td> <td>行22</td> <td>行23</td> </tr> <!-- more rows --> </tbody> </table> </div> <script src="https://cdn.staticfile.org/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/popper.js/1.14.7/umd/popper.min.js"></script> <script src="https://cdn.staticfile.org/bootstrap/4.3.1/js/bootstrap.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/fixed-header-table@1.3.2/dist/fixed-header-table.min.js"></script> <script> $(function() { $('.table').fixedHeaderTable({ fixedColumn: true }); }); </script> </body> </html> ```
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值