• bootstrap-table固定右侧列+表头和内容对齐


    1.框架:bootstrap4

    CSS样式: bootstrap-table.min.css,

    bootstrap-treeview.css,

    bootstrap-table-fixed-columns.css

    JS样式:jquery.min.js

    bootstrap.min.js,

    bootstrap-table.min.js,

    bootstrap-table-zh-CN.min.js,

    bootstrap-treeview.js,

    bootstrap-table-fixed-columns.js

    2.修改的地方:在bootstrap-table.min.js中找到源码:

    if (!this.options.cardView && this.options.showHeader && this.options.height) {
        this.$tableHeader.show();
        //注释下面两行
        //this.resetHeader();
        //padding += this.$header.outerHeight(true) + 1
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    3.项目中引入bootstrap-table-fixed-columns.css:

    .fixed-table-header-columns,
    .fixed-table-body-columns {
        position: absolute;
        background-color: #fff;
        display: none;
        box-sizing: border-box;
        overflow: hidden;
    }
     
    .fixed-table-header-columns .table,
    .fixed-table-body-columns .table {
        border-right: 1px solid #e2e2e2;
    }
     
    .fixed-table-header-columns .table.table-no-bordered,
    .fixed-table-body-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;
    }
    .fixed-table-container tbody tr:first-child td{
      border-top: 1px solid #e2e2e2;
    }
     
    .fixed-table-container tbody tr td:first-child, .fixed-table-container thead tr th:first-child{
      border-left: 1px solid #e2e2e2!important;
    }
     
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35

    bootstrap-table-fixed-columns.js

    /**
     * @author wu
     */
     
    (function ($) {
        'use strict';
     
        $.extend($.fn.bootstrapTable.defaults, {
            fixedColumns: false,
            fixedNumber: 1,
            fixedFrom: 'right'//left
        });
     
        var BootstrapTable = $.fn.bootstrapTable.Constructor,
            _initHeader = BootstrapTable.prototype.initHeader,
            _initBody = BootstrapTable.prototype.initBody,
            _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);
            if (this.options.fixedFrom == 'right') {
                this.$fixedHeader.css({ right: 0 });
                this.$fixedBody.css({ right: 0 })
            }
        };
     
        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();
            $trs.each(function () {
                if (that.options.fixedFrom == 'right') {
                    var index = that.options.columns[0].length - Math.abs(parseInt(that.options.fixedNumber));
                    $(this).find('th:lt(' + index + ')').remove();
     
                } else {
                    $(this).find('th:gt(' + that.options.fixedNumber + ')').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('');
                if (that.options.fixedFrom == 'right') {
                    var end = that.options.columns[0].length - Math.abs(parseInt(that.options.fixedNumber));
                } else {
                    var end = that.options.fixedNumber;
                }
     
                if (rowspan > 0) {
                    --end;
                    --rowspan;
                }
                if (that.options.fixedFrom == 'right') {
                    for (var i = end; i < that.options.columns[0].length; i++) {
                        $tr.append($tds.eq(i).clone());
                    }
                } else {
                    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.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;
            if (that.options.fixedFrom == 'right') {
                var trs = [].slice.call(this.$body.find('tr:first-child:not(.no-records-found) > *'));
                var $trs = $(trs.reverse());
                $trs.each(function (i) {
                    var $this = $(this);
                    var index = i;
                    if (i >= Math.abs(that.options.fixedNumber)) {
                        return false;
                    }
     
                    if (that.options.detailView && !that.options.cardView) {
                        index = i - 1;
                    }
                    that.$fixedHeader.find('th[data-field="' + visibleFields[$trs.length - 1 + index] + '"]')
                        .find('.fht-cell').width($this.innerWidth());
                    headerWidth += $this.outerWidth();
                });
                that.$header.find('> tr').each(function (i) {
                  that.$fixedHeader.height($(this).height());
                });
            } else {
                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.width(headerWidth + 1).show();
        };
     
        BootstrapTable.prototype.fitBodyColumns = function () {
            var that = this,
                top = -(parseInt(this.$el.css('margin-top')) - 2),
                // the fixed height should reduce the scorll-x height
                height = this.$tableBody.height() - 18;
                console.log("fitBodyColumns" + height);
     
            if (!this.$body.find('> tr[data-index]').length) {
                this.$fixedBody.hide();
                return;
            }
     
            if (!this.options.height) {
                top = this.$fixedHeader.height();
                console.log("fitBodyColumns header Height" + top);
                height = height - top;
            }
     
            // height = this.$tableBody.find("tbody").height();
            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());
            });
     
            // 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');
            });
            fixFixedRightColumnsEvents.call(this);
        };
     
        function fixFixedRightColumnsEvents() {
            var that = this;
            if (this.options.fixedFrom == 'right') {
                var fixedBeginIndex = that.options.columns[0].length - 1 - that.options.fixedNumber;
                $.each(this.header.events, function (i, events) {
                    if (i < fixedBeginIndex) {
                        return;
                    }
                    if (!events) {
                        return;
                    }
                    // fix bug, if events is defined with namespace
                    if (typeof events === 'string') {
                        events = calculateObjectValue(null, events);
                    }
     
                    var field = that.header.fields[i];
                    var fieldIndex = $.inArray(field, that.getVisibleFields());
     
                    if (fieldIndex === -1) {
                        return;
                    }
     
                    if (that.options.detailView && !that.options.cardView) {
                        fieldIndex += 1;
                    }
     
                    for (var key in events) {
                        that.$fixedBodyColumns.find('>tr:not(.no-records-found)').each(function () {
                            var $tr = $(this),
                                $td = $tr.find(that.options.cardView ? '.card-view' : 'td').eq(fieldIndex - fixedBeginIndex - 1),
                                index = key.indexOf(' '),
                                name = key.substring(0, index),
                                el = key.substring(index + 1),
                                func = events[key];
     
                            $td.find(el).off(name).on(name, function (e) {
                                var index = $tr.data('index'),
                                    row = that.data[index],
                                    value = row[field];
     
                                func.apply(this, [e, value, row, index]);
                            });
                        });
                    }
                });
            }
     
        }
     
    })(jQuery);
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
    • 241
    • 242
    • 243
    • 244
    • 245
    • 246
    • 247
    • 248
    • 249
    • 250
    • 251
    • 252
    • 253
    • 254
    • 255
    • 256
    • 257
    • 258
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273

    4.前端HTML或者JSP页面中:

     <table id="bootstrap-home-table" style="table-layout:fixed!important;word-break:break-all;"></table>
    
    • 1

    在js中:

    在初始化表格initTable()方法的第一行加入:
    $('#bootstrap-home-table').bootstrapTable('destroy');
    在方法内加入:
    fixedColumns: true,        //固定列
    fixedNumber:1,			  //固定右侧列
    fixedFrom: 'right',       // 左侧为left
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    5.固定右侧列效果如下:

    在这里插入图片描述

    最后运行项目并测试,右侧固定列功能已完成。

    如果文章有用,麻烦点个赞,给个评论,谢谢。

  • 相关阅读:
    qt qml
    参加霍格沃兹测试开发学社举办的火焰杯软件测试开发大赛是一种什么体验?
    Java中的ORM框架——myBatis
    Go语言入门心法(三): 接口
    【常见SQL报错及解决办法】个人记录,自用
    【无标题】shell_43.Linux三种在 shell 脚本中处理选项的方法
    Windows内核函数 - ANSI_STRING字符串与UNICODE_STRING字符串
    Harbor(V2.8+) 登录时报错 net/http: TLS handshake timeout
    基于spingboot的websocket订阅、广播、多人聊天室示例
    windocs连接麒麟桌面---vnc软件
  • 原文地址:https://blog.csdn.net/wlf_wzx/article/details/125410040