0

I want to use a custom property of jqgrid to persist some ASP.NET session value on the page, but cannot seem to get it working.

The grid is defined as below, with a custom property called "MyVariable".

$("#myGrid").jqGrid({
    url: RootAbsolutePath + "Customer/GetCustomerList",
    datatype: "json",
    mtype: 'POST',
    page: 1,
    rowNum: 10,
    rowList: [10, 20, 30],
    pager: $("#myPager"),
    toppager: true,
    colNames: column_names,
    colModel: [
        { name: "CUSTOMERNUMBER", index: "CUSTOMERNUMBER", width: 150, align: "center", "formatter": customerEditLink },
        { name: "DEALERSHIPID", index: "DEALERSHIPID", width: 150, align: "center", stype: "select", searchoptions: { "value": dealerShopSelector} },
        { name: "IDENTITYNUMBER", index: "IDENTITYNUMBER", width: 150, align: "center" },
        { name: "CUSTOMERNAME", index: "CUSTOMERNAME", width: 150, align: "left" },
        { name: "CUSTOMERTYPE", index: "CUSTOMERTYPE", width: 120, align: "center", "stype": "select", "searchoptions": { "value": typeSelector} },
        { name: "MOBILE", index: "MOBILE", width: 120, align: "center" },
        { name: "ADDRESS", index: "ADDRESS", width: 400, align: "left" },
    ],
    autowidth: true,
    shrinkToFit: false,
    height: "100%",
    viewrecords: true,
    hoverrows: true,
    sortname: "CUSTOMERNAME",
    sortorder: "asc",
    MyVariable: "Hello World!"
});

In the Controller, I set the value for MyVariable and return as Json data, hoping to persist the value on the grid:

    public JsonResult GetCustomerList()
    {
        var model = new someModel();
        List<object> listOfObjects = new List<object>();
        // do something with the model and get data into listOfObjects

        var jsonData = new
        {
            total = model.TotalPages,
            page = model.page,
            records = model.totalRecords,
            MyVariable = "Hello Controller!",          
            rows = listOfDataObjects
        };

        return Json(jsonData, JsonRequestBehavior.AllowGet);
     }

Now try to access this variale after the page loaded.

var $grid = $('#myGrid');
alert($grid.jqGrid('getGridParam', 'MyVariable'));

It always shows "Hello World", not "Hello Controller!". Does this mean the custom property can not be changed after grid is loaded?

Secondly, in this example if the user selects the CUSTOMERTYPE column header to filter the data, how do I get that filtered criteria value?

I am new to jqGrid and it's maddening to get the simple thing to work. Any help is really appreciated!

Oleg
  • 220,925
  • 34
  • 403
  • 798

1 Answers1

1

If you include some additional property in JSON response returned from the server then you can see it inside of beforeProcessing and loadComplete:

loadComplete: function (data) {
    $(this).jqGrid("setGridParam", {MyVariable: data.MyVariable});
}

or

beforeProcessing: function (data) {
    $(this).jqGrid("setGridParam", {MyVariable: data.MyVariable});
}

Alternatively you can use userdata part of the server response (see the documentation and the answer and this one).

On the other side session specific data are need be saved typically only on the server side or as cookie (as the part of HTTP header). So you should consider whether the usage of custom option of jqGrid is really correct way in your case.

UPDATED: To send MyVariable back to the server you can use for example beforeRequest callback:

beforeRequest: function () {
    var $self = $(this),
        postData = $self.jqGrid("getGridParam", "postData");
    // postData is object which contains information which will be send
    // to the server. we can modify it here
    postData.MyVariable = $self.jqGrid("getGridParam", "MyVariable");
}
Community
  • 1
  • 1
Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Sorry, where exactly should this loadComplete function be added to? I tried to add to the jqGrid, but throws exception that no such method. – user2361287 May 08 '13 at 09:38
  • @user2361287: It's callback function. You should add it like any other option of jqGrid (`colModel`, `datatype` and so on). – Oleg May 08 '13 at 09:40
  • Thanks Oleg. I found my issue is I copied setGridparam. It should be setGridParam. Now I am close to resolve my issue, the custom property MyVariable is updated now on the grid. But I still need to get this variable in the Controller class in C#. Is there a simple way without passing as a url parameter because this variable can contain many special characters and will invalidate the url? – user2361287 May 09 '13 at 02:35
  • @user2361287: Sorry, it was my typing error. To send `MyVariable` back to the server you can extend `postData` inside of `beforeRequest` callback or to use callback `serializeGridData` alternatively. See **UPDATED** part of my answer. – Oleg May 09 '13 at 05:59