jqGrid getLocalRow returning false when data prope

2019-06-04 04:14发布

问题:

Am having issues using getLocalRow along with data property

var $grid;
getGrid = function () {
    $grid = $("list");
    $grid.jqGrid({
        mtype: "POST",
        colNames: [],
        colModel: [
            ....
        ],
        pager: "",
        loadonce: true,
        multiselect: true,
        gridComplete: function () {
           var data = $(this).getDataIDs();
           for(var i=0; i < data.length;i++){
                $(this).setSelection(data[i]); // select all rows by default
           }
        },
        loadComplete: function (data) {

        },
        loadError: function (xhr) {

        }
    });
    return $grid;
};

There are two ways am populating the grid based on 2 scenarios.
In scenario 1, am just hitting server url and returning the data as JSON and populating in the grid. Using this option, when I iterate thru the selected rows and perform getLocalRow am getting the required o/p.

And in scenario 2, I construct a data object and pass it to the same grid. The problem occurs here when I iterate and use getLocalRow in this context am getting false for all the selected rows instead of the row data but works fine with getRowData.

scenario 1 :

$grid.jqGrid("clearGridData");
$grid.jqGrid("setGridParam", {url: '..', datatype: "json"}).trigger("reloadGrid");

scenario 2 :

$grid.jqGrid("clearGridData");
$grid("setGridParam", { data: MyOWNobject}).trigger("reloadGrid");        

Accessing getLocalRow :

var sel=[];
for (i = 0; i < $grid.jqGrid("getGridParam").selarrrow.length; i++) {
    sel.push($grid.jqGrid("getLocalRow", $grid.jqGrid("getGridParam").selarrrow[i]));
}

回答1:

The problem is in the filling of the grid. Every row of the grid have to get unique id. If your data already have some column with unique data then one can use key: true in the column to inform jqGrid to use the value from the column as the rowid.

I tested last time free jqGrid always with the data, which contains either id property in every column of have key: true property for a column which values one want use as the rowid. If you would follow the rule then the problem which you reports will not exist. See the demo http://jsfiddle.net/OlegKi/y9KHY/92/ which uses the code

$("#submit1").click(function () {
    var $grid = $("#gGrid");
    var grps = [
            { Name : "A",  Group : "11" },
            { Name : "B" , Group : "1122" }
        ];
    $grid.jqGrid({
        data: grps,
        colModel: [
            { name: 'Name', key: true },
            { name: 'Group' }
        ],
        cmTemplate: {width: 200},
        rownumbers: true,
        multiselect: true,
        gridComplete: function () {
            $("#cb_" + this.id).click();
        }
    });
});

$("#submit2").click(function () {
    var $grid = $("#gGrid"), p = $grid.jqGrid("getGridParam"), i;

    for (i = 0; i < p.selarrrow.length; i++) {
        alert("Data  > " + JSON.stringify($grid.jqGrid("getLocalRow", p.selarrrow[i])));
    }
});

The demo http://jsfiddle.net/OlegKi/y9KHY/93/ uses id property in input data.

Nevertheless, the usage of not full input data, without specifying rowid is allowed in general. Thus the described above behavior of free jqGrid is a bug. The bugs is fixed already in the code of free jqGrid on GitHub. You can verify it in the demo http://jsfiddle.net/OlegKi/y9KHY/94/, which uses the code directly from GitHub. I plan publish the next version soon.