Search code examples
jqgrid

Preserve record when InLineEditing JQGrid gives error when adding or changing


I am using InLineEditing JQGrid and when adding or changing a record, if it fails, the record disappears. I need to preserve these lines for the user to correct the wrong information and try to save the record again.

Below my snippet code

jQuery("#" + p_gridName).jqGrid('inlineNav', '#p' + p_gridName, {
    edit : true, editicon : "ui-icon-pencil", 
    add : true, addicon : "ui-icon-plus",
    save : true, saveicon : "ui-icon-disk", savetitle : "Salvar",
    cancel : true, cancelicon : "ui-icon-cancel", canceltitle : "Cancelar",
    editParams :  {
        keys : false, oneditfunc : null, successfunc : function (response) {                    
            if (response != null && response.responseText != null) {                                                
                if (response.responseText.includes("OK")) {
                    alert(response.responseText);
                    reloadDataGrid(p_gridName, p_contentName, p_filtroGrid);
                    atualizaInfoStatusNavegacao('edit', valorChave, p_primaryKey, p_contentName);
                    return true;
                } else {
                     alert(response.responseText);
                     return true;
                }
            }
        }, afterrestorefunc: function (rowid) {
                alert("after restore func");
                return rowid;
        }, restoreAfterError : false
    },
    addParams :  {
        useDefValues : true, addRowParams :  {
             successfunc : function (response) {
                 if (response.responseText.includes("OK")) {
                     alert(response.responseText);
                     reloadDataGrid(p_gridName, p_contentName, p_filtroGrid);
                     return true;
                 } else {
                     alert(response.responseText);
                     return true;
                 }
            }
        }
    }
});

Solution

  • There is a option called restoreAfterError (see docs here), which by default is true. Set it to false in order to get the desired behavior.

    This is a option (parameter) in editRow or saveRow.

    EDIT

    Your code should return true or better array with first value true/false and message like this

            if (response != null && response.responseText != null) {                                                
                if (response.responseText.includes("OK")) {
                    alert(response.responseText);
                    reloadDataGrid(p_gridName, p_contentName, p_filtroGrid);
                    atualizaInfoStatusNavegacao('edit', valorChave, p_primaryKey, p_contentName);
                    return [true,''];
                } else {
                     return [false, response.responseText];
                }
            } else {
                return [false, 'no response from the server'];
            }