Bootstrap Table Fixed Columns

解决在使用原插件时出现的样式问题,增加支持tfoot的展示

bootstrap-table-fixed-columns.css

.fixed-table-header-columns,
.fixed-table-body-columns,
.fixed-table-footer-columns {
    position: absolute;
    background-color: #F4F6FE;/* #E7EBEE */
    display: none;
    box-sizing: border-box;
    overflow: hidden;
}

.fixed-table-header-columns .table,
.fixed-table-body-columns .table,
.fixed-table-footer-columns .table {
    border-right: 1px solid #ddd;
}

.fixed-table-header-columns .table.table-no-bordered,
.fixed-table-body-columns .table.table-no-bordered,
.fixed-table-footer-columns .table.table-no-bordered {
    border-right: 1px solid transparent;
}

.fixed-table-body-columns table {
    position: absolute;
    animation: none;
}

.bootstrap-table .table-hover > tbody > tr.hover > td{
    background-color: #f5f5f5;
}

bootstrap-table-fixed-columns.js

(function ($) {
    'use strict';

    $.extend($.fn.bootstrapTable.defaults, {
        fixedColumns: false,
        fixedNumber: 1
    });

    var BootstrapTable = $.fn.bootstrapTable.Constructor,
        _initHeader = BootstrapTable.prototype.initHeader,
        _initBody = BootstrapTable.prototype.initBody,
        _initFooter = BootstrapTable.prototype.initFooter,
        _resetView = BootstrapTable.prototype.resetView;

    BootstrapTable.prototype.initFixedColumns = function () {
        this.$fixedHeader = $([
            '<div class="fixed-table-header-columns">',
            '<table>',
            '<thead></thead>',
            '</table>',
            '</div>'].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 = $([
            '<div class="fixed-table-body-columns">',
            '<table>',
            '<tbody></tbody>',
            '</table>',
            '</div>'].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);
        
        this.$fixedFooter = $([
            '<div class="fixed-table-footer-columns">',
            '<table>',
            '<tfoot></tfoot>',
            '</table>',
            '</div>'].join(''));
        
        this.timeoutFooterColumns_ = 0;
        this.$fixedFooter.find('table').attr('class', this.$el.attr('class'));
        this.$fixedFooterColumns = this.$fixedFooter.find('tfoot');
        this.$tableFooter.before(this.$fixedFooter);
    };

    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();
        
        /****************固定列复选框事件****************/
        var $ths = $trs.find('th');      
        var headTh = $ths.eq(0);
        var input=headTh.find('input'); 
    	var inp=that.$header.find("th[data-field=" + 0 + "] input");
    	
    	input.click(function () {
    		inp.click();
        });
        /*********************************************************/
    	
        $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('');
            /*******固定列操作事件转移及复选框事件*******/
            for (var i = 0; i < that.options.fixedNumber; i++) {
                var indexTd = i;
                var oldTd = $tds.eq(indexTd);
                var fixTd = oldTd.clone();
                var buttons = fixTd.find('a');
                
                //事件转移:冻结列里面的事件转移到实际按钮的事件
                buttons.each(function (key, item) {
                    $(item).click(function () {
                        that.$body.find("tr[data-index=" + $tr.attr('data-index') + "] td:eq(" + indexTd + ") a:eq(" + key + ")").click();
                    });
                });
                if(i==0){
                	var inputs=fixTd.find('input');
                	var index=inputs.data("index");
                	var inp=that.$body.find("tr[data-index=" + index + "] td:eq(" + 0 + ") input[data-index=" + index + "]");
                	
                	inputs.click(function () {
                		inp.click();
                    });               	
                }
                $tr.append(fixTd);
            }
            that.$fixedBodyColumns.append($tr);
            /********************************************************/
//            var end = that.options.fixedNumber;
//            if (rowspan > 0) {
//                --end;
//                --rowspan;
//            }
//            for (var i = 0; i < end; i++) {
//                $tr.append($tds.eq(i).clone());
//            }
//            that.$fixedBodyColumns.append($tr);
//            
//            if ($tds.eq(0).attr('rowspan')){
//            	rowspan = $tds.eq(0).attr('rowspan') - 1;
//            }
        });
    };
    
    BootstrapTable.prototype.initFooter = function () {
    	_initFooter.apply(this, Array.prototype.slice.apply(arguments));

        if (!this.options.fixedColumns) {
            return;
        }
        
        var that = this, $trs = this.$tableBody.find('table tfoot tr').clone();

        $trs.each(function () {
            $(this).find('th:gt(' + (that.options.fixedNumber - 1) + ')').remove();
        });
        this.$fixedFooterColumns.html('').append($trs); 

    };

    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);
        
        clearTimeout(this.timeoutFooterColumns_);
        this.timeoutFooterColumns_ = setTimeout($.proxy(this.fitFooterColumns, 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.find('th').height(this.$header.find('tr').find('th:first-child').innerHeight());
        this.$fixedHeader.width(headerWidth + 1).show();
    };

    BootstrapTable.prototype.fitBodyColumns = function () {
        var that = this,
            top = -(parseInt(this.$el.css('margin-top')) - 2),
            height = this.$tableBody.find('table tbody').height();
        if (!this.$body.find('> tr[data-index]').length) {
            this.$fixedBody.hide();
            return;
        }

        if (!this.options.height) {
            top = this.$fixedHeader.height();
        }

        this.$fixedBody.css({
            width: this.$fixedHeader.width()-1,
            height: height,
            top: top
        }).show();

        this.$body.find('> tr').each(function (i) {
            that.$fixedBody.find('tr:eq(' + i + ')').height($(this).height());
        });

        // 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');
        });
    };
    
    BootstrapTable.prototype.fitFooterColumns = function () {
        var that = this,
        	top = -(parseInt(this.$el.css('margin-top')) - 2),
        	visibleFields = this.getVisibleFields(),
        	height = this.$tableBody.find('table tfoot').height(),
            footerWidth = 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());
            footerWidth += $this.outerWidth();
        });
        
        if (!this.options.height) {
            top = this.$fixedHeader.height() + this.$tableBody.find('table tbody').height();
        }
        
        this.$fixedFooter.css({
            width: footerWidth,
            height : height,
            top: top - 1
        }).show();
    };

})(jQuery);

对于冻结列的复选框和操作事件这边没有做优化,仍旧有bug,后面有机会改好了继续上传

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值