javascript - "Uncaught TypeError: Cannot call method ' indexOf ' of undefined"选中jqgrid行时

标签 javascript jquery xml jqgrid indexof

我用jqgrid有一段时间了。选择一行时出现错误:

> Uncaught TypeError: Cannot call method 'indexOf' of undefined
> jquery.jqGrid.src.js:2465 $.jgrid.extend.setSelection
> jquery.jqGrid.src.js:2465 jQuery.extend.each jquery-1.7.1.js:658
> jQuery.fn.jQuery.each jquery-1.7.1.js:271 $.jgrid.extend.setSelection
> jquery.jqGrid.src.js:2460 $.fn.jqGrid jquery.jqGrid.src.js:587
> $.fn.jqGrid.each.$.before.click.bind.ts.p.datatype
> jquery.jqGrid.src.js:2235 jQuery.event.dispatch jquery-1.7.1.js:3256
> jQuery.event.add.elemData.handle.eventHandle

我的网格定义在这里:

var sql4 = 'select id_num, est_number, customer, product, rev, w, l, fw, fl, expr1009, status, comments from schema.table where customer = "' + customer + '" and est_number = "' + est_num + '"';
$("#the_table").jqGrid({
    url:'thescript.php?sql=' + sql4,
    height: 300,
    shrinkToFit: true,
    width: 650,
    datatype: 'xml',
    mtype: 'POST',
    colNames:["ID","Estimate","Customer","Product","Rev","W","L","FW","FL","Expr1009","Status","Comments"],
    colModel:[
        {name:"id_num",index:"id_num",width:"10"},
        {name:"est_number",index:"est_number",width:"10"},
        {name:"customer",index:"customer",width:"10"},
        {name:"product",index:"product",width:"10"},
        {name:"rev",index:"rev",width:"10"},
        {name:"w",index:"w",width:"10"},
        {name:"l",index:"l",width:"10"},
        {name:"fw",index:"fw",width:"10"},
        {name:"fl",index:"fl",width:"10"},
        {name:"expr1009",index:"expr1009",width:"10"},
        {name:"status",index:"status",width:"10"},
        {name:"comments",index:"comments",width:"10"}
    ],
    rowNum:10000,
    sortname: 'id_num',
    sortorder: 'asc',
    viewrecords: true,
    gridview: true,
    caption: '',
    ondblClickRow: function(id){
        //do some stuff here
    }
})
.jqGrid('filterToolbar')
.trigger('reloadGrid');

我无法弄清楚问题出在哪里,因为看起来网格在其他方面工作正常。

我注意到在下面的 jqgrid 函数中,id = 1 for ever row(此信息包含在变量 pt 中。

setSelection : function(selection,onsr) {
    return this.each(function(){
        var $t = this, stat,pt, ner, ia, tpsr;
        if(selection === undefined) { return; }
        onsr = onsr === false ? false : true;
        pt=$t.rows.namedItem(selection+"");

下一行是错误发生的地方。变量 pt 有 4 个节点,它们的 id 都是:1。

if(!pt || pt.className.indexOf( 'ui-state-disabled' ) > -1 ) { return; }

** 回答**

我在 ColModelid_num 字段中添加了 key:true 选项。网格的每一行都有相同的 ID,所以它导致了一个问题。我认为当您没有唯一 ID 时,错误最常发生。在你的网格中。

最佳答案

我认为你应该使用 encodeURIComponent以任何方式:

url: 'thescript.php?sql=' + encodeURIComponent(sql4)

代替

url: 'thescript.php?sql=' + sql4

也许你应该发送 sql 参数而不是作为 URL 的一部分,而是在 POST 数据中。在你应该使用的情况下

url: 'thescript.php',
postData: {
    sql: function () {
        return 'select id_num, est_number, customer, product, rev, w, l, fw, fl,' +
            ' expr1009, status, comments from schema.table where customer = "' +
            customer + '" and est_number = "' + est_num + '"';
    }
}

已更新:错误将在 the linessetSelection 的代码。所以 pt.classNameundefined。这很奇怪,但您可以验证网格中没有 id 重复项。

关于javascript - "Uncaught TypeError: Cannot call method ' indexOf ' of undefined"选中jqgrid行时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10852725/

相关文章:

javascript - 无法加载模块脚本 : The server responded with a non-JavaScript MIME type of "text/plain". 严格的 MIME 类型检查 i

javascript - 有条件的然后在 promise 中( bluebird )

javascript - 如何导出一页的本地存储并导入另一页?

javascript - 如何将 url/text 中的符号转换为十六进制字符? (转换 = 到 %3D)

c# - 数据集中的大型 XML 文件(内存不足)

python - 这是什么时间格式? 20151104101700-0800

javascript - 如何在数据库中保存 Google map v3 圆形和矩形叠加层而不会出现错误?

jquery - 在 jQuery 中从数组中排除数组

jquery - 打开Fancybox-2后,将焦点发送到fancybox窗口

javascript - 使用 Javascript 解析 XML 对象数组中的数据