5

jqGrid用来以表格格式显示数据,使用JSPservlet

编辑

当执行类似的操作时,我想显示来自服务器的错误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;}}},
                    {}) ;

小服务程序代码

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从 jqgrid 记录后,如果No message is shown记录是,则在网格中inserted successfully
  • error Status: 'Unauthorized'. Error code: 401如果 servlet 无法在数据库中插入记录,则会显示。

我的问题是:

  • 从 jqgrid 记录后inserting,如果插入了记录,那么我应该如何向用户显示已插入数据的信息。
  • 以及我应该如何向用户发送消息Error while insertingerror code我应该使用哪个?)

提前致谢.....

4

2 回答 2

5

我在旧答案另一个答案中建议使用现有的隐藏行网格形式 ( tr.tinfo) 来显示不是错误的信息。因为答案并不为人所知,所以我在这里重复相同的信息,但我会尝试更详细地解释所有内容。

首先,重要的是要了解哪些元素具有标准的编辑/添加表单。使用 IE 或 Chrome、Firebug 或许多其他工具的开发人员工具可以很容易地发现 jqGrid 创建的添加/编辑表单在表单顶部包含两个隐藏行

在此处输入图像描述

默认情况下,第一行将用作错误消息的位置。可以使用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;
}

显示错误消息,如

在此处输入图像描述

同样,如果服务器响应包含成功的HTTP 状态代码afterSubmit,则可以在提交编辑/添加的数据后使用回调显示状态消息。的实施可能是关于以下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-13T22:26:19.260 回答
3

我在对我的服务器的编辑调用中做了类似的事情,所以我认为这将以与添加非常相似的方式工作。

在编辑/删除/添加调用之后的控制器上,您将确定是否有要传递给用户的消息,如果是,则通过 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){

因此,在上面的示例中,如果操作失败,您可以显示一条带有 a 的消息,success = false或者如果操作已完成,但您想将一些额外信息传递给用户,您也可以使用sucess = true&& showMessage = true

这是一个 JSON 编辑示例,但概念和逻辑对于 XML 和添加/删除操作应该是相同的。

于 2013-02-13T13:48:01.947 回答