
正文
easyui datagird 列宽自适应
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
代码如下:
onLoadSuccess: function (data) {
var rows = data.rows; //得到行数据
var columnMaxCharacter = new Array(); //该列最大字符数
//遍历所有行数据,获得该列数据的最大字符数
var propertys = Object.getOwnPropertyNames(rows[0]);
for (var i = 0; i < rows.length; i++) {
for (var j = 0; j < propertys.length; j++) {
var s = eval('rows[' + i + '].' + propertys[j]);
//s = s.replace('<center>', ''); //屏蔽html标签
//s = s.replace('</center>', '');
if ((typeof columnMaxCharacter[propertys[j]]) == 'undefined') {
columnMaxCharacter[propertys[j]] = 0;
}
if ( s != null) {
if (s.length > columnMaxCharacter[propertys[j]]) {
columnMaxCharacter[propertys[j]] = s.length;
}
}
}
} //设置列宽度和字体
for (var j = 0; j < rows.length; j++) {
//得到该列的字体
// alert($("td[field='"+cls[j]+"'] div").get(0).currentStyle);
// var fontSize=$("td[field='"+cls[j]+"'] div").get(0).currentStyle.fontSize;//获得字体大小
// fontSize= fontSize.replace("px","");//去掉px方便运算
var fontSize = 12;
var w = fontSize * (columnMaxCharacter[propertys[j]] + 1); //求出宽度
//设定该列的宽度
$("td[field='" + propertys[j] + "'] div").width(w);
} }








