如果编辑时出现错误,则防止恢复行

发布于 2024-10-11 07:25:26 字数 558 浏览 0 评论 0原文

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 ,如何防止恢复行?

如果我通过模态框进行编辑,那么一切都可以。但在内联模式下则不然。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小ぇ时光︴ 2024-10-18 07:25:26

editRow 函数具有以下参数:

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

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

还有一点小意见。您应该使用 jQuery.parseJSONJSON.parse 而不是使用eval

更新:我在这里回答您在评论中提出的问题。为什么使用 errorfunc 很重要,而不总是使用 succesfunc?有不同的原因。如果你把盐装进贴有糖标签的盒子里,可能会给你的厨房带来痛苦的后果。完全相同的是,如果错误使用 editRow 的不同回调函数。我可以举一些例子:

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

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

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

入画浅相思 2024-10-18 07:25:26

我想修复相同的场景,我可以这样做:

$.extend($.jgrid.inlineEdit, { restoreAfterError: false });

在网格模型中:

ajaxRowOptions: {
    complete: function(res, stat) {
        if (res.statusText=='OK') {
            return [true, res.responseText ];
        } else {
            return [false, res.responseText ];
        }
    }
}

I wanted to fix the same scenario and I could by doing:

$.extend($.jgrid.inlineEdit, { restoreAfterError: false });

And in grid model:

ajaxRowOptions: {
    complete: function(res, stat) {
        if (res.statusText=='OK') {
            return [true, res.responseText ];
        } else {
            return [false, res.responseText ];
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文