我正在向 jqgrid 添加新数据。数据与客户相关,因此新记录必须包含客户 ID。因此,我希望用于添加记录的弹出表单应该带有预先填充的客户代码(只读)。客户代码在下拉列表中可用。我尝试了以下方法 - 添加了一个带有自定义函数 AddRow 的自定义按钮(这是从 Internet 借来的代码)。在函数内部,我使用 setColProp 将 CustomerId 设置到字段中。然而,什么也没有发生。甚至没有弹出窗口出现。有没有更简单的方法来做同样的事情?
jQuery("#list").jqGrid({
url: '<%= ResolveClientUrl("~/service/OfficeData.asmx/GetDealer_SMS") %>',
editurl: '<%= ResolveClientUrl("~/service/OfficeData.asmx/InsertDealer_SMS") %>',
datatype: "json",
mtype: 'POST',
ajaxGridOptions: { contentType: 'application/json; charset=utf-8' },
ajaxEditOptions: { ContentType: 'application/json; charset=utf-8', dataType: 'json' },
serializeGridData: function (postData) {
//alert("Call");
if (postData.DealerCode === undefined) { postData.DealerCode = DealerCode; }
else {
postData.DealerCode = DealerCode;
}
return JSON.stringify(postData);
},
jsonReader: { repeatitems: false, root: "d.rows", page: "d.page", total: "d.total", records: "d.records" },
colNames: ['ID', 'DealerCode', 'Name', 'Designation', 'Address', 'EMail', 'Mobile'],
colModel: [{ name: 'id', index: 'id', width: 10, align: 'left', editable: true, hidden: true,
editrules: { required: false },
editoptions: {
dataInit: function (element) {
$(element).attr("readonly", "readonly");
}
}
},
{ name: 'DealerCode', index: 'DealerCode', width: 60, align: 'left', editable: true,
editrules: { required: true },
editoptions: {
dataInit: function (element) {
$(element).attr("readonly", "readonly");
}
}
},
{ name: 'RecpName', index: 'RecpName', width: 150, align: 'left', editable: true,
editrules: { required: true }, editoptions: { size: 30, maxlength: 150 }
},
{ name: 'RecpDesignation', index: 'RecpDesignation', width: 100, align: 'left', editable: true,
editrules: { required: true }, editoptions: { size: 30, maxlength: 150 }
},
{ name: 'RecpAddress', index: 'RecpAddress', width: 250, align: 'left', editable: true,
editrules: { required: true }, editoptions: { size: 30, maxlength: 400 }
},
{ name: 'RecpEMail', index: 'RecpEMail', width: 150, align: 'left', editable: true,
editrules: { required: true }, editoptions: { size: 30, maxlength: 150 }
},
{ name: 'RecpMobile', index: 'RecpMobile', width: 100, align: 'left', editable: true,
editrules: { required: true }, editoptions: { size: 30, maxlength: 30 }
}],
loadError: function (jqXHR, textStatus, errorThrown) {
alert('Error while loading -' + errorThrown);
},
pager: jQuery('#pager'),
rowNum: 20,
rowList: [10, 20, 50],
sortname: 'RecpName',
sortorder: "asc",
viewrecords: true,
imgpath: '/scripts/themes/steel/images',
caption: 'Dealer Recepient Details'
}).jqGrid('navGrid', '#pager', { edit: false, add: false, del: false, search: false })
.navButtonAdd('#pager',
{ caption: "Add",
buttonicon: "ui-icon-plus",
onClickButton: addRow,
title: "",
cursor: "pointer"
}
)
.navButtonAdd('#pager',
{ caption: "Edit",
buttonicon: "ui-icon-pencil",
onClickButton: editRow,
position: "last",
title: "",
cursor: "pointer"
}
).navButtonAdd('#pager',
{ caption: "Delete",
buttonicon: "ui-icon-trash",
onClickButton: deleteRow,
position: "last",
title: "",
cursor: "pointer"
}
);
function addRow() {
alert(DealerCode);
$("#grid").jqGrid('setColProp', 'DealerCode', { editoptions: { readonly: true, size: 10, value: DealerCode} });
$('#grid').jqGrid('editGridRow','new',
{ url: '<%= ResolveClientUrl("~/service/OfficeData.asmx/InsertDealer_SMS") %>',
editData: {},
serializeEditData: function(data){
data.id = 0;
return $.param(data);
},
recreateForm: true,
beforeShowForm: function(form) {
$('#pData').hide();
$('#nData').hide();
},
beforeInitData: function(form) {},
closeAfterAdd: true,
reloadAfterSubmit:true,
afterSubmit : function(response, postdata)
{
var result = eval('(' + response.responseText + ')');
var errors = "";
if (result.success == false) {
for (var i = 0; i < result.message.length; i++) {
errors += result.message[i] + "<br/>";
}
} else {
$('#msgbox').text('Entry has been added successfully');
$('#msgbox').dialog(
{ title: 'Success',
modal: true,
buttons: {"Ok": function() {
$(this).dialog("close");}
}
});
}
// only used for adding new records
var newId = null;
return [result.success, errors, newId];
}
});
} // end of addRow
提前致谢