我有一个网页,用于填充jqGrid中的JSON对象(版本4.6.0)。 JSON对象是从java控制器返回的。我如何初始化网格以及如何填充JSON对象如下所示。
问题在于,无论何时单击列标题,我都会丢失以下列中的数据:组织,部门和标题。通过“丢失数据”,我的意思是这些列中的数据由于某种原因消失了。这些列恰好是嵌套的JSON对象。我在这里错过了什么?感谢。
这是从服务器返回的JSON字符串。
[
{"id":51,"displayName":"John Doe",
"currentExperience": {"id":26,"orgName":"compnay A","deptName":"MIS","title":"senior software engineer"}},
{"id":52,"displayName":"Jane Doe",
"currentExperience": {"id":29,"orgName":"compnay B","deptName":"MIS","title":"software engineer"}}
]
function initializeGrid($grid, $pager, caption)
{
$grid.jqGrid({
datatype: "local",
colNames: ["VipId", "ExpId", "Name", "organization", "department", "title"],
colModel: [
{ name: "id", hidden: true },
{ name: "currentExperience.id", hidden: true },
{ name: "displayName", width: 100, sortable:true },
{ name: "currentExperience.orgName", width: 100, sortable:true },
{ name: "currentExperience.deptName", width: 100, sortable:true },
{ name: "currentExperience.title", width: 100, sortable:true }
],
pager: $pager,
rowNum: 10,
rowList: [10, 20, 30],
sortname: "id",
sortorder: "asc",
viewrecords: true,
gridview: true,
autoencode: true,
caption: caption,
multiselect: true,
width: 450,
hidegrid: false
});
}
function wireClickEventForSearchVipsButton($form)
{
$(document).on("click", "#btnSearchVips", function(event){
event.preventDefault();
var dataMap = composeSearchVipParameters($form);
<c:url var="link" value="/protected/vips/lookup" />
$.ajax({
url: '${link}',
type: 'GET',
dataType: 'json',
accepts: {
text: "application/json"
},
data: dataMap,
error: function() {
alert('an error occurred');
},
success: function(response) {
// this is how I populate response from the server
if (response) {
$grid = $('#gridAvailableVips');
if ($grid) {
$grid.jqGrid("clearGridData");
for(var i=0;i<=response.length;i++) {
$grid.jqGrid('addRowData',i+1,response[i]);
}
}
}
else {
}
}
}); // end of $.ajax()
}); // end of $(document).on()
}
答案 0 :(得分:0)
谢谢,奥列格。使用以下方法时,问题就消失了。
function initializeGrid($grid, $pager, caption, json)
{
$grid.jqGrid({
datastr: json,
datatype: "jsonstring",
colNames: ["VipId", "ExpId", "Name", "organization", "department", "title"],
colModel: [
{ name: "id", hidden: true },
{ name: "currentExperience.id", hidden: true },
{ name: "displayName", width: 100, sortable:true },
{ name: "currentExperience.orgName", width: 100, sortable:true },
{ name: "currentExperience.deptName", width: 100, sortable:true },
{ name: "currentExperience.title", width: 100, sortable:true }
],
pager: $pager,
rowNum: 10,
rowList: [10, 20, 30],
sortname: "id",
sortorder: "asc",
viewrecords: true,
gridview: true,
autoencode: true,
caption: caption,
multiselect: true,
width: 450,
hidegrid: false
});
}
function wireClickEventForSearchVipsButton($form)
{
$(document).on("click", "#btnSearchVips", function(event){
event.preventDefault();
var dataMap = composeSearchVipParameters($form);
<c:url var="link" value="/protected/vips/lookup" />
$.ajax({
url: '${link}',
type: 'GET',
dataType: 'json',
accepts: {
text: "application/json"
},
data: dataMap,
error: function() {
alert('an error occurred');
},
success: function(response) {
// this is how I populate response from the server
if (response) {
$grid = $('#gridAvailableVips');
if ($grid) {
$grid.jqGrid('GridUnload');
initializeGrid($('#gridAvailableVips'), $pagerAvailableVips, 'Available Vips', response);
}
}
else {
}
}
}); // end of $.ajax()
}); // end of $(document).on()
}