jquery - Jqgrid根据其内容可编辑列宽

标签 jquery html css jqgrid width

我已将 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.jsjQuery.jqGrid.autoWidthColumns.js并使用 $("#gridid").jqGrid("autoWidthColumns").jqGrid({/*option*/}); 创建网格而不是标准$("#gridid").jqGrid({/*option*/}); .

The demo使用 autoWidthColumns插件。

关于jquery - Jqgrid根据其内容可编辑列宽,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26751123/

相关文章:

javascript - jQuery 命令不起作用

html - 为什么我的按钮背景在 Firefox 中被截断,而在 Google Chrome 中却没有?

javascript - 如何使用 jquery 更改 anchor <a> 位于表格单元格中的样式

jQuery 模态对话框问题

javascript - 如果单词包含PHP数组循环中的空格,则写入时数据会 split

html - 无法弄清楚如何将结果限制为希望使用 .group

html - UIWebView 中带有 HTML 和 CSS 的 iPhone native 应用程序

html - 放大/缩小时如何使阴影框保持在同一个位置?

javascript - 在 jQuery 选择器中使用函数参数

javascript - $(this) 正在提醒 jquery 中先前选择的值