我已将 Oleg 提供的代码包含在 link 中.它可以完美地根据其内容设置列的大小。我现在面临的唯一问题是,当我为列值设置“editble:true”时,跨度也会与元素一起显示。此跨度被添加到各个单元格以获取文本的宽度存在于单元格中。现在要编辑列,显示的列值是:
<span class="mywrapping">text</span>
请帮忙。
最佳答案
你是对的。现在在我看来,包含包装会更有效 <span>
只是临时测量单元格的宽度。在这种情况下,包裹跨度不会停留在单元格中,并且您所描述的问题不会再出现。
The demo提供网格中实现“自动宽度”行为的修改版本。它使用以下代码
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
的插件here 。在这种情况下,只需包含 jQuery.jqGrid.setColWidth.js
和jQuery.jqGrid.autoWidthColumns.js
并使用 $("#gridid").jqGrid("autoWidthColumns").jqGrid({/*option*/});
创建网格而不是标准$("#gridid").jqGrid({/*option*/});
.
The demo使用 autoWidthColumns
插件。
关于jquery - Jqgrid根据其内容可编辑列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26751123/