Jqgrid验证响应同时包含onSucces和On错误

时间:2014-11-10 14:43:18

标签: jqgrid jqgrid-formatter

我正在服务器端执行验证,并根据执行的验证返回值。但是,在on success success函数之后我的控制转到onError函数。

以下是我的代码。请求帮助。谢谢。

 formatoptions: {
            restoreAfterError:false,
            keys: true, // we want use [Enter] key to save the row and [Esc] to cancel editing.
            onEdit: function (rowid) {

                $('#newDetailCodeGroup').attr('disabled','disabled').addClass("btnDisabled").removeClass("btnNormalInactive");
                //alert("in onEdit: rowid=" + rowid + "\nWe don't need return anything");
            },
            onSuccess: function (jqXHR) {
                $('#newDetailCodeGroup').attr('disabled','disabled').addClass("btnDisabled").removeClass("btnNormalInactive");
                $('input[id*="gs_"]').val("");
                var selRow = $grid.jqGrid('getGridParam', 'selrow');
                var errors = jQuery.parseJSON( jqXHR.responseText );
                if(!jQuery.isEmptyObject( errors ))
                {
                    if(errors.groupDesc){
                        jQuery('#' + selRow + '_groupDesc').css({'background':'#fff0f0','border':'1px solid red','color':'red'});
                        jQuery('#' + selRow + '_groupDesc').attr('title',errors.groupDesc);
                    }
                return false;
                }else{
                    $grid.setGridParam({ search: false, postData: { "filters": ""} ,datatype: 'json'}).trigger("reloadGrid");
                }



                // the function will be used as "succesfunc" parameter of editRow function
                // (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#editrow)
                /*alert("in onSuccess used only for remote editing:" +
                    "\nresponseText=" + jqXHR.responseText +
                    "\n\nWe can verify the server response and return false in case of" +
                    " error response. return true confirm that the response is successful");
                // we can verify the server response and interpret it do as an error
                // in the case we should return false. In the case onError will be called
                return true;*/
            },
            onError: function (rowid, jqXHR, textStatus) {
                $grid.setGridParam({ search: false, postData: { "filters": ""} ,datatype: 'json'}).trigger("reloadGrid");
                // the function will be used as "errorfunc" parameter of editRow function
                // (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#editrow)
                // and saveRow function
                // (see http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing#saverow)
                /*alert("in onError used only for remote editing:" +
                    "\nresponseText=" + jqXHR.responseText +
                    "\nstatus=" + jqXHR.status +
                    "\nstatusText" + jqXHR.statusText +
                    "\n\nWe don't need return anything");*/
            },

1 个答案:

答案 0 :(得分:0)

此行为对应onSuccess的代码,因为您从中返回false。如果您检测到服务器响应包含错误,则可以从false(对应onSuccess内联编辑回调)返回successfunc。在onError(对应于内联编辑的errorfunc)内部,您应该只显示从服务器响应中解码的错误消息,如果您需要,则应显示 make reloadGrid允许用户在当前编辑的网格中修复错误。

顺便说一句,在reloadGrid内部使用afterSave(对应于内联编辑的aftersavefunc回调)而不是onSuccess内部更合乎逻辑。此外,我建议您将.trigger("reloadGrid")放在setTimeout内。它允许jqGrid完成内联编辑的标准处理,并在之后生成reloadGrid