Prevent restoring row if error was rised while editing

谁说胖子不能爱 提交于 2019-11-28 07:00:19

问题


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.


回答1:


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




回答2:


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 ];
        }
    }
}


来源:https://stackoverflow.com/questions/4668167/prevent-restoring-row-if-error-was-rised-while-editing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!