DataTables fixed headers misaligned with columns in wide tables

前端 未结 21 1590
悲&欢浪女
悲&欢浪女 2020-11-30 19:23

Problem

When using the sScrollX, sScrollXInner and/or sScrollY to achieve a fixed header table with its inner content scroll

21条回答
  •  温柔的废话
    2020-11-30 20:18

    I know there has been an answer flagged already, but for someone with similar problems that the above does not work for. I'm using it in conjunction with bootstrap theme.

    There is a line that can be altered in the dataTables.fixedHeader.js file. the default layout of the function looks as follows.

        _matchWidths: function (from, to) {
            var get = function (name) {
                return $(name, from)
                    .map(function () {
                        return $(this).width();
                    }).toArray();
            };
    
            var set = function (name, toWidths) {
                $(name, to).each(function (i) {
                    $(this).css({
                        width: toWidths[i],
                        minWidth: toWidths[i]
                    });
                });
            };
    
            var thWidths = get('th');
            var tdWidths = get('td');
    
            set('th', thWidths);
            set('td', tdWidths);
        },
    

    change the

        return $(this).width();
    

    to

        return $(this).outerWidth();
    

    outerWidth() is a function that is contained in jquery.dimensions.js if you are usingg a newer version of jquery. ie. jquery-3.1.1.js

    what the above function does is it maps the th of the original table, then applies them to the "cloned" table(fixed header). in my case they were always off by about 5px to 8px when misaligned. Most probably not the best fix out there but provides a solution to all other tables in the solution without having to specify it per table declaration. It has only been tested in Chrome so far, but it should provide an adjudicate solution on all browsers.

提交回复
热议问题