【问题标题】:Jqgrid - Uncaught RangeError: Maximum call stack size exceededJqgrid - 未捕获 RangeError:超出最大调用堆栈大小
【发布时间】:2015-10-28 01:49:34
【问题描述】:

根据内容动态列宽

我尝试通过这种方式根据内容动态调整列宽,通过查找每行的字符长度,然后最终从中获取最大长度并将其设置为网格列宽。

loadComplete : function () {
                $("#grid").on("jqGridAfterLoadComplete jqGridRemapColumns", function () {
                var $this = $("#grid"),
                colModel = $this.jqGrid("getGridParam", "colModel"),
                iCol,
                iRow,
                rows,
                row,
                n = $.isArray(colModel) ? colModel.length : 0;
                var rowData = "";
                var rowDataLen="";
                var input = [];
                var divs = $( "div" );
                var colWidth=125;
                for (iCol = 0; iCol < n; iCol++) {
                            input = [];
                            for (iRow = 0, rows = this.rows; iRow < rows.length; iRow++) {
                                        row = rows[iRow];
                                        rowData = $(row.cells[iCol]).find(divs).html();
                                        if(rowData != undefined)
                                            rowDataLen = rowData.length;
                                        input.push(rowDataLen);
                            }
                            var finalWidth =  Math.max.apply(null, input);
                            if(finalWidth < colWidth)
                                finalWidth = colWidth;
                            $("#grid").jqGrid("setColWidth", iCol, finalWidth);
                            var gw = $("#grid").jqGrid('getGridParam','width');
                            $("#grid").jqGrid('setGridWidth',gw);
                       }                    
            });     
        },

它工作正常。

但是它太慢并且出现Uncaught RangeError: Maximum call stack size exceeded

当我有更多记录时出错,例如 500。

谁能帮助调整上述解决方案,使其更快?

这是我的 HTML 代码:

<td role="gridcell" style="text-align:left;" title="Hot-forged Hot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forged." aria-describedby="grid_test">
<div style="max-height: 120px">Hot-forged Hot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forgedHot-forged.</i><br><br><i>tttttttttttttttttttttttttttttttt</i></div></td>

我实际上是在找到 div 内容的最大字符大小..我也尝试以这种方式直接获取 &lt;td&gt; 的标题属性 - rowData = $(row.cells[iCol]).attr('title'); - 但它也给出了同样的错误

固定行高的列模型格式化程序:

格式化程序:函数(cellvalue){ if(cellvalue == undefined || cellvalue == null){ 单元格值 = ""; } 返回 '​​' + 单元格值 + ''; },

或者我怎样才能降低这段代码的性能?请帮忙..

【问题讨论】:

    标签: jquery html css performance jqgrid


    【解决方案1】:

    您发布的代码是错误的,但我确定您的代码还有其他地方,即长 title 和长单元格内容的来源。

    您发布的代码中的主要错误:您不应绑定到loadComplete 内的网格loadComplete 将被执行多次。在每次执行时,您都会添加 一个更多 绑定,这是错误的。相反,您应该移动$("#grid").on("jqGridAfterLoadComplete jqGridRemapColumns", function () {...}); 并在创建网格之前设置它。你应该明白jqGridAfterLoadComplete是每次都会在loadComplete之前触发的事件。我想你会以某种方式产生递归。

    我强烈建议您迁移到free jGrid(请参阅here)或至少使用我在jQuery.jqGrid.autoWidthColumns.js 中发布的autoWidthColumns 方法(请参阅here)。有关更多信息,请参阅 the answerthe old demo。您只需要在创建网格之前使用$("#grid").jqGrid("autoWidthColumns");。所有必需的绑定 ($("#grid").on("jqGridAfterLoadComplete jqGridRemapColumns", function () {...});) 在内部执行 autoWidthColumns 方法。

    【讨论】:

      猜你喜欢
      • 2018-01-24
      • 2014-08-01
      • 2013-05-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多