如果在编辑时出现错误,防止恢复行 [英] Prevent restoring row if error was rised while editing

查看:13
本文介绍了如果在编辑时出现错误,防止恢复行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

onSelectRow: function(id){
  if(id && id!==lastSel){
    jQuery(this).restoreRow(lastSel);
    lastSel=id;
  }
  jQuery(this).editRow(id,true,null,
                       function(response, postdata){
                           var data = eval('(' + response.responseText + ')');
                           data.result.success ? alert('success') : alert('error')
                       });
}

在这种情况下,我可以处理错误,但在此行数据恢复之后.问题是如果 data.result.success == false 时如何防止恢复行?

In this case I can handle errors, but after this row data restored. The question is how to prevent restoring row if data.result.success == false ?

如果我通过模态框进行编辑,那么一切正常.但在内联模式下不会.

If I edit through modal box, then all is ok. But in inline mode doesn't.

推荐答案

editRow 函数有以下参数:

jQuery("#grid_id").jqGrid('editRow',rowid, keys, oneditfunc, succesfunc, url,
                          extraparam, aftersavefunc,errorfunc, afterrestorefunc);

您当前的代码仅使用 succesfunc.重要的是,服务器返回一些 HTTP 状态代码 大于或等于 400.然后服务器响应将被 jQuery.ajax 和 jqGrid.要在出现错误时显示任何错误消息或任何其他操作,您应该使用 editRow 函数.

You current code use succesfunc only. It is important, that the server return some HTTP status code which are grater or equal to 400. Then the server response will be interpret as error by jQuery.ajax and jqGrid. To display any error message or for any other action in case of error you should use errorfunc parameter of the editRow function.

还有一点小话.您应该使用 jQuery.parseJSONJSON.parse 而不是eval 的用法.

One more small remark. You should use jQuery.parseJSON or JSON.parse instead of the usage of eval.

已更新:我在此回答您在评论中提出的问题.为什么使用 errorfunc 而不是总是使用 succesfunc 很重要?有不同的原因.如果你在标有糖标签的盒子里装满盐,它可能会在你的厨房里产生苦涩的后果.editRow的不同回调函数的错误使用情况完全一样.我可以举几个例子:

UPDATED: I answer here on your questions from the comment. Why it is important to use errorfunc and not always succesfunc? There are different reason. If you fill the box having the label sugar with the salt, it could has bitter consequences in your kitchen. Exactly the same is in case of wrong usage different callback functions of the editRow. I can give just some examples:

  • 您的服务器部分不仅会显式地产生错误.您的 Web 服务器可以从您的任何其他代码(如 SQL 查询)中引发异常.即使您捕获了所有此类错误,发送到服务器的格式错误或其他错误的输入数据也会产生带有失败 HTTP 状态和错误描述的响应.此类错误响应将生成您使用的框架(ASP.NET、PHP 等)的 Web 服务器.
  • jqGrid使用的jQuery应该知道哪个服务器响应成功,哪个是错误响应.经常错误响应有另一种格式,所以那里不会被jQuery.ajax解析(jQuery不会将它从JSON字符串转换成对象).
  • jqGrid 知道响应是否成功很重要.如果 jqGrid 在响应后发生错误,则做一件事,如果响应成功,则做另一件事.将调用不同的事件,将显示不同的消息等等.
  • Errors can be produced by your server part not only explicitly. Your web server can throw an exception from any other your code like SQL Queries. Even if you catch all such errors, the input data sent to the server having wrong format or having other error can produce the response with the failed HTTP status and the error description. Such error response will produce your web server of the framework which you use (ASP.NET, PHP and so on).
  • jQuery used by jqGrid should know which server response is successful and which is the error response. Frequently error response has another format, so there will not be parsed by jQuery.ajax (jQuery will not convert it from JSON string to the object).
  • It is important for jqGrid to know whether the response was successful or not. In case of error jqGrid do one things after the response, in case of successful response another things. Different events will be called, different messages will be shown and so on.

我写的关于处理错误的内容是一般规则.jqGrid 定义了许多事件以防出错.例如 loadError 用于网格填充,errorTextFormat 用于所有类型的表单编辑,errorCell 用于单元格编辑和 errorfunc 用于内联编辑.所有方法都是基于这样一个事实,即如果发生错误,服务器响应具有与错误相对应的 HTTP 状态代码(大于或等于 400).

What I wrote about the working with errors is the general rule. jqGrid defines many events in case of errors. For example loadError for the grid filling, errorTextFormat for all types of form editing, errorCell for the cell editing and errorfunc for inline editing. All the methods are based on the fact that in case of error the server response has the HTTP status code which corresponds the error (are grater or equal to 400).

这篇关于如果在编辑时出现错误,防止恢复行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆