我使用jqGrid以表格格式显示数据,使用JSP和servlet。
编辑
当执行像insert, update, delete这样的操作时,我想显示来自服务器的错误。(datatype: "xml")
JQGrid
jQuery("#list10_d").jqGrid({
height:250,
width:600,
url:'Assignment?action=Assign',
datatype: "xml",
colNames:['Sr. No.','PID', 'DATE', 'EMPID'],
colModel:[{name:'srNo',index:'srNo', width:30,sortable:false},
{name:'PID',index:'PID',width:0, sortable:true,editable:false},
{name:'DATE',index:'DATE', width:75,sortable:true,editable:true,editoptions: { dataInit: function(el) { setTimeout(function() { $(el).datepicker({dateFormat:"dd-M-yy",showButtonPanel: true,changeYear: true,changeMonth: true}).attr('readonly','readonly'); }, 200); }}},
{name:'EMPID',index:'EMPID', width:150,sortable:true,editable:true}
],
rowNum:10,
rowList:[10,20,50,100],
pager: '#pager10_d',
sortname: 'PID',
viewrecords: true,
sortorder: "asc",
},
multiselect: true,
editurl: "Assignment?action=Edit",
caption:"Assignment"
} ).navGrid('#pager10_d',{edit:false,add:true,del:false,addtext:'Assign '},
{},
{modal:true,jqModal: false,closeOnEscape:true,savekey: [true,13],closeOnEscape:true, recreateForm: true,width:500,mtype:'POST', url: 'Assignment',editData:{action: 'Assign',PID: function () {return PID;}},
afterSubmit: function (response) {
alert('After Submit \n' +'statusText: '+ response.statusText);
var myInfo = '<div class="ui-state-highlight ui-corner-all">'+
'<span class="ui-icon ui-icon-info" ' +
'style="float: left; margin-right: .3em;"></span>' +
response.statusText + 'Inserted'+
'</div>',
$infoTr = $("#TblGrid_" + $.jgrid.jqID(this.id) + ">tbody>tr.tinfo"),
$infoTd = $infoTr.children("td.topinfo");
$infoTd.html(myInfo);
$infoTr.show();
// display status message to 3 sec only
setTimeout(function () {
$infoTr.slideUp("slow");
}, 5000);
return [true, "", ""]; // response should be interpreted as successful
},
errorTextFormat: function (response) {
alert('Error Text Format: \n' +'statusText: '+ response.statusText);
return '<span class="ui-icon ui-icon-alert" ' +
'style="float:left; margin-right:.3em;"></span>' +
response.statusText;},
{closeOnEscape:true, recreateForm: true,mtype: 'POST',url: 'Assignment',delData: {action: 'Delete',PID: function () {return PID;}}},
{}) ;Servlet代码
if(request.getParameter("action").equalsIgnoreCase("Assign"))
{
PID = request.getParameter("PID");
String DATE= request.getParameter("DATE");
String EMPID= request.getParameter("EMPID");
String query = "insert into ASSIGN(PID,DATE,EMPID) values('"+ PID +"','"+ DATE +"','"+ EMPID"')";
boolean b = insert.InsertData(query);
if(b)
{
System.out.println("New record added successfully! : "+query);
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
//response.sendError(200, "success");
response.setStatus(200, "Inserted successfully");
}
else
{
System.out.println("Failed to add Record! : "+query);
response.setContentType("text/xml");
response.setCharacterEncoding("UTF-8");
//response.sendError(399, "not Inserted successfully");
response.setStatus(404, "Error while inserting");
}
}//INSERT对于上面的例子
inserting之后,如果记录为inserted successfully,则网格中的No message is shownerror Status: 'Unauthorized'. Error code: 401。我的问题是:
inserting记录之后,如果插入了该记录,那么我应该如何向用户显示将数据插入的消息。Error while inserting (我应该为此使用哪个error code?)提前谢谢.
发布于 2013-02-13 22:26:19
在旧的答案和另一个中,我建议使用现有的隐藏网格表单行(tr.tinfo)来显示不存在错误的信息。因为答案并不为人所知,所以我在这里重复同样的信息,但我将尝试解释更详细的内容。
首先,了解哪些元素具有标准的编辑/添加表单是很重要的。使用IE或Chrome、Firebug或许多其他工具的开发工具可以很容易地发现,jqGrid创建的Add/Edit表单在表单上包含两个隐藏行

默认情况下,第一行将用作错误消息的位置。我们可以使用errorTextFormat来定制一些信息。
如果服务器响应包含错误HTTP状态代码(>=400),则将调用回调errorTextFormat,您可以使用
errorTextFormat: function (response) {
return response.responseText;
}或者类似的东西
errorTextFormat: function (response) {
return '<span class="ui-icon ui-icon-alert" ' +
'style="float:left; margin-right:.3em;"></span>' +
response.responseText;
}显示错误消息,如

同样,如果服务器响应包含成功的afterSubmit,则可以使用HTTP状态码回调在提交经过编辑/添加的数据后显示状态消息。afterSubmit的实现可以有以下几个方面
afterSubmit: function (response) {
var myInfo = '<div class="ui-state-highlight ui-corner-all">'+
'<span class="ui-icon ui-icon-info" ' +
'style="float: left; margin-right: .3em;"></span>' +
response.responseText +
'</div>',
$infoTr = $("#TblGrid_" + $.jgrid.jqID(this.id) + ">tbody>tr.tinfo"),
$infoTd = $infoTr.children("td.topinfo");
$infoTd.html(myInfo);
$infoTr.show();
// display status message to 3 sec only
setTimeout(function () {
$infoTr.slideUp("slow");
}, 3000);
return [true, "", ""]; // response should be interpreted as successful
}代码将显示状态消息3秒,只有abd然后使用jQuery.slideUp动画隐藏它。看起来就像

我希望这是你所需要的。
发布于 2013-02-13 13:48:01
在对我的服务器进行编辑调用时,我做了一些类似的工作,所以我认为这将以非常类似于add的方式工作。
在控制器上,在一个编辑/删除/添加调用之后,您将确定是否有一条消息要传递给用户,如果是这样,则通过JSON (在您的例子中是XML)传递给网格。
例如
if (infoDialogTrigger) {
return Json(new { success = true, showMessage = true, message = "Updating your Inventory and we are displaying this info box" });
}//if
else {
return Json(new { success = true, showMessage = false, message = "" });
}//else在您的jqGrid中,您将拥有编辑/删除/添加函数:
function EditCollectionItem (rowid, grid){
$(grid).jqGrid('editGridRow', rowid,
{
viewPagerButtons: false,
editData: { },
afterComplete: function (response) {
var DialogVars = $.parseJSON(response.responseText); //parse the string that was returned in responseText into an object
//if there was a failure with the update, or there was information to pass to the user
if (!DialogVars.success || DialogVars.showMessage) {
alert(DialogVars.message);
}
} //afterComplete
}); //$(grid).jqGrid('editGridRow
}//function EditCollectionItem (rowid, grid){因此,在上面的示例中,如果操作失败,您可以使用success = false显示消息,或者如果操作已经完成,但是您希望向用户传递一些额外的信息,您也可以使用sucess = true && showMessage = true。
这是一个编辑的JSON示例,但是XML和add/delete操作的概念和逻辑应该是相同的。
https://stackoverflow.com/questions/14396701
复制相似问题