【问题标题】:jQuery unrecognized Expression error in jqGrid dataUrl method for edittype='select'用于edittype ='select'的jqGrid dataUrl方法中的jQuery无法识别表达式错误
【发布时间】:2015-09-09 01:43:41
【问题描述】:

这是this 线程的延续。我正在使用 free-jqGrid 4.9.0 并进行内联编辑。

 { name: 'ContactName',  editable: true, width: 100, sortable: false, frozen: true, formatter: 'select',  edittype: 'select',
                    editoptions: {
                        dataUrl: '/InvestorList/GetContactList',
                        postData: function (rowid, value, cmName) {

                            return { projectId: rowid };
                        }
                    }

在我的 ASP .NET MVC 控制器中,我有以下方法:

public JsonResult GetContactList(string projectId)
    {
        var contacts = new Dictionary<string, string>();//context.GetContactList(projectId);
        contacts.Add("123","IAM1");
        contacts.Add("1234", "IAM2");
        contacts.Add("12345", "IAM3");
        return Json(contacts, JsonRequestBehavior.AllowGet);
    }

使用 IE 开发者工具,这是响应:

{"123":"IAM1","1234":"IAM2","12345":"IAM3"}

但我收到以下错误,

http://localhost:51176/Scripts/jquery-2.1.3.min.js 中第 2 行第 12461 列的未处理异常 0x800a139e - JavaScript 运行时错误:语法错误,无法识别的表达式:{"123":"IAM1","1234":"IAM2","12345":"IAM3"}

我想知道dataUrl 期望什么样的格式,因为我对其他列使用完全相同的格式,例如editoptions: { value: TeaserStatusList } 在这种情况下TeaserStatusList{"123":"IAM1","1234":"IAM2","12345":"IAM3"} 具有相同的格式

谢谢

更新:

{ name: 'ContactId', editable: true, width: 100, sortable: false, edittype: 'select', editoptions: {  dataUrl: '/InvestorList/GetContactList',
                        postData: function (rowid, value, cmName) {                            
                            var accid = $(gridId).jqGrid("getCell", rowid, "AccountId");
                            return { accountId: accid };
                        },
                        buildSelect: function (data) {
                            var s = "<select>";
                            data = JSON.parse(data);
                            $.each(data, function (k, v) {
                                s += '<option value="' + k + '">' + v + '</option>';
                            });
                            return s + "</select>";
                        }
                    }

【问题讨论】:

    标签: jquery asp.net-mvc jqgrid free-jqgrid


    【解决方案1】:

    jqGrid 期望 dataUrl 返回带有 &lt;select&gt; 语句的 HTML 片段。因此,您应该在editoptions 中添加buildSelect 属性,它将服务器响应转换为带有&lt;select&gt; 语句的字符串。相应的代码可能如下所示

    buildSelect: function (data) {
         var s = "<select>", i, l, val;
         if (data != null) {
             for (val in data) {
                 if (data.hasOwnProperty(val)) {
                     s += "<option value='" + val + "'>" + data[val] + "</option>";
                 }
             }
         }
         return s + "</select>";
     }
    

    我没有测试代码,但在我看来它对应于从GetContactList 操作返回的数据格式。

    我建议您在操作 GetContactList 的开头添加行 HttpContext.Current.Response.Cache.SetMaxAge (new TimeSpan(0)); 以确保您不会遇到缓存问题并且每次都会调用操作 GetContactList

    【讨论】:

    • 谢谢@Oleg,还是有问题。该字段显示值而不是该字段的文本。我有ContactIdContactName 用于下拉菜单。在这种情况下,下拉菜单可以正常工作,但是一旦编辑完成,单元格中就没有文本了。如何解决这个问题?
    • @Imran:我看到你使用formatter: 'select'。这很奇怪。 输入数据用于初始填充网格(不用于编辑)的格式是什么?问题很简单:formatter: 'select' 将在填充网格时使用。它需要 editoptions.value 并且不能与 dataUrl 一起使用。此外,ContactName 列的输入数据必须包含像 "123""1234" 这样的,而不是像 "IAM1""IAM2" 这样的文本。见the documentation
    • 所以我删除了formatter: 'select' 并添加了ContactId 作为字段名称。现在,它会在加载网格时显示诸如“123”之类的值。单击该行时,可以看到带有值的下拉列表。我错过了什么吗?
    • :我在问题中添加了我的colmodel
    • @Imran:您现在使用哪些数据作为ContactName 列的输入(在网格中填充数据期间)?是像“IAM1”还是“IAM2”这样的文本?如果您不使用诸如“123”或“1234”之类的ID(值),那么为什么要使用不同的值和文本创建&lt;select&gt;'/InvestorList/GetContactList' 返回 ["IAM1", "IAM2", "IAM3"] 而不是 {"123":"IAM1","1234":"IAM2","12345":"IAM3"} 不是更好吗?如果这是您需要的,那么您应该更改 buildSelect 以处理新的数据格式。
    猜你喜欢
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-21
    相关资源
    最近更新 更多