如何解決JS組件系列之BootstrapTable凍結(jié)列功能IE瀏覽器兼容性問題

小編給大家分享一下如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供淮濱企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為淮濱眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。

谷歌瀏覽器效果如下:

第一列固定

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題

貌似問題完美解決!可是,事與愿違,很遺憾,上面說了,這是谷歌瀏覽器的效果,沒有問題。我們來看看IE里面

IE11效果:

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題

IE10效果:

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題

很顯然,不管是IE內(nèi)核版本多少,凍結(jié)的列里面的內(nèi)容都無法顯示。怎么辦?這可為難死寶寶了!

二、解決方案

還好有萬能的開源,查看該頁面源代碼發(fā)現(xiàn)可以找到凍結(jié)列這個(gè)js的源碼。

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題

點(diǎn)擊進(jìn)入可以看到這個(gè)js的所有源碼,找到源碼就好辦了,我們?cè)囍母脑创a看是否能解決這個(gè)bug。

我們?cè)赽ootstrap-table下面的extensions文件夾下面新增加一個(gè)文件夾fixed-column

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能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" >',
   '<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分別封裝成了一個(gè)單獨(dú)的表格,修改后將thead和tbody放到了一個(gè)table里面;

2)依次遍歷凍結(jié)的列放入到固定的tbody里面;

其實(shí)也就改了那么幾個(gè)地方,就能完美解決IE的bug。我們先來看看效果:

IE11

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題

IE10

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題

IE8

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題

我們?cè)賮砜纯慈绾问褂谩?/p>

1、引用js和對(duì)應(yīng)的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調(diào)用如下

如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題

加兩個(gè)參數(shù)fixedColumns和fixedNumber即可,什么意思不用過多解釋,是否凍結(jié)列、凍結(jié)列的列數(shù)。還有一點(diǎn)需要說明的是,這里調(diào)用的時(shí)候不能指定它的height,如果指定height,表格的凍結(jié)顯示會(huì)有問題。

以上是“如何解決JS組件系列之Bootstrap Table凍結(jié)列功能IE瀏覽器兼容性問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

分享名稱:如何解決JS組件系列之BootstrapTable凍結(jié)列功能IE瀏覽器兼容性問題
網(wǎng)站URL:http://muchs.cn/article2/gppioc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、響應(yīng)式網(wǎng)站商城網(wǎng)站、建站公司網(wǎng)站建設(shè)、App設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

營銷型網(wǎng)站建設(shè)