无法获得实际的记录ID

时间:2012-03-29 13:43:02

标签: jquery jqgrid jqgrid-php

我创建了从服务器加载数据的jqgrid,我可以在网格中查看数据,但是当我尝试扩展并启动onclickSubmit $.jgrid.del时无法获得实际的记录ID(在我的情况下是101,102)而不是它返回1,2,可能是行索引id。

的jqGrid

jQuery("#eventGrid").jqGrid({
    url:"/eventAllInfo",
    datatype: "json",
    restful:  true,
    mtype: 'GET',
    width:900,

    colNames:['id','title', 'description'],
    colModel:[ 
        {name:'e_info_id',index:'e_info_id', width:60, sorttype:"int",editable:true,editoptions:{size:10}},
        {name:'e_meta_title',index:'e_meta_title', width:90,editable:true,editoptions:{size:10}},
        {name:'e_meta_description',index:'e_meta_description', width:100,editable:true,editoptions:{size:10}},          
    ],
    rowNum:10, rowList:[10,20,30], 
    jsonReader : { repeatitems: false },
    pager: '#pager',        
    caption: "Show Events"
});

JSON响应

{
  "success": true,
  "message": "Records Retrieved Successfully -EventAllInfo",
  "page": "1",
  "total": 1,
  "records": "2",
  "rows": [
    {
      "e_info_id": "101",
      "e_meta_title": "Oracle Business Summit",
      "e_meta_description": null,
      "e_meta_img": null,
      "e_meta_video": null,

    },
    {
      "e_info_id": "102",
      "e_meta_title": "Expo 2014 - Environment",
      "e_meta_description": "",
      "e_meta_img": "",
      "e_meta_video": "",

    }
  ]
}

在json reader中指定id解决了我删除记录的问题,但是当我编辑记录时,我的postdata参数包含

e_info_id: "101"
e_meta_description: ""
e_meta_title: "Oracle Business Summit"
id: "101"
oper: "edit"

当我尝试以postdata.id或postdata.e_info_id的形式访问它时,它返回undefined,这里是编辑的onclickSubmit

 onclickSubmit: function (options, postdata) {
        console.log(postdata);
        console.log(postdata.id); //undefined


        options.url = options.editurl +'/' + encodeURIComponent(postdata.id);
    }

2 个答案:

答案 0 :(得分:2)

查看文档here我认为您应该在jsonReader中指定您的id属性名称。

jsonReader : { repeatitems: false, id: "e_info_id" }

答案 1 :(得分:0)

如果使用jsonReader: { repeatitems: false } jqGrid,则不知道它应该使用哪个值作为命名的rowid。 rowid是网格中id元素的<tr>属性的值。

要解决此问题,您有两种选择:

  1. key: true列中定义e_info_id属性。
  2. id: "e_info_id"中使用jsonReader(请参阅基督徒的回答)
  3. idjsonReader属性的默认值为id: "id"

    了解页面上的id值必须唯一非常重要。例如,如果您在页面上有两个网格,并且两个网格都包含整数ID信息,则可能会发生冲突。在这种情况下,您可以使用idPrefix选项。如果id元素的<tr>属性的值将从idPrefix(在两个原因页面中应该不同)和“标准”id构建