views:

112

answers:

1

I have a JQGrid with loadonce:true(so it's all client side) and paging enabled(with, say 20 pages).

I would like to specify a row(programatically, without user input) and have my grid navigate to the corresponding page to select the specified row.

Is this possible with the current JQGrid?

I've looked into search and filter, but that just reloads the grid with new rows - I need my grid to navigate to the correct page - Keeping its data and structure.

I'm in the process of optimizing my grid structure, so any changes needed(say client side to server side) would be possible.

Regards, Byron Cobb.

+1  A: 

Because you use loadonce:true, then you prepare the data on the server. On the server side you can decide which row must be selected. On the server side you can also easy calculate on which page will be the selected row. The id of selected row and the selected page you can for example include as a part of the userdata. So the data sent from the server could looks like following:

{
    "total": 5,
    "page": 1,
    "records": 107,
    "rows": [
        ...
    ],
    "userdata": {
        "page": 3,
        "selId": 24 
    }
}

Inside of loadComplete you can do about following

loadComplete: function(data) {
    if (jQuery("#list").getGridParam('datatype') === "json") {
        // data.userdata is the same as jQuery("#list").getGridParam('userData');
        var userdata = jQuery("#list").getGridParam('userData');
        var curPage = jQuery("#list").getGridParam('page'); // is always 1
        if (curPage !== userdata.page) {
            setTimeout(function(){
                jQuery("#list").setGridParam(
                    { page: userdata.page }).trigger("reloadGrid");
                jQuery("#list").setSelection (userdata.selId, true);
            },100);
        }
        else {
            jQuery("#list").setSelection (userdata.selId, true);
        }
    }
}

A working examples you can see on http://www.ok-soft-gmbh.com/jqGrid/DataToSelect.htm and http://www.ok-soft-gmbh.com/jqGrid/DataToMultiSelect.htm.

Oleg
Thank you as usual Oleg! Would this method require me to recreate the grid on the server each time i wanted to navigate to a different page? My program has a tree and the grid. when a user selects an element in the tree the grid will search for that element and select it in the grid.
Byron Cobb
Hi! Because you use `loadonce:true` only the first load will be from the server. I test `getGridParam('datatype') === "json"` in `loadComplete` because jqGrid change in to `"local"` and in the next refresh (paging, sorting, filtering/searching) all works local without server. With tree grids I have no experience, but because it has the same jqGrid API all should also work or one can make toe corresponding code modification based on the same idea.
Oleg
Hi Oleg! I've implemented my selection function based off your advice and it works perfectly! Thanks. I've altered it slightly since my table data never changes - On load I stored a json list of all my rows and their corresponding ids. On selection of the tree node, I look up the page number of the node and select it in the grid.
Byron Cobb
@Byron Cobb: I am glad to hear that you solve your problem! You welcome!
Oleg