bootstrap Table表头固定

本文介绍了使用BootstrapTable时如何实现表头固定的功能,并解决了一些常见问题,如表头和内容不对齐等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  • 简介

bootstrap Table的使用方法不多介绍,可参考官方说明文档,地址:http://bootstrap-table.wenzhixin.net.cn/documentation/

  • 表头固定方法

  1. 设置height属性值。这种方法有个弊端:因为table的height是固定了,当浏览器的高度小于table的高度时,table的部分数据显示不出来;这种还要去动态算table的高度
  2. 用插件。插件名称是jquery.michiweber.table-head-fixed.js (源码见文章末尾),原理:给bootstrap Table添加了一个thread和原来的thread内容一样,样式有差异,当没有滚动时显示旧的thread,当滚动后隐藏旧的thread,显示后添加的thread。插件使用方法如下:
  • 引入样式:
 table.table-fixed-head .table-fixed-head-thead {
            border-bottom: 1px solid #dddddd;
            border-top: 1px solid #dddddd;
            background: #fff;
        };


  • 引入插件js(jquery.michiweber.table-head-fixed.js);
  • table标签加: class="table table-striped table-fixed-head" data-table-fixed-head-trigger="20" data-table-fixed-head-top="90";其中data-table-fixed-head-trigger指的是滚动高度大于这个值时隐藏旧的thread,显示后添加的thread;data-table-fixed-head-top指的是距离顶部的距离(这个值需要自己手动修改);

  • 问题

问题1:   当table为div的子元素,table没有设置高度(即100%),div有滚动条时,当你滚动时没有效果,看过jquery.michiweber.table-head-fixed.js这个源码你就明白:

div的滚动不会触发上面我截图中红色区域那块代码。

解决方案:自己给添加table的父元素div添加滚动事件(代码参考原来代码scroll函数)

问题2:表头和内容不对齐,个人建议两个简单方案:

  1. 在 this.build函数设置css时候添加'border': 0(即没有边框,就不会有这种问题了);
  2. 自己添加滚动函数时候重新调用这两个函数(table.tfh('kill');    table.tfh(options);)让重新生成一下表头,注意:这两个方法运行的次数不要过于频繁,否则浏览器就崩掉了。                    

  • 源码

(function($){
	$.fn.tfh = function(){

		var method = (arguments.length === 2) ? arguments[0] : ((arguments.length === 1 && typeof arguments[0] === 'string' ? arguments[0] : undefined));
		var options = $.extend({
			trigger: 0,
			top: 0
		},(arguments.length === 2) ? arguments[1] : ((arguments.length === 1 && typeof arguments[0] === 'object' ? arguments[0] : {} )));

		this.width = function(){
			return this.find('thead').attr('data-tmp-width',parseInt(this.find('thead').css('width'))).find('*').each(function(){
				$(this).attr('data-tmp-width',parseInt($(this).css('width')));
			}).end().end();
		};

		this.fix = function(){
			return this.find('.table-fixed-head-thead').css({
				'top': options.top + 'px',
				'position': 'fixed'
			}).end();
		};

		this.clone = function(){
			return this.find('thead').clone(true).prependTo(this).addClass('table-fixed-head-thead').end().end().removeAttr('data-tmp-width').find('*').removeAttr('data-tmp-width').end().end();
		};

		this.build = function(){
			return this.tfh('width').tfh('clone').find('[data-tmp-width]').each(function(){
				$(this).css({
					'width': $(this).data('tmp-width') + 'px',
					'minWidth': $(this).data('tmp-width') + 'px',
					'maxWidth': $(this).data('tmp-width') + 'px',
				});
			}).removeAttr('data-tmp-width').end().tfh('fix', options);
		};

		this.kill = function(){
			this.find('.table-fixed-head-thead').remove();
		};

		this.show = function(){
			return this.addClass('fixed').find('thead').css('visibility','visible').not('.table-fixed-head-thead').css('visibility','hidden').end().end();
		};

		this.hide = function(){
			return this.removeClass('fixed').find('thead').css('visibility','hidden').not('.table-fixed-head-thead').css('visibility','visible').end().end();
		};

		if(method !== undefined){
			return this[method].call($(this));
		} else {
			var table = this.build.call($(this),options);
			var tableWidth = table.css('width');
			var tableScrollLeft = table.position().left;

			if($(document).scrollTop() > options.trigger) {
				table.tfh('show');
			} else {
				table.tfh('hide');
			}

			var resizeTimer;
			$(window).resize(function(){
				window.clearInterval(resizeTimer);
				resizeTimer = window.setInterval(function() {
					window.clearInterval(resizeTimer);
					if(tableWidth !== table.css('width')) {
						tableWidth = table.css('width');
						table.tfh('kill');
						table.tfh(options);
					}
				}, 1000);
			}).scroll(function(){
				if($(document).scrollTop() > options.trigger) {
					table.tfh('show');
					table.find('.table-fixed-head-thead').css('left',(tableScrollLeft - $(document).scrollLeft()) + 'px');
				} else {
					table.tfh('hide');
				}
			});
		}
	}
	$(document).ready(function(){
		$('table.table-fixed-head').each(function(){
			$(this).tfh({
				trigger: ($(this).data('table-fixed-head-trigger') !== undefined ? $(this).data('table-fixed-head-trigger') : 0),
				top: ($(this).data('table-fixed-head-top') !== undefined ? $(this).data('table-fixed-head-top') : $(this).position().top)
			});
		});
	});
}(jQuery));


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>行1列1</td> <td>行1列2</td> <td>行1列3</td> </tr> <tr> <td>行2列1</td> <td>行2列2</td> <td>行2列3</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>行1列1</td> <td>行1列2</td> <td>行1列3</td> </tr> <tr> <td>行2列1</td> <td>行2列2</td> <td>行2列3</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> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值