jQuery的未确认前$在jqGrid的dataUrl方法P $ pssion错误edittype ='选择' [英] jQuery unrecognized Expression error in jqGrid dataUrl method for edittype='select'
问题描述
这是在<延续href=\"http://stackoverflow.com/questions/30981085/how-to-pass-parameter-to-editoptions-dataurl-method-for-edittype-select-for-in/30981678?noredirect=1#comment50022434_30981678\">this线。我使用的自由的jqGrid 4.9.0 并有内联编辑。
{名称:'联系人姓名',可编辑:真,宽度:100,排序:假的,冷冻的:真实,格式:'选择',edittype:'选择',
editoptions:{
dataUrl:'/ InvestorList / GetContactList',
POSTDATA:功能(ROWID,价值,cmName){ 返回{专案编号:ROWID};
}
}
在我的ASP.NET MVC的控制器,我有以下方式:
公共JsonResult GetContactList(字符串专案编号)
{
VAR接触=新的解释和LT;字符串,字符串&GT;(); // context.GetContactList(专案编号);
contacts.Add(123,IAM1);
contacts.Add(1234,IAM2);
contacts.Add(12345,IAM3);
返回JSON(联系人,JsonRequestBehavior.AllowGet);
}
使用IE浏览器的开发者工具,这是响应:
{123:IAM1,1234:IAM2,12345:IAM3}
不过,我得到以下错误,
未处理的异常在第2行,列在12461 的http://本地主机:51176 /Scripts/jquery-2.1.3.min.js
0x800a139e - JavaScript的运行时错误:语法错误,无法识别的前pression:{123:IAM1,1234:IAM2,12345:IAM3}
块引用>我想知道什么样的格式呢
dataUrl
期待,因为我用的是完全一样的格式为其他栏目如editoptions:{值:TeaserStatusList}
在这种情况下TeaserStatusList
有相同的格式为{ 123:IAM1,1234:IAM2,12345:IAM3}
感谢
更新:
{名称:'的ContactID,编辑:真,宽度:100,排序:假的,edittype:'选择',editoptions:{dataUrl:'/ InvestorList / GetContactList',
POSTDATA:功能(ROWID,价值,cmName){
VAR ACCID = $(gridId).jqGrid(getCell,ROWID,帐户ID);
返回{帐户ID:ACCID};
},
buildSelect:功能(数据){
变种S =&LT;选择&gt;中;
数据= JSON.parse(数据);
$。每个(数据,功能(K,V){
S + ='&LT;期权价值=+ K +'&GT;' + V +'&LT; /选项&GT;';
});
返回小号+&LT; /选择&gt;中;
}
}
解决方案jqGrid的期望
像dataUrl
返回HTML片段与&LT;选择&GT;
语句。因此,你应该添加buildSelect
地产editoptions
它转换为&LT服务器响应字符串;选择&GT;
语句。相应的code可能会低于buildSelect:功能(数据){
变种S =&LT;选择&gt;中,I,L,缬氨酸;
如果(数据!= NULL){
对于(VAL数据){
如果(data.hasOwnProperty(VAL)){
S + =&LT;期权价值='+ VAL +'&gt;中+数据[VAL] +&LT; /选项&gt;中;
}
}
}
返回小号+&LT; /选择&gt;中;
}我没有测试code,但在我看来,它对应的返回数据的格式为:
GetContactList
操作。我会建议你添加行
HttpContext.Current.Response.Cache.SetMaxAge(新的TimeSpan(0));
在动作的开头GetContactList
,以确保你将有缓存没有问题,行动GetContactList
将被称为每次。This is in continuation of this thread. I'm using the free-jqGrid 4.9.0 and have inline editing.
{ 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 }; } }
In my ASP .NET MVC controller, I have the following method:
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); }
Using IE developer tools, this is the response:
{"123":"IAM1","1234":"IAM2","12345":"IAM3"}
But I get the following error,
Unhandled exception at line 2, column 12461 in http://localhost:51176/Scripts/jquery-2.1.3.min.js 0x800a139e - JavaScript runtime error: Syntax error, unrecognized expression: {"123":"IAM1","1234":"IAM2","12345":"IAM3"}
I'm wondering what kind of format does
dataUrl
expect because I use the exact same format for other columns e.g.editoptions: { value: TeaserStatusList }
In this caseTeaserStatusList
has the same format as{"123":"IAM1","1234":"IAM2","12345":"IAM3"}
Thanks
Update:
{ 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>"; } }
解决方案jqGrid expect that
dataUrl
returns HTML fragment with<select>
statement. Thus you should addbuildSelect
property ineditoptions
which converts the server response to the string with<select>
statement. The corresponding code could be something like belowbuildSelect: 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>"; }
I didn't tested the code, but it seems to me it corresponds the format of data returned from
GetContactList
action.I would recommend you to add the line
HttpContext.Current.Response.Cache.SetMaxAge (new TimeSpan(0));
at the begining of the actionGetContactList
to be sure that you will have no problem with caching and the actionGetContactList
will be called every time.这篇关于jQuery的未确认前$在jqGrid的dataUrl方法P $ pssion错误edittype ='选择'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!