最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - JQGrid getGridParam not returning ID of data item - Stack Overflow

programmeradmin0浏览0评论

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)

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();
            });

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.

The

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

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

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)

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();
            });

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.

The

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

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

Share Improve this question asked May 23, 2013 at 13:29 DevilandDeviland 3,3747 gold badges34 silver badges53 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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.

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.

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.

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 remend you to use gridComplete at all. It's better to use loadComplete. See the answer for details.

发布评论

评论列表(0)

  1. 暂无评论