JS 組件系列之Bootstrap Table 凍結(jié)列功能IE瀏覽器兼容性問(wèn)題解決方案
前言:最近項(xiàng)目里面需要用到表格的凍結(jié)列功能,所謂“凍結(jié)列”,就是某些情況下表格的列比較多,需要固定前面的幾列,后面的列滾動(dòng)。遺憾的是,bootstrap table里自帶的fixed column功能有一點(diǎn)bug,于是和同事討論該如何解決,于是就有了這篇文章。
一、起因回顧
最近項(xiàng)目里面有一個(gè)表格需求,該表格列是動(dòng)態(tài)產(chǎn)生的,而且列的數(shù)量操作一定值以后就會(huì)出現(xiàn)橫向滾動(dòng)條,滾動(dòng)的時(shí)候需要前面幾列固定。也就是所謂的excel的凍結(jié)列功能。該如何實(shí)現(xiàn)呢?不用多說(shuō),當(dāng)然是查文檔,于是找到了這篇http://issues.wenzhixin.net.cn/bootstrap-table/index.html#extensions/fixed-columns.html。谷歌瀏覽器效果如下:
第一列固定


貌似問(wèn)題完美解決!可是,事與愿違,很遺憾,上面說(shuō)了,這是谷歌瀏覽器的效果,沒(méi)有問(wèn)題。我們來(lái)看看IE里面
IE11效果:

IE10效果:

很顯然,不管是IE內(nèi)核版本多少,凍結(jié)的列里面的內(nèi)容都無(wú)法顯示。怎么辦?這可為難死寶寶了!
二、解決方案
還好有萬(wàn)能的開源,查看該頁(yè)面源代碼發(fā)現(xiàn)可以找到凍結(jié)列這個(gè)js的源碼。

點(diǎn)擊進(jìn)入可以看到這個(gè)js的所有源碼,找到源碼就好辦了,我們?cè)囍母脑创a看是否能解決這個(gè)bug。
我們?cè)赽ootstrap-table下面的extensions文件夾下面新增加一個(gè)文件夾fixed-column

下面就貼出我們改好的源碼:
(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分別封裝成了一個(gè)單獨(dú)的表格,修改后將thead和tbody放到了一個(gè)table里面;
2)依次遍歷凍結(jié)的列放入到固定的tbody里面;
其實(shí)也就改了那么幾個(gè)地方,就能完美解決IE的bug。我們先來(lái)看看效果:
IE11

IE10

IE8

我們?cè)賮?lái)看看如何使用。
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)用如下

加兩個(gè)參數(shù)fixedColumns和fixedNumber即可,什么意思不用過(guò)多解釋,是否凍結(jié)列、凍結(jié)列的列數(shù)。還有一點(diǎn)需要說(shuō)明的是,這里調(diào)用的時(shí)候不能指定它的height,如果指定height,表格的凍結(jié)顯示會(huì)有問(wèn)題。
以上所述是小編給大家介紹的JS 組件系列之Bootstrap Table 凍結(jié)列功能IE瀏覽器兼容性問(wèn)題解決方案,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- IE瀏覽器下JS腳本提交表單后,不能自動(dòng)提示問(wèn)題解決方法
- JS編寫兼容IE6,7,8瀏覽器無(wú)縫自動(dòng)輪播
- js實(shí)現(xiàn)保存文本框內(nèi)容為本地文件兼容IE,chrome,火狐瀏覽器
- JS實(shí)現(xiàn)獲取圖片大小和預(yù)覽的方法完整實(shí)例【兼容IE和其它瀏覽器】
- JS如何判斷瀏覽器類型和詳細(xì)區(qū)分IE各版本瀏覽器
- javascript 判斷當(dāng)前瀏覽器版本并判斷ie版本
- JavaScript自定義瀏覽器滾動(dòng)條兼容IE、 火狐和chrome
- JavaScript兼容瀏覽器FF/IE技巧
- Javascript在IE和Firefox瀏覽器常見兼容性問(wèn)題總結(jié)
- JavaScript 判斷瀏覽器是否是IE
相關(guān)文章
js實(shí)現(xiàn)一個(gè)頁(yè)面多個(gè)倒計(jì)時(shí)的3種方法
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)一個(gè)頁(yè)面多個(gè)倒計(jì)時(shí)的3種方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02
基于javascript處理nginx請(qǐng)求過(guò)程詳解
這篇文章主要介紹了基于javascript處理nginx請(qǐng)求過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07
基于JavaScript實(shí)現(xiàn)除夕煙花秀與隨機(jī)祝福語(yǔ)
新年即將來(lái)臨,本文將為大家介紹一個(gè)基于JavaScript實(shí)現(xiàn)的頁(yè)面特效:煙花秀+春節(jié)隨機(jī)祝福語(yǔ)。文中的示例代碼講解詳細(xì),感興趣的可以了解一下2022-01-01
ymPrompt的doHandler方法來(lái)實(shí)現(xiàn)獲取子窗口返回值的方法
今天在寫頁(yè)面時(shí)用到了ymPrompt的win方法來(lái)彈出一個(gè)窗口。由于要用到獲取子窗口返回來(lái)的值判斷是否刷新父窗口,在ymPrompt的組件Demo中一直沒(méi)有找到合適的方法實(shí)現(xiàn)2010-06-06
Javascript面試經(jīng)典套路reduce函數(shù)查重
reduce函數(shù),是ECMAScript5規(guī)范中出現(xiàn)的數(shù)組方法.下面通過(guò)本文給大家分享Javascript面試經(jīng)典套路reduce函數(shù)查重,需要的朋友參考下吧2017-03-03
JavaScript對(duì)象拷貝與賦值操作實(shí)例分析
這篇文章主要介紹了JavaScript對(duì)象拷貝與賦值操作,結(jié)合實(shí)例形式分析了javascript對(duì)象定義、拷貝、賦值等相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2018-12-12
使用JS實(shí)現(xiàn)導(dǎo)航切換時(shí)高亮顯示的示例講解
今天小編就為大家分享一篇使用JS實(shí)現(xiàn)導(dǎo)航切換時(shí)高亮顯示的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

