td不允许自己扩展_JS组件系列——自己动手扩展BootstrapTable的 冻结列 功能:彻底解决高度问题...

前言:一年前,博主分享过一篇关于bootstrapTable组件冻结列的解决方案 JS组件系列——Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案 ,通过该篇,确实可以实现bootstrapTable的冻结列效果,并且可以兼容ie浏览器。这一年的时间,不断有园友以及群里面的朋友问过我关于固定高度之后,冻结列页面效果不能对齐的问题,奈何博主太忙,一直没有抽空将这个问题优化。最...
摘要由CSDN通过智能技术生成

前言:一年前,博主分享过一篇关于bootstrapTable组件冻结列的解决方案 JS组件系列——Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案 ,通过该篇,确实可以实现bootstrapTable的冻结列效果,并且可以兼容ie浏览器。这一年的时间,不断有园友以及群里面的朋友问过我关于固定高度之后,冻结列页面效果不能对齐的问题,奈何博主太忙,一直没有抽空将这个问题优化。最近项目里面也不断有人提过这个bug,这下子不能再推了,必须要直面“惨淡的bug”,于是昨天利用一天的时间将原来的扩展做了一下修改,能够完美解决固定高度之后冻结列的问题,并且,博主还加了一些特性,比如右侧列的冻结、冻结列的选中等等,有需要的朋友可以捧个场。相信通过此篇,老板再也不用担心我的冻结列不能固定高度了~~

一、问题追踪

记得在之前的那篇里面介绍过,bootstrapTable组件自带的冻结列扩展,不能兼容ie浏览器,即使最新版本的ie也会无法使用,这是一般的系统不能忍受的,所以在那篇里面给出过解决方案,但并未分析ie浏览器不能兼容的原因,昨天博主花了点时间特意调试了下源码,原来在ie里面,使用jquery的clone()方法和谷歌等浏览器有所区别。为了展示这个区别,这里先抛个砖。比如有如下代码:

aaa bbb ccc
ddd eee fff
ggg hhh iii

var $tr = $('#tbtest tr:eq(0)').clone();

var $tds = $tr.find('td');

$tr.html('');

alert($tds.eq(0).html());

代码本身很简单,只是为了测试用。看到这里你可以试着猜一下alert的结果。

算了,不考大家了,直接贴出来吧,有图有真相!

相信不用我过多的解释哪个是ie,哪个是谷歌了吧。

两者的区别很明显,谷歌里面得到“aaa”,而ie里面得到空字符串。这是为什么呢?

其实如果你用值类型和引用类型的区别来解释这个差别你就不难理解了,在谷歌浏览器里面,$tr变量是一个引用类型,当你清空了它里面的内容,只是清除了$tr这个变量的“指针”,或者叫指向,$tds变量仍然指向了$tr的原始内容,所以调用$tds.eq(0).html()的时候仍然能得到结果aaa;同样的代码在ie浏览器里面,$tr变量就是一个值类型,你清空了它里面的内容之后,$tds的内容也被清空了。如果你有更好的解释,欢迎赐教哈。

之所以组件原生的js不能兼容ie浏览器,就是因为它使用了clone()这个方法,导致在不同的浏览器看到不同的结果。相信bootstrapTable组件的作者应该是知道这个区别的,只不过没有太在意这些,从作者做的很多功能的兼容性能够看出,他做的功能很多没有太多的考虑ie浏览器的效果。

二、效果预览

还是老规矩,说了这个多,没图怎么行,小二,上图!

没有固定高度的情况:单列冻结。

多列冻结。

固定任意高度效果

ie浏览器也没有问题,这里就不再重复上图了。

三、源码解析

源码没啥说的,有兴趣可以自己看看,主要的原理还是重写bootstrapTable构造器的事件,来达到想要的效果。

(function ($) {

'use strict';

$.extend($.fn.bootstrapTable.defaults, {

fixedColumns: false,

fixedNumber: 1

});

var BootstrapTable = $.fn.bootstrapTable.Constructor,

_initHeader = BootstrapTable.prototype.initHeader,

_initBody = BootstrapTable.prototype.initBody,

_resetView = BootstrapTable.prototype.resetView;

BootstrapTable.prototype.initFixedColumns = function () {

this.$fixedHeader = $([

'

',

'

'',

'

',

'

'].join(''));

this.timeoutHeaderColumns_ = 0;

this.$fixedHeader.find('table').attr('class', this.$el.attr('class'));

this.$fixedHeaderColumns = this.$fixedHeader.find('thead');

this.$tableHeader.before(this.$fixedHeader);

this.$fixedBody = $([

'

',

'

'

',

'

',

'

'].join(''));

this.timeoutBodyColumns_ = 0;

this.$fixedBody.find('table').attr('class', this.$el.attr('class'));

this.$fixedBodyColumns = this.$fixedBody.find('tbody');

this.$tableBody.before(this.$fixedBody);

};

BootstrapTable.prototype.initHeader = function () {

_initHeader.apply(this, Array.prototype.slice.apply(arguments));

if (!this.options.fixedColumns) {

return;

}

this.initFixedColumns();

var that = this, $trs = this.$header.find('tr').clone();

$trs.each(function () {

$(this).find('th:gt(' + (that.options.fixedNumber - 1) + ')').remove();

});

this.$fixedHeaderColumns.html('').append($trs);

};

BootstrapTable.prototype.initBody = function () {

_initBody.apply(this, Array.prototype.slice.apply(arguments));

if (!this.options.fixedColumns) {

return;

}

var that = this,

rowspan = 0;

this.$fixedBodyColumns.html('');

this.$body.find('> tr[data-index]').each(function () {

var $tr = $(this).clone(),

$tds = $tr.find('td');

//$tr.html('');这样存在一个兼容性问题,在IE浏览器里面,清空tr,$tds的值也会被清空。 //$tr.html(''); var $newtr = $('

');

$newtr.attr('data-index', $tr.attr('data-index'));

$newtr.attr('data-uniqueid', $tr.attr('data-uniqueid'));

var end = that.options.fixedNumber;

if (rowspan > 0) {

--end;

--rowspan;

}

for (var i = 0; i < end; i++) {

$newtr.append($tds.eq(i).clone());

}

that.$fixedBodyColumns.append($newtr);

if ($tds.eq(0).attr('rowspan')) {

rowspan = $tds.eq(0).attr('rowspan') - 1;

}

});

};

BootstrapTable.prototype.resetView = function () {

_resetView.apply(this, Array.prototype.slice.apply(arguments));

if (!this.options.fixedColumns) {

return;

}

clearTimeout(this.timeoutHeaderColumns_);

this.timeoutHeaderColumns_ = setTimeout($.proxy(this.fitHeaderColumns, this), this.$el.is(':hidden') ? 100 : 0);

clearTimeout(this.timeoutBodyColumns_);

this.timeoutBodyColumns_ = setTimeout($.proxy(this.fitBodyColumns, this), this.$el.is(':hidden') ? 100 : 0);

};

BootstrapTable.prototype.fitHeaderColumns = function () {

var that = this,

visibleFields = this.getVisibleFields(),

headerWidth = 0;

this.$body.find('tr:first-child:not(.no-records-found) > *').each(function (i) {

var $this = $(this),

index = i;

if (i >= that.options.fixedNumber) {

return false;

}

if (that.options.detailView && !that.options.cardView) {

index = i - 1;

}

that.$fixedHeader.find('th[data-field="' + visibleFields[index] + '"]')

.find('.fht-cell').width($this.innerWidth());

headerWidth += $this.outerWidth();

});

this.$fixedHeader.width(headerWidth).show();

};

BootstrapTable.prototype.fitBodyColumns = function () {

var that = this,

top &#

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值