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

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

问题描述

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?

如何防止恢复行?

如果我通过模式框进行编辑,则一切正常.但是在串联模式下则不会.

解决方案

您当前的代码仅使用succesfunc.重要的是,服务器返回一些 HTTP状态代码大于或等于400.然后 jQuery.ajax 和jqGrid.要显示任何错误消息或发生错误时采取任何其他措施,您应该使用 jQuery.parseJSON JSON.parse,而不要使用eval. /p>

已更新:我在这里从评论中回答您的问题.为什么使用errorfunc而不是始终使用succesfunc很重要?有不同的原因.如果您在带有糖标签的盒子中装满盐,则可能对您的厨房造成痛苦的后果.如果用法错误,完全相同,editRow的不同回调函数.我可以举一些例子:

  • 您的服务器部件不仅会显式地产生错误.您的Web服务器可以从您的任何其他代码(例如SQL查询)中引发异常.即使捕获所有此类错误,发送到服务器的格式错误或其他错误的输入数据也会产生带有失败的HTTP状态和错误描述的响应.这种错误响应将生成您所使用的框架(ASP.NET,PHP等)的Web服务器.
  • jqGrid使用的
  • jQuery应该知道哪个服务器响应成功,哪个是错误响应.错误响应通常具有另一种格式,因此jQuery.ajax不会解析 (jQuery不会将其从JSON字符串转换为对象).
  • 让jqGrid知道响应是否成功很重要.如果发生错误,jqGrid会在响应后做一件事,如果响应成功,则要做另一件事.将调用不同的事件,将显示不同的消息,依此类推.

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

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')
                       });
}

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 function has the following parameters:

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

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.

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

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:

  • 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.

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天全站免登陆