我认为你的代码有很多小问题。我准备了演示
https://jsfiddle.net/OlegKi/rmo2370r/19/
,它将修复大多数问题,并演示select2的用法和免费jqGrid的一些特性。
第一个小问题是使用正确的rowid。使用当前隐藏列
{name: "Id", jsonmap: "Id", key: true, hidden: true}
这是使用jqGrid的用户的典型误解。Rowid将另存为
id
行的属性(
<tr>
元素)。看见
the picture
. 不需要将案例信息隐藏起来
<td>
网格内的元素。相反,您可以使用以下jqGrid选项
prmNames: { id: "Id" },
jsonReader: { id: "Id" },
相反选项
jsonReader.id
在填充网格期间通知jqGrid从何处获取rowid,并
prmNames.id
在编辑网格期间提供id的名称。
要在JSFiddle中填充jqGrid,可以使用Echo服务:
url: '/echo/json/',
datatype: 'json',
mtype: 'POST', // required for '/echo/json/'
postData: {
json: JSON.stringify(mydata)
},
对URL的请求
/echo/json/
将
mydata
作为回应。可以使用Chrome/IE/Firefox开发工具的网络选项卡来详细检查HTTP流量。
以同样的方式
editurl: '/echo/json/',
formDeleting: {
url: '/echo/json/',
...
}
用于内联编辑和表单删除。
接下来的变化。我补充道
resetWidthOrg: true
中的属性
autoResizing
:
autoResizing: {
compact: true,
resetWidthOrg: true
}
改变了工作结果
autowidth: true
结合
autoresizeOnLoad: true
. 您可以看到,所有列的宽度都是基于列的内容的,比以前好得多。看见
the issues
了解更多详细信息。
我不明白
customActionsFormatter
. 我将其替换为标准格式化程序操作
{ name: "act", template: "actions" }
免费jqGrid允许在需要时非常轻松地自定义操作按钮。看见
the answer
和
the wiki article
了解更多详细信息。
使用的旧代码
cmTemplate: {
autoResizable: true,
editable: true
}
和设置
editable: false
在most列中。相反,您只需删除
editable: true
从…起
cmTemplate
添加
可编辑:true
仅在一列中,您需要编辑该列,并将其包括在
cmTemplate模板
其他常用设置
colModel
:
cmTemplate: {
width: 300,
autoResizable: true
}
许多其他代码也可以简化。参见修改后的代码
onSelectRow
例如
要自定义删除对话框,可以使用以下设置:
formDeleting: {
url: '/echo/json/', // '/ajax/plans_to_forms/delete/' in final solution
width: 320,
caption: 'Delete Plan to Form Link',
msg: 'Are you sure you want to delete this link?',
beforeShowForm: function ($form) {
var rowids = $form.find("#DelData>td").data("rowids");
console.log(rowids);
if (rowids.length > 1) {
$form.find("td.delmsg")
.html('Are you sure you want to delete all the selected form links?');
}
}
}
删除发送数据
Id=20622,20626
和
oper=del
到服务器(
formDeleting.url
). 可以使用
serializeDelData
如果需要,将数据转换为JSON。
要在编辑期间从列向服务器发送更多数据,可以添加
editable: "hidden"
在某列中。我在中添加了属性
FormId
演示列和编辑期间发送到服务器的数据如下所示
{"FormId":"3393","DrugGroupName":"Some other value","oper":"edit","Id":"20620"}
填写的数据
<select>
对于服务器的其他Ajax请求,需要使用
editoptions.dataUrl
. 我在演示中添加了
editoptions.postData
要仅模拟对服务器的实际请求,请执行以下操作:
editoptions: {
dataUrl: "/echo/json/",
postData: {
json: JSON.stringify([
"Non-Specialty Medications",
"General Pharmacy Authorization",
"Some other value"
])
},
buildSelect: function (data) {
var select = "<select>", i;
for (i = 0; i < data.length; i++) {
select += "<option value='" + String(data[i]).replace(/\'/g, "'") +
"'>" + $.jgrid.htmlEncode(data[i]) + "</option>"
}
return select + "</select>";
},
selectFilled: function(options) {
var $self = $(this);
setTimeout(function() {
$(options.elem).select2({
width: "100%"
}).on('select2:select', function (e) {
// save the data on selection
$self.jqGrid("saveRow", options.rowid);
});
}, 0);
}
},
stype: "select",
searchoptions: {
sopt: ["eq", "ne"],
generateValue: true,
noFilterText: "Any",
selectFilled: function(options) {
$(options.elem).select2({
width: "100%"
});
}
}
}
上述请求
dataUrl
返回JSON字符串
[ "Non-Specialty Medications", "General Pharmacy Authorization", "Some other value" ]
和
buildSelect
使用将数据转换为HTML片段
<选择(>);
包含所有
<options>
. 由此产生的
<选择(>);
将转换为select2 CONTROL内的
selectFilled
回拨。最后,代码使用
ajaxSelectOptions: {
type: "POST",
dataType: "json"
}
将Ajax请求的参数更改为的选项
数据URL
. 演示
https://jsfiddle.net/OlegKi/rmo2370r/19/
包含其他一些小更改,如删除不必要的空寻呼机div和
pager: true
就像你已经用过的一样
toppager: true
. 这是我在免费jqGrid fork中实现的另一个特性,以简化jqGrid的使用。