1

I have a jqGrid that allows inline editing/adding of data. When a user double clicks on the row, inline edit starts. When the user presses enter or clicks on another row, I want the changes to be saved. For some reason, however, the row is being restored before the saveRow function is being called and my edits are not saved (the AJAX call never happens).

My code is shown below. When I press the enter key, my changes are saved and sent to the server. When I click on another row, my changes are not sent to the server (no AJAX call) and I see in the console that afterrestorefunc is called before the saveEdit.

var editParams = {
    afterSubmit: processResponse, 
    successfunc: function(response) {
        var processed = processResponse(response);
        if(processed[0] !== true) {
            $.jgrid.info_dialog($.jgrid.errors.errcap, processed[1], $.jgrid.edit.bClose);
        }
        return processed[0];
    }, 
    afterrestorefunc: function(id) {
        console.log('afterrestorefunc - ' + id);
    }, 
    bottominfo: 'Fields marked with an (*) are required', 
    keys: true
}, 
    zipEditParams = $.extend(true, {}, editParams, {url: 'editdata.php'});
/* Set global default options */
$.extend($.jgrid.defaults, {
    grouping: true, 
    shrinkToFit: false, 
    rowList: [10, 20, 30, 40, 50, 100], 
    rowNum: 20, 
    repeatitems: false, 
    ondblClickRow: inlineEdit, 
    onSelectRow: saveEdit
});
$('#zipcodes')
    .jqGrid({
        datatype: 'json', 
        colNames: ['Zip', 'City', 'State', 'Country'], 
        colModel: [
            {name: 'zip_zip', jsonmap: 'zip_zip', width: 75, editable: true, editrules: {required: true}, formoptions: {elmprefix: '(*) '}}, 
            {name: 'zip_city', jsonmap: 'zip_city', width: 75, editable: true, editrules: {required: true}, formoptions: {elmprefix: '(*) '}}, 
            {name: 'zip_state', jsonmap: 'zip_state', width: 75, editable: true, editrules: {required: true}, formoptions: {elmprefix: '(*) '}}, 
            {name: 'zip_country', jsonmap: 'zip_country', width: 75, editable: true, editrules: {required: true}, formoptions: {elmprefix: '(*) '}}
        ], 
        height: 200, 
        ignoreCase: true, 
        jsonReader: { repeatitems: false, id: 'zip_zip' }, 
        pager: '#zipcodes_pager', 
        url: 'data.php', 
        editurl: 'editdata.php'
    })
    .jqGrid('navGrid', '#zipcodes_pager', {add: false, edit: false, del: false, refresh: false, search: false})
    .jqGrid('inlineNav', '#zipcodes_pager', {edit: true, save: true, cancel: false, editParams: zipEditParams});

var lastSel;
/* Start editing the row */
function inlineEdit(id, iRow, iCol) {
    var editParams = this.id == 'zipcodes' ? zipEditParams : (this.id == 'labels' ? labelEditParams : (this.id == 'users' ? userEditParams : {}));
    $(this).jqGrid('editRow', id, editParams);
    focusRow(id, iCol, this);
}

function focusRow(id, iCol, table) {
    var ele = document.getElementById(id + '_' + table.p.colModel[iCol].name), 
        length = ele.value.length;
    ele.focus();
    if(ele.setSelectionRange) { //IE
        ele.setSelectionRange(length, length);
    }
    else if(ele.createTextRange) {
        var range = ele.createTextRange();
        range.collapse(true);
        range.moveEnd('character', length);
        range.moveStart('character', start);
        range.select();
    }
}

function saveEdit(id) {
    if(id && id != lastSel) {
        console.log('saveRow: ' + this.id + ' - ' + lastSel);
        /* Save the last selected row before changing it */
        var saveParams = zipEditParams;
        $(this).jqGrid('saveRow', lastSel, saveParams);
        lastSel = id;
    }
}

function processResponse(response) {
    console.log('processResponse');
    var obj = $.parseJSON(response.responseText), 
        retVal = true, 
        message = '', 
        new_id = '';
    if(obj.message) {
        if(obj.message == 'not logged in') {
            location.href = 'logout.php';
        }
        else if(obj.message != 'true') {
            message = obj.message;
            retVal = false;
        }
    }
    return [retVal, message, new_id];
}

Any help with this problem is greatly appreciated.

UPDATE It seems that if I comment out .jqGrid('inlineNav', '#zipcodes_pager', {edit: true, save: true, cancel: false, editParams: zipEditParams});, then everything works as expected. I also tried just using .jqGrid('inlineNav', '#zipcodes_pager');, but saw the same results. This is nice and could work in the short term, but I still would like the functionality of inline adding if possible.

Kyle
  • 4,421
  • 22
  • 32

1 Answers1

1

i think you need to add more functionality to your onSelectRow function

onSelectRow: function (id) {
        if (id && id !== lastsel) {
        // cancel editing of the previous selected row if it was in editing state.
        // jqGrid hold intern savedRow array inside of jqGrid object,
        // so it is safe to call restoreRow method with any id parameter
        // if jqGrid not in editing state
        if (typeof lastsel !== "undefined") {
        jQuery("#grid").jqGrid('restoreRow', lastsel);
        }
    lastSel = id;
    }
    }, 

in this code i'm not doing editing on selecting the row, but you can see that you need to restore the previous selected row, if that row was in edit mode and you click on some other row.

Try this.

Piyush Sardana
  • 1,748
  • 4
  • 19
  • 33
  • I don't want to restore the last selected row, I want to save the last selected row. I call `jQuery('#grid').jqGrid('saveRow', lastSel, saveParams)` to do this. Like I said in my question, the the aftersavefunc seems to be called before the saveRow func. To me, this suggests that the row is being restored automatically for some reason. Any thoughts? – Kyle Aug 01 '12 at 12:38
  • I updated my question with something I just found out. Perhaps this may help. – Kyle Aug 01 '12 at 12:44
  • buddy, this saveRow method internally calls the editRow, if there's is no separate url is specified for it. the link which i'm giving you will save the row if you are editing and press enter of click on save, meanwhile if your one row is already in edit mode and you select the other row, so it is so obvious the your previous row should no be in edit mode, it should be restored back two normal state, else there will be two rows in edit mode, check this link http://www.trirand.com/jqgridwiki/doku.php?id=wiki:inline_editing this will save your row which is in inline editing mode. – Piyush Sardana Aug 01 '12 at 13:06
  • in this link you will see onSelectRow method,just in the begining of the page, try to implement that. – Piyush Sardana Aug 01 '12 at 13:07
  • In the link you provided, and one that I have read many times, it does show the use of `restoreRow` in the onSelectRow function. Just below that example, it says `If you want to save instead of restore the editing you can call saveRow in place of restoreRow.` In my saveEdit function, I call saveRow. About no url being supplied, there is a url defined inside of the editZipParams object that gets passed to saveRow. I apologize if it seems like I am not understanding something you have said. – Kyle Aug 01 '12 at 13:18
  • Your code will enter in if block if, you click on some other row, leaving you current row where you were editing... but there's one line missing in your saveEdit function jQuery('#grid_id').editRow(id, true); after the if block...did u try that? – Piyush Sardana Aug 01 '12 at 13:29
  • plain english..your code will hit if block if you leave your current row where u were editing and select some other row. so if your code it not hitting if...how your row will be saved? so, if you put editRow line after if block and specify ur url there, then it should work..if it doesn't, i'm sorry i cant help you further. – Piyush Sardana Aug 01 '12 at 13:32
  • according to me, what your code is doing right now is when you click on some other row, leaving your currently editing row, it will not get restored cause you are calling saveRow method there which is exactly that links says. – Piyush Sardana Aug 01 '12 at 13:37
  • Yes, thank you. After you said that, I suddenly realized that in my `inlineEdit` function, I never saved the row before starting to edit the new row. I'm sorry that it took me so long to realize what you were trying to tell me. – Kyle Aug 01 '12 at 13:38
  • Yes, you are receiving both. I was just checking that everything worked now. Sorry for the frustration I may have caused you. In the end, calling saveRow inside of my inlineEdit function before I began editing the line fixed the problem. Thank you again. – Kyle Aug 01 '12 at 13:56