jqgrid如何显示服务器端消息
我使用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 = ''+ '' + response.statusText + 'Inserted'+ '', $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 '' + 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
对于上面的例子
- 从jqgrid
inserting
记录后,如果inserted successfully
inserting
记录,则网格中No message is shown
-
error Status: 'Unauthorized'. Error code: 401
error Status: 'Unauthorized'. Error code: 401
如果servlet无法在数据库中插入记录,则显示error Status: 'Unauthorized'. Error code: 401
。
我的问题是:
- 从jqgrid
inserting
记录后,如果inserting
记录,那么我应该如何显示消息,向用户提供插入数据的信息。 - 如何
Error while inserting
向用户发出Error while inserting
消息(我应该使用哪个error code
?)
提前致谢…..
我在旧答案和另一个 答案中建议使用现有的隐藏行格式( tr.tinfo
)来显示非错误的信息。 因为答案不是很清楚我在这里重复相同的信息,但我会尝试解释所有更详细的信息。
首先,了解哪些元素具有标准的编辑/添加表单非常重要。 使用IE或Chrome,Firebug或许多其他工具的开发人员工具,可以很容易地发现jqGrid创建的添加/编辑表单在表单顶部包含两个隐藏的行 :
默认情况下,第一行将用作错误消息的位置。 可以使用errorTextFormat
来自定义信息。
如果服务器响应包含错误的HTTP状态代码(> = 400),那么将调用回调errorTextFormat
并且您可以使用
errorTextFormat: function (response) { return response.responseText; }
或类似的东西
errorTextFormat: function (response) { return '' + response.responseText; }
显示错误消息
以同样的方式,如果服务器响应包含成功的HTTP状态代码,则可以在提交编辑/添加的数据之后使用afterSubmit
回调来显示状态消息。 afterSubmit
的实现可以是以下内容
afterSubmit: function (response) { var myInfo = ''+ '' + response.responseText + '', $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秒的状态消息,然后使用jQuery.slideUp动画隐藏它。 它看起来像
我希望这是你需要的。
我在对服务器的编辑调用中做了类似的事情,所以我认为这将以与添加类似的方式工作。
在编辑/删除/添加调用之后的控制器上,您将确定是否有要传递给用户的消息,如果是,则通过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:
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
的消息,或者如果操作已完成,但您想将一些额外信息传递给用户,您也可以使用showMessage = true
&& showMessage = true
。
这是编辑的JSON示例,但XML和添加/删除操作的概念和逻辑应该相同。