【问题标题】:Custom property of jqgrid cannot be changed?jqgrid的自定义属性不能更改?
【发布时间】:2013-05-08 08:16:50
【问题描述】:

我想使用 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 的新手,让简单的事情工作起来令人抓狂。非常感谢任何帮助!

【问题讨论】:

    标签: jquery jqgrid persistence custom-attributes


    【解决方案1】:

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

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

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

    或者,您可以使用服务器响应的userdata 部分(请参阅the documentationthe answerthis 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");
    }
    

    【讨论】:

    • 对不起,这个 loadComplete 函数到底应该添加到哪里?我试图添加到 jqGrid,但抛出没有这种方法的异常。
    • @user2361287:这是回调函数。您应该像 jqGrid 的任何其他选项一样添加它(colModeldatatype 等)。
    • 谢谢奥列格。我发现我的问题是我复制了 setGridparam。它应该是 setGridParam。现在我即将解决我的问题,自定义属性 MyVariable 现在已在网格上更新。但是我仍然需要在 C# 的 Controller 类中获取这个变量。有没有不作为url参数传递的简单方法,因为这个变量可以包含许多特殊字符并且会使url无效?
    • @user2361287:对不起,这是我的打字错误。要将MyVariable 发送回服务器,您可以在beforeRequest 回调中扩展postData,或者使用回调serializeGridData。请参阅我的回答的已更新部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2015-08-11
    • 1970-01-01
    • 2014-06-08
    • 2011-02-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多