JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案


Posted in Javascript onJune 30, 2017

前言:最近项目里面需要用到表格的冻结列功能,所谓“冻结列”,就是某些情况下表格的列比较多,需要固定前面的几列,后面的列滚动。遗憾的是,bootstrap table里自带的fixed column功能有一点bug,于是和同事讨论该如何解决,于是就有了这篇文章。

一、起因回顾

最近项目里面有一个表格需求,该表格列是动态产生的,而且列的数量操作一定值以后就会出现横向滚动条,滚动的时候需要前面几列固定。也就是所谓的excel的冻结列功能。该如何实现呢?不用多说,当然是查文档,于是找到了这篇http://issues.wenzhixin.net.cn/bootstrap-table/index.html#extensions/fixed-columns.html。谷歌浏览器效果如下:

第一列固定

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

貌似问题完美解决!可是,事与愿违,很遗憾,上面说了,这是谷歌浏览器的效果,没有问题。我们来看看IE里面

IE11效果:

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

IE10效果:

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

很显然,不管是IE内核版本多少,冻结的列里面的内容都无法显示。怎么办?这可为难死宝宝了!

二、解决方案

还好有万能的开源,查看该页面源代码发现可以找到冻结列这个js的源码。

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

点击进入可以看到这个js的所有源码,找到源码就好办了,我们试着改改源码看是否能解决这个bug。

我们在bootstrap-table下面的extensions文件夹下面新增加一个文件夹fixed-column

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

下面就贴出我们改好的源码:

(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.$fixedBody = $([
   '<div class="fixed-table-column" style="position: absolute; background-color: #fff; border-right:1px solid #ddd;">',
   '<table>',
   '<thead></thead>',
   '<tbody></tbody>',
   '</table>',
   '</div>'].join(''));
  this.timeoutHeaderColumns_ = 0;
  this.timeoutBodyColumns_ = 0;
  this.$fixedBody.find('table').attr('class', this.$el.attr('class'));
  this.$fixedHeaderColumns = this.$fixedBody.find('thead');
  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 $tr = this.$header.find('tr:eq(0)').clone(),
   $ths = $tr.clone().find('th');
  $tr.html('');
  for (var i = 0; i < this.options.fixedNumber; i++) {
   $tr.append($ths.eq(i).clone());
  }
  this.$fixedHeaderColumns.html('').append($tr);
 };
 BootstrapTable.prototype.initBody = function () {
  _initBody.apply(this, Array.prototype.slice.apply(arguments));
  if (!this.options.fixedColumns) {
   return;
  }
  var that = this;
  this.$fixedBodyColumns.html('');
  this.$body.find('> tr[data-index]').each(function () {
   var $tr = $(this).clone(),
    $tds = $tr.clone().find('td');
   $tr.html('');
   for (var i = 0; i < that.options.fixedNumber; i++) {
    $tr.append($tds.eq(i).clone());
   }
   that.$fixedBodyColumns.append($tr);
  });
 };
 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.$fixedBody.find('thead th[data-field="' + visibleFields[index] + '"]')
    .find('.fht-cell').width($this.innerWidth() - 1);
   headerWidth += $this.outerWidth();
  });
  this.$fixedBody.width(headerWidth - 1).show();
 };
 BootstrapTable.prototype.fitBodyColumns = function () {
  var that = this,
   top = -(parseInt(this.$el.css('margin-top')) - 2),
   height = this.$tableBody.height() - 2;
  if (!this.$body.find('> tr[data-index]').length) {
   this.$fixedBody.hide();
   return;
  }
  this.$body.find('> tr').each(function (i) {
   that.$fixedBody.find('tbody tr:eq(' + i + ')').height($(this).height() - 1);
  });
  //// events
  this.$tableBody.on('scroll', function () {
   that.$fixedBody.find('table').css('top', -$(this).scrollTop());
  });
  this.$body.find('> tr[data-index]').off('hover').hover(function () {
   var index = $(this).data('index');
   that.$fixedBody.find('tr[data-index="' + index + '"]').addClass('hover');
  }, function () {
   var index = $(this).data('index');
   that.$fixedBody.find('tr[data-index="' + index + '"]').removeClass('hover');
  });
  this.$fixedBody.find('tr[data-index]').off('hover').hover(function () {
   var index = $(this).data('index');
   that.$body.find('tr[data-index="' + index + '"]').addClass('hover');
  }, function () {
   var index = $(this).data('index');
   that.$body.find('> tr[data-index="' + index + '"]').removeClass('hover');
  });
 };
})(jQuery);
.fixed-table-container thead th .th-inner, .fixed-table-container tbody td .th-inner {
   line-height: 18px;
  }
  .fixed-table-pagination .pagination a {
   padding: 5px 10px;
  }
  .fixed-table-toolbar .bars, .fixed-table-toolbar .search, .fixed-table-toolbar .columns {
   margin-top: 5px;
   margin-bottom: 5px;
  }

主要修改的地方:

1)源码里面将thead和tbody分别封装成了一个单独的表格,修改后将thead和tbody放到了一个table里面;

2)依次遍历冻结的列放入到固定的tbody里面;

其实也就改了那么几个地方,就能完美解决IE的bug。我们先来看看效果:

IE11

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

IE10

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

IE8

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

我们再来看看如何使用。

1、引用js和对应的css

<script src="~/Content/bootstrap-table/extensions/fixed-column/js/bootstrap-table-fixed-columns.js"></script>
<link href="~/Content/bootstrap-table/extensions/fixed-column/css/bootstrap-table-fixed-columns.css" rel="external nofollow" rel="stylesheet" />

2、js调用如下

JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案

加两个参数fixedColumns和fixedNumber即可,什么意思不用过多解释,是否冻结列、冻结列的列数。还有一点需要说明的是,这里调用的时候不能指定它的height,如果指定height,表格的冻结显示会有问题。

以上所述是小编给大家介绍的JS 组件系列之Bootstrap Table 冻结列功能IE浏览器兼容性问题解决方案,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

Javascript 相关文章推荐
js 获取服务器控件值的代码
Mar 05 Javascript
JavaScript几种形式的树结构菜单
May 10 Javascript
js substring从右边获取指定长度字符串(示例代码)
Dec 23 Javascript
JavaScript中检查对象property的存在性方法介绍
Dec 30 Javascript
在jQuery中处理XML数据的大致方法
Aug 14 Javascript
JavaScript操作select元素和option的实例代码
Jan 29 Javascript
js实现统计字符串中特定字符出现个数的方法
Aug 02 Javascript
12306 刷票脚本及稳固刷票脚本(防挂)
Jan 04 Javascript
一道面试题引发的对javascript类型转换的思考
Mar 06 Javascript
解决vue2中使用axios http请求出现的问题
Mar 05 Javascript
js中如何完美的解析数据
Mar 18 Javascript
在react-antd中弹出层form内容传递给父组件的操作
Oct 24 Javascript
vue2.0 axios前后端数据处理实例代码
Jun 30 #Javascript
JS 组件系列之Bootstrap Table的冻结列功能彻底解决高度问题
Jun 30 #Javascript
MUI实现上拉加载和下拉刷新效果
Jun 30 #Javascript
js实现京东轮播图效果
Jun 30 #Javascript
jquery实现一个全局计时器(商城可用)
Jun 30 #jQuery
Vue和Bootstrap的整合思路详解
Jun 30 #Javascript
JavaScript原型继承_动力节点Java学院整理
Jun 30 #Javascript
You might like
PHP __autoload函数(自动载入类文件)的使用方法
2012/02/04 PHP
php类常量的使用详解
2013/06/08 PHP
php四种基础算法代码实例
2013/10/29 PHP
配置eAccelerator和XCache扩展来加速PHP程序的执行
2015/12/22 PHP
调用WordPress函数统计文章访问量及PHP原生计数器的实现
2016/03/21 PHP
利用jquery包将字符串生成二维码图片
2013/09/12 Javascript
jQuery焦点图切换简易插件制作过程全纪录
2014/08/27 Javascript
JQuery EasyUI的使用
2016/02/24 Javascript
jQuery日历插件datepicker用法详解
2016/03/03 Javascript
JavaScript中用let语句声明作用域的用法讲解
2016/05/20 Javascript
AngularJS 简单应用实例
2016/07/28 Javascript
jQuery模拟Marquee实现无缝滚动效果完整实例
2016/09/29 Javascript
关于Vue.js 2.0的Vuex 2.0 你需要更新的知识库
2016/11/30 Javascript
javascript 显示全局变量与隐式全局变量的区别
2017/02/09 Javascript
AngularJS中的路由使用及实现代码
2017/10/09 Javascript
Angular设置别名alias的方法
2018/11/08 Javascript
JavaScript惰性载入函数实例分析
2019/03/27 Javascript
微信小程序图片加载失败时替换为默认图片的方法
2019/12/09 Javascript
vue实现计算器功能
2020/02/22 Javascript
介绍Python中几个常用的类方法
2015/04/08 Python
Python中类的定义、继承及使用对象实例详解
2015/04/30 Python
Python验证码识别的方法
2015/07/10 Python
Python实现多线程抓取网页功能实例详解
2017/06/08 Python
Python Xml文件添加字节属性的方法
2018/03/31 Python
使用pycharm设置控制台不换行的操作方法
2019/01/19 Python
浅析Python3 pip换源问题
2020/01/06 Python
keras 获取某层的输入/输出 tensor 尺寸操作
2020/06/10 Python
美国经典刺绣和字母儿童服装特卖:Smocked Auctions
2018/07/16 全球购物
师范院校学生自荐信范文
2013/12/27 职场文书
单位介绍信范文
2014/01/18 职场文书
《阳光》教学反思
2014/02/23 职场文书
德育标兵事迹材料
2014/08/24 职场文书
外贸英文求职信范文
2015/03/19 职场文书
2019年警察入党转正申请书最新范文
2019/09/03 职场文书
什么是Python装饰器?如何定义和使用?
2022/04/11 Python
docker 制作mysql镜像并自动安装
2022/05/20 Servers