jqgrid的自定义属性无法更改?

时间:2013-05-08 08:16:51

标签: jquery jqgrid persistence custom-attributes

我想使用jqgrid的自定义属性在页面上保留一些ASP.NET会话值,但似乎无法使其正常工作。

网格定义如下,其自定义属性名为“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!"
});

在Controller中,我设置了MyVariable的值并作为Json数据返回,希望将值保留在网格上:

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

现在尝试在加载页面后访问此变量。

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

它总是显示“Hello World”,而不是“Hello Controller!”。这是否意味着在加载网格后无法更改自定义属性?

其次,在此示例中,如果用户选择CUSTOMERTYPE列标题来过滤数据,我该如何获得过滤后的标准值?

我是jqGrid的新手,让简单的事情发挥作用令人抓狂。任何帮助都非常感谢!

1 个答案:

答案 0 :(得分:1)

如果您在服务器返回的JSON响应中包含一些其他属性,那么您可以在beforeProcessingloadComplete内看到它:

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

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

或者,您可以使用userdata部分服务器响应(请参阅the documentationthe answer以及this one)。

另一方面,会话特定数据通常只需保存在服务器端或作为cookie(作为HTTP标头的一部分)。所以你应该考虑在你的情况下使用jqGrid的自定义选项是否真的正确。

更新:要将MyVariable发送回服务器,您可以使用例如beforeRequest回调:

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