bootstrap-table固定最后一列

本文介绍了一个修改版的Bootstrap Table插件,该插件实现了在IE浏览器下兼容的固定列(最右侧)和固定表头功能。通过自定义的JS和CSS代码,解决了滚动时列错位的问题,并确保了固定表头使用滚动条占位。

 bootstrap-table  固定列(最右边)及固定表头兼容IE

下面是修改后的js源码,需要引入

以及css修改;固定到最右边,修复错位,使固定表头也用滚条占位;

.fixed-table-header-columns>table{
  position: relative;
  float: right;
}

.fixed-table-body-columns>table{
  position: relative;
  float: right;
}
.bootstrap-table .fixed-table-container .fixed-table-header{
    overflow-y: scroll;
}
.fixed-table-header-columns, .fixed-table-body-columns {
    right: 17px;
}
.fixed-table-body-columns table{
    border:0 !important;
}
.fixed-table-body-columns table>tbody>tr>td{
    border-top:0;
}

 
(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,
        _getCaret = BootstrapTable.prototype.getCaret;  // Add: Aleksej

    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);
    };

    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(true); //Fix: Aleksej "clone()" mit "clone(true)" ersetzt
        var trLen = $trs.length;
        $trs.each(function () {
            // This causes layout problems:
            console.log(that.options.fixedNumber)
            //$(this).find('th:gt(' + (that.options.fixedNumber -1) + ')').remove(); // Fix: Aleksej "-1" hinnzugef�gt. Denn immer eine Spalte Mehr geblieben ist
            var newTr = trLen - that.options.fixedNumber;
            $(this).find('th:lt(' + newTr + ')').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');

            var dataIndex = $tr.attr("data-index");
            $tr = $("<tr></tr>");
            $tr.attr("data-index", dataIndex);

            var end = that.options.fixedNumber;
            if (rowspan > 0) {
                --end;
                --rowspan;
            }

            var colTd = $tds.length - 1;    //td长度减1
            for (var i = 0; i < end; i++) {

                $tr.append($tds.eq(colTd - i).clone());   //克隆最后一列
            }
            that.$fixedBodyColumns.append($tr);

            if ($tds.eq(0).attr('rowspan')) {
                rowspan = $tds.eq(0).attr('rowspan') - 1;
            }
        });
    };

    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;
        var vLen = visibleFields.length;   //表格列数
        this.$body.find('tr:first-child:not(.no-records-found) > *').each(function (i) {
            var $this = $(this),
                index = i;

            //修改长度相等的时候
            if (i == vLen - that.options.fixedNumber) {


                if (that.options.detailView && !that.options.cardView) {
                    index = i - 1;
                }

                var $th = that.$fixedHeader.find('th[data-field="' + visibleFields[index] + '"]');
                $th.find('.fht-cell').width($this.innerWidth());
                headerWidth += $this.outerWidth();

                $th.data('fix-pos', index);
            }
        });
        this.$fixedHeader.width(headerWidth + 1).show();

        // fix click event
        this.$fixedHeader.delegate("tr th", 'click', function () {
            $(this).parents(".fixed-table-container").find(".fixed-table-body table thead tr th:eq(" + $(this).data("fix-pos") + ") .sortable").click();
        })
    };

    /**
     * Add: Aleksej
     * Hook f�r getCaret. Aktualisieren Header bei Fixed-Columns wenn diese sortiert wurden
     * @method getCaret
     * @for BootstrapTable
     */
    BootstrapTable.prototype.getCaret = function () {
        var result = _getCaret.apply(this, arguments);

        if (this.options.fixedColumns && this.$fixedHeaderColumns instanceof jQuery) {
            var that = this, $th;

            $.each(this.$fixedHeaderColumns.find('th'), function (i, th) {
                $th = $(th);
                $th.find('.sortable').removeClass('desc asc').addClass($th.data('field') === that.options.sortName ? that.options.sortOrder : 'both');
            });
        }

        return result;
    };

    /**
     * Add: Aleksej, zum berechnen von Scrollbar-Gr��e
     * @method calcScrollBarSize
     * @return Number
     */
    BootstrapTable.prototype.calcScrollBarSize = function () {
        // Es ist egal, ob H�he oder Breite
        var tmpWidth = 100,
            $container = $('<div>').css({
                    width: tmpWidth,
                    overflow: 'scroll',
                    visibility: 'hidden'
                }
            ).appendTo('body'),
            widthWithScroll = $('<div>').css({
                width: '100%'
            }).appendTo($container).outerWidth();

        $container.remove();
        return tmpWidth - widthWithScroll;
    };

    BootstrapTable.prototype.fitBodyColumns = function () {
        var that = this,
            borderHeight = (parseInt(this.$el.css('border-bottom-width')) + parseInt(this.$el.css('border-top-width'))), // Add. Aleksej
            top = this.$fixedHeader.outerHeight() + borderHeight, // Fix. Aleksej "-2" mit "+ borderHeight" ersetzt
            // the fixed height should reduce the scorll-x height
            height = this.$tableBody.height() - this.calcScrollBarSize(); // Fix. Aleksej "-14" mit "- this.calcScrollBarSize()" ersetzt

        if (!this.$body.find('> tr[data-index]').length) {
            this.$fixedBody.hide();
            return;
        }

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

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

        this.$body.find('> tr').each(function (i) {
            that.$fixedBody.find('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');
        });

        // fix td width bug
        var $first_tr = that.$body.find('tr:eq(0)');
        that.$fixedBody.find('tr:eq(0)').find("td").each(function (index) {
            $(this).width($first_tr.find("td:eq(" + index + ")").width())
        });
    };

})(jQuery);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值