3

I have been tearing my hair out over the last couple of days. Just as a quick outline of the problem. I am using JqGrid 4.2.0 (latest version at the time of writing). In a single page, I have two grids. One on the left, looking to act as the navigator. I want to load the data on the right-hand grid with data determined by the row ID of the item clicked on the left hand side.

My problem is that the first selected row ID gets "stuck" and all ajax calls in future are the same identical rowid (for example: if first selected row was 514, every other selected row will output 514 on the ajax call to load the other grid, if first selected was 513, all others 513, etc)

I suspect it might be some kind of variables crossing or somesuch as I placed alert calls for testing throughout the execution and they all alert the correct ID number until the point where the next grid is loaded, at which point the row ID becomes the erroneous one.

Here is my code below:

First segment is the initial list on the left with the OnSelectRow call, and the second section of code is for the data grid on the right hand side which actuall holds the data)

renderImportsList = function(url, data, firstrow) {
var cnames = data.names;
var cmodel = data.model;
currentrow = firstrow;
$("#imports_grid").jqGrid({
    url: url + "&type=list",
    //caption: "Imports",
    datatype: "json",
    colNames: cnames,
    colModel: cmodel,
    recordtext: "<b>Imports: {1}</b>",
    autowidth: true,
    rowNum: 10000,
    toolbar: [true,"top"],
    pager: "#imports_grid_pager",
    pgbuttons: false,
    pginput: false,
    viewrecords: true,
    multiselect: false,
    sortorder: "desc",
    sortable: true,
    onSelectRow: function(rowid) {
        if (rowid != firstrow) {
            if ($("#" + firstrow).hasClass("ui-state-highlight"))
                $("#" + firstrow).removeClass("ui-state-highlight")
        }
        setTimeout(function() { 
            // Display import items
            var itype = "checkpoint";
                            alert(rowid); // This returns the right row ID so far
            renderImportItems(url, rowid, itype);
        }, 500);
    },
    loadComplete: function() {
        $("#imports_grid tr").css("border-color", "#666");
        $("tr.jqgroup").css("background", "#e9efff");
        $("tr.jqfoot").css("background", "#ced5e9");
        $("#imports_grid tr.jqfoot td").css("border-right", "none");
        $("#t_imports_grid").css("padding-bottom", "3px");
        $("#imports_grid").setSelection(firstrow, true);
        $("#imports_grid").trigger("reloadGrid"); // Call to fix client-side sorting
    }
});     
$("#imports_grid").jqGrid('navGrid','#imports_grid_pager',{edit:false,add:false,del:false,search:false});
$("#imports_grid").trigger("reloadGrid"); // Call to fix client-side sorting
sizeGrid("imports_grid");

}

This part executes fine, the rowid at this stage is what I clicked on, according to the alert placed above. Below is the second function which is called from inside OnSelectRow in the function above.

renderImportItems = function(url, rowid, itype) {
$.ajax({
    url: srvrname + "applications/PMS/views/view/imports/" + itype + ".php",
    success: function(data) {
        var cnames  = data.names;
        var cmodel  = data.model;
                    alert(rowid); // Here, the code still executes the right row ID
        $("#checkpoint_grid").jqGrid({
            url: url + "&rid=" + rowid + "&type=" + itype,
            // This is where it breaks. No matter what, I keep getting rowid to equal whichever row was selected the very first time the grid was clicked (or loaded programatically onload)
            datatype: "json",
            colNames: cnames,
            colModel: cmodel,
            recordtext: "<b>Total: {1}</b>",
            autowidth: true,
            rowNum: 500,
            pager: "#" + itype + "_grid_pager",
            pgbuttons: false,
            pginput: false,
            viewrecords: true,
            multiselect: false,
            sortorder: "desc",
            sortable: true,
            loadComplete: function() {
                $("#" + itype + "_grid tr").css("border-color", "#666");
                $("tr.jqgroup").css("background", "#e9efff");
                $("tr.jqfoot").css("background", "#ced5e9");
                $("#" + itype + "_grid tr.jqfoot td").css("border-right", "none");
                $("#" + itype + "_grid").trigger("reloadGrid"); // Call to fix client-side sorting
            }
        }); 
        $("#" + itype + "_grid").jqGrid('navGrid','#' + itype + 'grid_pager',{edit:false,add:false,del:false,search:false});
        $("#" + itype + "_grid").trigger("reloadGrid"); // Call to fix client-side sorting
        sizeGrid(itype + "_grid");
    }
});

}

As you can see above: at the point at which the first alert is called; it is still outputting the correct ID number, but as soon as the second grid is initialized; the ID returns to whatever it was inintially set on the very first call.

Any help provided would be greatly appreciated. If it helps, here are some firebug outputs to demonstrate the issue...

(Domain name removed for privacy)

(First load: programmatic: firstrow selected = 514) Response: //mydomain.com/views/view/grid.php?rid=514&type=checkpoint&_search=false&nd=1321336809180&rows=500&page=1&sidx=&sord=desc

(Clicked row: selected row = 503) Response: //mydomain.com/views/view/grid.php?rid=514&type=checkpoint&_search=false&nd=1321336863994&rows=500&page=1&sidx=&sord=desc

(Clicked row: selected row = 510) Response: //mydomain.com/views/view/grid.php?rid=514&type=checkpoint&_search=false&nd=1321336864848&rows=500&page=1&sidx=&sord=desc

Preller
  • 33
  • 1
  • 3

3 Answers3

2

I've experienced the same problem myself. I recommend you define your grid outside the onSelectRow function with datatype set to "local" and only change the parts that change between each load within onSelectRow:

$("#checkpoint_grid").jqGrid('setGridParam', {
    url: null
}).jqGrid('setGridParam', {
    url: url + "&rid=" + rowid + "&type=" + itype,
    datatype: "json"
}).trigger("reloadGrid");

I usually do this with postData: null, but I think the underlying problem is that jqGrid caches some grid params.

flesk
  • 7,439
  • 4
  • 24
  • 33
2

You should include GridUnload for the $("#checkpoint_grid") inside of renderImportItems (for example after var cmodel = data.model;):

$("#checkpoint_grid").jqGrid('GridUnload');

The problem is that the code which create grid should be executed once. The code create for example grid headers, pager and some other areas excepting the grid body. Then the Ajax request will be made to get the data for the grid and to fill the body. If the user click on the column header to sort the data by the column or if the user click on the "Next page" button only the data will be refreshed in the grid. So one should create grid only once. If the next call will be done to already existing grid the call will be just ignored. It's the line of code (the internal property grid will be set here).

Additionally I would be included cache: false parameter at least in the second $.ajax call (the call inside of inside of renderImportItems).

Here you will find a demo which uses GridUnload.

Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
0

I think the following steps should work:

  1. Call the onRowSelect function. Set async: false for your ajax call (optional) and rowid should be kept as a global var
  2. Reload the 2nd grid based on the primary key data from the 1st grid using This
Community
  • 1
  • 1
Rohan
  • 1,705
  • 7
  • 17
  • 38