bootstraptable固定表头和列

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" type="text/css" href="../bootstrap.min.css">
    <link rel="stylesheet" type="text/css" href="../bootstrap-table.css">
    <!--固定列需要引用此css文件-->
    <link rel="stylesheet" type="text/css" href="../bootstrap-table-fixed-columns.css">

    <script type="text/javascript" src="../jquery.js"></script>
    <script type="text/javascript" src="..//bootstrap.js"></script>
    <script type="text/javascript" src="../bootstrapTable/bootstrap-table.js"></script>
    <script type="text/javascript" src="../bootstrap-table-zh-CN.js"></script>
    <!--固定列需要引用此js文件-->
    <script type="text/javascript" src="../bootstrap-table-fixed-columns.js"></script>
    <style>
        body{
            width: 100%;
        }
        //固定表头样式
        #goodsList {
            padding: 0;
            height: 400px;
            overflow-y: scroll;//重要!
        }
        .fixTable thead {
            background-color: #fff;
        }



        //对于复杂的表头,固定列的时候需要调整样式,具体数值根据实际定
        /*固定第一列样式控制start*/
        #detailTableContainer .fixed-table-header-columns{
            height: 121px !important;
            top: 42px !important;
            display: block !important;
            background: #eeeeee !important;
        }
        #detailTableContainer .fixed-table-body-columns{
            top: 163px !important;
            height: 770px !important;
            width: 73px !important;

        }
        /*固定第一列样式控制end*/
    </style>
</head>
<body>
<!--固定的表头相对于此div绝对定位,故此div要position: relative相对定位-->
<div id="goodsList"  style="position: relative;width: 100%">
    <!--固定的表头跟内容对不齐的解决方法-重要代码1-->
    <!--style="table-layout: fixed;word-break:break-all;"
    table-layout:fixed 的作用是指定每列标题头的宽度
    word-break:break-all,如果单元格内的数据在被列宽限制的情况下会换行显示
    确定了每列的宽度后方便调整固定表头跟内容对齐-->
<table id="textTable"  class="table table-bordered scrollTable" style="table-layout: fixed;word-break:break-all;">
</table>
</div>
<script>
    $(function(){
        var  Columns = [
            //注意:不要有下面注释掉的这一个大表头,这样的话每列的宽度就控制不了了,
            //不知道原因,实践得来的
            // [
            //     {
            //         title: 公园绿地明细表',
            //         align: 'center',
            //         colspan: 7,
            //     }
            // ],
            [
                {
                    field: 'F3',
                    title: '合计',
                    halign:'center',
                    align:'center',
                    rowspan: 2,
                    //固定的表头跟内容对不齐的解决方法-重要代码2:width:100,
//注意后面的每列都要有width,取值建议是50的整倍数(实践经验),
//因为最终每列的宽度并不是自己写的值,而是按照倍数均分的宽度。
                    width:100
                },{
                title: '公园面积',
                halign:'center',
                align:'center',
                colspan:4,
                width:400
            },{
                field: 'F9',
                title: '山体公园',
                halign:'center',
                align:'center',
                rowspan: 2,
                width:100
            },{
                field: 'F10',
                title: '广场用地',
                halign:'center',
                align:'center',
                rowspan: 2,
                width:100
            }
            ],[
                {
                    field: 'F11',
                    title: '合计',
                    halign:'center',
                    align:'center',
                    width:100
                },{
                    field: 'F12',
                    title: '绿地面积',
                    halign:'center',
                    align:'center',
                    width:100
                },{
                    field: 'F13',
                    title: '建筑占地面积',
                    halign:'center',
                    align:'center',
                    width:100
                },{
                    field: 'F14',
                    title: '其他占地面积',
                    halign:'center',
                    align:'center',
                    width:100
                }
            ]


        ];
        var TableData = [
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
            {'F3':123,'F9':152,'F10':5556,'F11':6523,'F12':7812,'F13':1753,'F14':9526},
        ];
        $('#textTable').bootstrapTable('destroy').bootstrapTable({
            columns:Columns,
            data: TableData,
            striped:true,
            search:true, //搜索框
            pagination:false,  //设置为 true 会在表格底部显示分页条。
            //固定第一列代码
            // fixedColumns: true,
            // fixedNumber: 1 //固定列数
        });

        var $fixTable = $('#goodsList .fixTable');
        $('#goodsList').scroll(function() {
            var id = '#' + this.id;
            var scrollTop = $(id).scrollTop() || $(id).get(0).scrollTop,
                style = {
                    // 固定的表头跟内容对不齐的解决方法-重要代码3:通过width,left等参数调整固定表头的位置,使其与内容对齐
                    'position': 'absolute',
                    'left': '0',
                    'right': '0',
                    'top': scrollTop + 'px',
                    'width':'100%'
                };
            if ($fixTable.length) {
                (scrollTop === 0) ? $fixTable.addClass('hidden') : $fixTable.removeClass('hidden');
                $fixTable.css(style);
            } else {
                var html = $(id + ' .scrollTable thead').get(0).innerHTML;
                var table = $('<table class="table table-bordered fixTable"><thead>' + html + '</thead></table>');
                table.css(style);
                $(id).append(table);
                $fixTable = $(this).find('.fixTable');
            }
        });
    })
</script>

</body>
</html>

参考:https://segmentfault.com/a/1190000004713875?_ea=701754

bootstrap-table-fixed-columns.js、bootstrap-table-fixed-columns.css下载地址:https://github.com/wenzhixin/bootstrap-table-fixed-columns
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> ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值