Search code examples
jqgrid

Execute cancel add, edit and delete event manually in jqGrid


I use jqGrid 4.5.4

When I click int the add button from jqGrid "inlinenav", it creates a row in the grid.

But if I accidentally click on another grid row, the "added row" disappears, and the "inlinenav" edit and delete buttons stop working.

For the buttons to start working again I need to click the add button again and cancel the inclusion.

Is there a way for me to execute the cancel add, edit and delete event manually in the method "onSelectRow"?

Below my code:

jQuery("#" + p_gridName).jqGrid("GridUnload");        
jQuery("#" + p_gridName).jqGrid({
    data : p_dados, 
    datatype : "local", 
    sortable : true, 
    colNames : p_header, 
    colModel : p_descriptor,
    rowNum : 20, 
    rowList : [20, 50, 100], 
    pager : '#p' + p_gridName, 
    recordpos : 'right', 
    viewrecords : true,
    multisort : true,
    sortorder : "desc", 
    width: screen.availWidth - (screen.availWidth * 3 / 100),
    height : screen.availHeight - 300, 
    ignoreCase : true, 
    multiselect : false, 
    shrinkToFit : false,
    caption: "-",
    onSelectRow : function(ids) {        
        var aux = jQuery("#" + p_gridName).jqGrid('getRowData', ids);
        valorChave = aux[p_primaryKey];            
        var $this = $(this);
        $this.jqGrid('setGridParam', {
            editurl : (ids === "new_row" ? 'EdicaoInLineAction.do?method=salvarDadosGrid&contentName=' + p_contentName :  'EdicaoInLineAction.do?method=salvarDadosGrid&contentName=' + p_contentName + '&chaveRegistro=' + valorChave)
        });
    },
    editurl : 'EdicaoInLineAction.do?method=salvarDadosGrid&contentName=' + p_contentName
});
jQuery("#" + p_gridName).navGrid('#p' + p_gridName, {
    edit : false, 
    add : false, 
    del : function(valorChave){ return true }, 
    refresh : false, 
    search : false
    },{},{}, {            
    onclickSubmit : function (rp_ge, postdata) {
        var keyValue = $(this).jqGrid("getCell", postdata, p_primaryKey);
        return {chaveRegistro : keyValue};
    },
    afterSubmit : function (response, postdata) {
        if (response != null && response.responseText != null) {
            if (response.responseText.includes("OK")) {
                alert(response.responseText);
                return [true, "", ""];
            }
            else {
                alert(response.responseText);
                return [false, "", ""];
            }
        }
    }
});
jQuery("#" + p_gridName).jqGrid('inlineNav', '#p' + p_gridName, {
    edit : true, editicon : "ui-icon-pencil", //icone para edição
    add : true, addicon : "ui-icon-plus", //icone para inclusão
    save : true, saveicon : "ui-icon-disk", //icone para salvar
    savetitle : "Salvar", //hint para salvar
    cancel : true, cancelicon : "ui-icon-cancel", //icone para cancelar
    canceltitle : "Cancelar", //hitn para cancelar
    editParams : {
        keys : false, 
        oneditfunc : null, 
        successfunc : function (response) {
            if (response != null && response.responseText != null) {
                if (response.responseText.includes("OK")) {
                    alert(response.responseText);
                    return true;
                }
                else {
                    alert(response.responseText);
                    return true;
                }
            }
        }
    },
    addParams : {
        useDefValues : true, addRowParams : {
            successfunc : function (response) {
                if(response.responseText.includes("OK")) {
                    alert(response.responseText);
                    return true;
                }
                else {
                    alert(response.responseText);
                    return true;
                }
            }
        }
    }
});

Solution

  • I'm not sure if this option is available in jqGrid 4.5.4, but this behaviour can be disabled if yo set the option restoreAfterSelect to false. This option by default is true:

    jQuery("#" + p_gridName).jqGrid('inlineNav', '#p' + p_gridName, {
        ...
        restoreAfterSelect : false,
        ...
    });
    

    P.S. (edit) Just checked - this option is available in 4.5.4 and you can use it