新文章 网摘 文章 随笔 日记

设置Telerik Grid自动列宽

    //设置Telerik Grid自动列宽
    if (typeof JSCommon.autoFitTelerikGridColumns != 'function') {
        JSCommon.prototype.autoFitTelerikGridColumns = function autoFitColumns(girdId) {
            var $container = $('#' + girdId);
            var containerWidth = Math.min(parseInt($container.css('width')), parseInt($container.parent().eq(0).css('width'))) + 'px';

            var $grid = $container.find('table');
            var $header = $container.find('.t-grid-header');
            $header.find('.t-link').css('padding', '0').css('margin', '0').css('width', 'auto');
            $header.find('.t-header').css('padding', '0').css('margin', '0').css('width', 'auto');
            $header.css('width', 'auto');

            var $content = $container.find('.t-grid-content');
            $content.css('width', 'auto');
            $content.css('overflow', 'auto');
            $content.find('td').css('white-space', 'nowrap');//不换行

            var $bottom = $container.find('.t-grid-pager.t-grid-bottom');

            $grid.css('min-width', '10px');
            $grid.css('width', 'auto');
            var $tHeadThs = $($grid[0]).find('th:Visible');
            var $tBodyTds = $($grid[1]).find('tr').eq(0).find('td:Visible');
            var widths = new Array();
            var totalWidth = 0;
            for (var i = 0; i < $tHeadThs.length; i++) {
                var width1 = parseInt($tHeadThs.eq(i).css('width'));
                var width2 = parseInt($tBodyTds.eq(i).css('width'));
                var width = Math.max(width1, width2);
                widths.push(width);
                totalWidth += width;
            }
            $grid.css('width', totalWidth + 'px');
            for (var j = 0; j < $tHeadThs.length; j++) {
                $tHeadThs.eq(j).css('width', widths[j] + 'px');
                $tBodyTds.eq(j).css('width', widths[j] + 'px');
            }
            $tHeadThs.css('overflow', 'visible');
            $($grid[1]).find('tr').find('td:Visible').css('overflow', 'visible');

            $bottom.css('width', $content.css('width'));
            $container.css('max-width', containerWidth);
            $container.css('overflow', 'auto');
            $content.css('height', 'auto');
        };
    }

 

posted @ 2019-12-20 12:03  岭南春  阅读(433)  评论(0编辑  收藏  举报