//自动列宽
function autoFitColumns(girdId) {
var $grid = $(‘#‘ + girdId).find(‘table‘);
$grid.css(‘width‘, ‘auto‘);
var $tHeadThs = $($grid[0]).find(‘th‘);
var $tBodyTds = $($grid[1]).find(‘tr‘).eq(0).find(‘td‘);
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‘);
}
}
原文地址:https://www.cnblogs.com/nirvanan/p/12072416.html