我在这个链接中包含了Oleg提供的代码。根据列的内容设置列的大小是非常有效的。我现在面临的唯一问题是,当我为列值设置"editable:true"时,跨度也会与元素一起显示。将此跨度添加到各个单元格以获取单元格中文本的宽度。现在要编辑列,显示的列值为:
<span class="mywrapping">text</span>
请帮忙。
你说得对。在我看来,现在我将更有效地包括仅临时包装<span>
来测量单元格的宽度。在这种情况下,包装跨度将不会停留在单元格中,您所描述的任何问题都不会显得更多。
该演示提供了网格中实现"autowidth"行为的修改版本。它使用以下代码
var autosizeColumn = function (iCol) {
var $this = $(this), iRow, rows, row, colWidth,
cm = typeof iCol === "string" ? $this.jqGrid("getColProp", iCol) : $this.jqGrid("getGridParam", "colModel")[iCol],
getOuterWidth = function ($elem) {
var $wrappingSpan, width;
$elem.wrapInner("<span class='mywrapping'></span>");
$wrappingSpan = $elem.find(">.mywrapping");
width = $wrappingSpan.outerWidth();
$elem.html($wrappingSpan.html());
return width;
};
if (cm == null || cm.hidden) {
return; // don't change the width of hidden columns
}
colWidth = getOuterWidth($(this.grid.headers[iCol].el).find(">div")) + 25; // 25px for sorting icons
for (iRow = 0, rows = this.rows; iRow < rows.length; iRow++) {
row = rows[iRow];
if ($(row).hasClass("jqgrow")) {
colWidth = Math.max(colWidth, getOuterWidth($(row.cells[iCol])));
}
}
$this.jqGrid("setColWidth", iCol, colWidth);
},
autosizeColumns = function () {
var $this = $(this), iCol,
colModel = $this.jqGrid("getGridParam", "colModel"),
n = $.isArray(colModel) ? colModel.length : 0;
for (iCol = 0; iCol < n; iCol++) {
autosizeColumn.call(this, iCol);
}
};
$grid.bind("jqGridAfterLoadComplete jqGridRemapColumns jqGridInlineAfterSaveRow", autosizeColumns);
更新。或者可以使用autoWidthColumns
插件,我在这里发布了jQuery.jqGrid.addColumn.js
。在这种情况下,只需要包括jQuery.jqGrid.setColWidth.js
和jQuery.jqGrid.autoWidthColumns.js
,并使用$("#gridid").jqGrid("autoWidthColumns").jqGrid({/*option*/});
而不是标准$("#gridid").jqGrid({/*option*/});
创建网格。
该演示使用了autoWidthColumns
插件。