JQGrid getGridParam不返回数据项的ID [英] JQGrid getGridParam not returning ID of data item

查看:131
本文介绍了JQGrid getGridParam不返回数据项的ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网格正在使用从外部ajax请求生成的本地数据(通过外部,我的意思是不使用jqgrid的内置ajax)

I have a grid that is using local data generated from an external ajax request (by external I mean not using the inbuilt ajax of jqgrid)

ajax请求中的数据作为本地数据传递到网格中,并且网格设置为loadonce.

the data from the ajax request is passed into the grid as local data and the grid is set to loadonce.

window.grid = $(window.tableName).jqGrid({
            datatype: "local",
            data: DataFromResponse,
            height: "auto",
            autowidth: true,
            hidegrid: false,
            ignoreCase: true,
            loadonce: true,
            pager: window.pagerName,
            rowNum: 10,
            viewrecords: true,
            gridview: true,
            caption: '',
            colNames :[cols],
            colmodel :[]{cols},
            gridComplete: function() {
                CreateButtons();
            });

如您所见,网格在此阶段是基本的,但是我们需要在名为'buttons'的列中添加一些按钮,我们使用 CreateButtons 方法来执行此操作.

As you can see the grid is basic at this stage, however we need to add some buttons within a column named 'buttons' we use the CreateButtons method to do this.

function CreateButtons(){
   var grid = $(window.tableName);
   var data = grid.jqGrid('getGridParam', 'data');
   $.each(data, function(index, item){
      var cl = item._id_;
      alert(cl);
   });    
}

此功能的警报应该给我网格设置的行的ID,但它没有任何作用

the alert of this function should give me the id of the row as set by the grid but it is not working any ideas

推荐答案

仅当从远程源(datatype"xml""json")加载的数据时,内部data参数中才会存在属性_id_.并使用了选项loadonce: true.如果一个在本地加载数据(一个使用datatype: "local"),则_id_不存在.

The property _id_ exist in internal data parameter only if the data loaded from remote source (datatype is "json" of "xml") and the option loadonce: true was used. If one load the data locally (one uses datatype: "local") then _id_ is not exist.

如果正确填充网格,则由data参数(在您的情况下为DataFromResponse)指定的数据是包含id属性以及用于填充列数据的其他属性的项目数组.另一种选择是为某些列指定key: true.在这种情况下,该行的id将从该列获取.

If you correctly fill the grid the data specified by data parameter (DataFromResponse in your case) is array of items which contains id property together with the other properties used to fill the column data. One other option is specifying of key: true for some column. In the case the id for the row will be get from the column.

如果未指定和id信息(这很糟糕),并且需要访问所有本地数据(包括jqGrid生成的ID),则需要获得两个jqGrid选项:_indexdata.对象_index包含网格的所有ID作为属性. id属性的值是data数组中与id对应的整数索引.因此,您可以使用for-in循环枚举网格的所有ID(您需要枚举_index的属性).参见答案这一个以获得详细信息.

If you don't specified and id information (which is very bad) and you need to access all local data inclusive the ids generated by jqGrid you need get two jqGrid options: _index and data. The object _index contains as properties all ids of the grid. The value of the id property is integer index in data array which corresponds the id. So you can use for-in loop to enumerate all ids of the grid (you need enumerate properties of _index). See the answer and this one for details.

您另外写道,您在gridComplete内的网格的一栏中创建了按钮.该方法似乎对我不好.最好将自定义格式器与gridview: true选项一起使用(请参见答案).此外,我不建议您完全使用gridComplete.最好使用loadComplete.有关详细信息,请参见答案.

You wrote additionally that you created buttons in a column of the grid inside of gridComplete. The approach seems me not good. It's better to use custom formatter instead together with gridview: true option (see the answer). Moreover I don't recommend you to use gridComplete at all. It's better to use loadComplete. See the answer for details.

这篇关于JQGrid getGridParam不返回数据项的ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆