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

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.

Oleg
  • 220,925
  • 34
  • 403
  • 798
mihserf
  • 97
  • 2
  • 6

2 Answers2

1

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 ];
        }
    }
}
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Vishnu Atrai
  • 2,370
  • 22
  • 24
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).

Oleg
  • 220,925
  • 34
  • 403
  • 798
  • Thanks a lot! But why just not listening succesfunc? As it's done in the afterSubmit function: afterSubmit : function(response, postdata){var data = eval('(' + response.responseText + ')'); return [data.result.success,'message',data.result.id] } For example if returned false then restoring will not be run. – mihserf Jan 12 '11 at 13:24
  • I've found another solultion. In succesfunct always return true. jQuery(this).editRow(rowid,true,null, function(response, postdata){var data = jQuery.parseJSON(response.responseText); alert(data.result.success); return true; }); So restoration will not be triggered. – mihserf Jan 12 '11 at 13:34
  • Thanks. I understood your position about error handling. But in the RoR application there is usually global js-function for handling server errors and other function that accepts dom objects and object validation errors(as server response in JSON) for marking the fields (and any other parameters). So in this case we should separate validation errors and other common server errors (400, 500, etc.). – mihserf Jan 12 '11 at 17:08
  • @mihserf: I don't use RoR, but simple research shows, that before version 2.3 you could set HTTP status code with `response.headers["Status"]` and starting with 2.3 version using `status` and `status_message` helpers. See http://stackoverflow.com/questions/2238244/custom-error-pages-in-rails/2281215#2281215 as example. I use mostly Microsoft ASP.NET MVC or WFC to build RESTfull services and can set custom HTTP error code without any problem. In general you can produce **any** error in RoR and send more information in the body. Throw `WebFaultException` exception in .NET do this. – Oleg Jan 12 '11 at 17:49
  • @mihserf: Shortly: it dose not matter which technology you use on the server. If the server send results with HTTP protocol you **have to** send errors per HTTP errors. All web server technologies gives you possibilities to do this. – Oleg Jan 12 '11 at 17:51
  • Yes, seems that I should agree with you. In the case of validation errors I should send header with the status unprocessable_entity. Thanks for discussion! – mihserf Jan 12 '11 at 18:26
  • @mihserf: You welcome! I continued the discussion because at the begining of my work with jqGrid I spend much time till I really understand that I **must** create jqGrid so that **any kind of error** inclusive IP connection errors and so on will be seen and interpret as errors in jqGrid. So I just shared now my experience in the subject with you. – Oleg Jan 12 '11 at 19:08
  • @michele: If you need to show an error message from the server response you can use the `errorfunc` function. If you need display a message about successful processing on the server you can use `succesfunc` or `aftersavefunc`. – Oleg Jul 22 '11 at 15:09