js表格功能:冻结行列,排序,列拖拽,怎么一起实现

JavaScript07

js表格功能:冻结行列,排序,列拖拽,怎么一起实现,第1张

1.如图,一个没有经过固定操作的电子表格,当随着表格被拖动或滚动,需要被固定的首行或首列的信息也跟着滚动消失了。 2.首先打开excel文档,假设我们需要让表格的前两行内容在表格滚动时固定在表格顶部,即第3行与第4行不变,如图所示。

选中XY单元格,点击冻结窗格。此时的效果是:

1. 冻结(X-1)列

2. 冻结(Y-1)行

如果想冻结前三行,也就是不想冻结列。按照上面的说法,选择A4单元格(X=A,Y=4)冻结后就是前三行。

可以的,实现方法如下:

(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: absolutebackground-color: #fffborder-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 = 0i <this.options.fixedNumberi++) {

$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 = 0i <that.options.fixedNumberi++) {

$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)

bootstrap-table-fixed-columns.js修改后的源码

.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

}

bootstrap-table-fixed-columns.css修改后

主要修改的地方:

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

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