jqGrid AddRowData rowId has wrong value

fr4nk picture fr4nk · Jul 3, 2014 · Viewed 7.8k times · Source

if i add local data (one row) to an existing jqgrid, the autogenerated value by $.jgrid.randId() seems to be wrong. I need the new row id for further actions.

var mydata = [
    {id: '12', thingy: "abc"}, 
    {id: '34', thingy:"def"}, 
    {id: '56', thingy: 'ghi'}
];

$("#grid").jqGrid({
    data: mydata ,
    datatype: 'local',
    gridview: true,
    height: 'auto',
    autoencode: true,
    rowNum: 3,
    colModel: [{
        name: 'id',
        width: 60,
    },{
        name: 'thingy',
        width: 90,
    }],
    caption: "Stack Overflow Example",
    ondblClickRow: function(rowid) {
        console.log(rowid);  // print current row id
    }
});

$('#UpdateGridButton').click(function(){
    var p = $('#grid').getGridParam();
    console.log("found gridParamData:", p.data);
    if (p.data){
        var newData = [
            {id: '78', thingy: "jkl"}
        ];
        var rowId = $.jgrid.randId(); // new row ID
        $("#grid").jqGrid('addRowData', rowId, newData);
        console.log(rowId); // print new row id
    }    
});

See this fiddle & console output (2x click the row): http://jsfiddle.net/quK3s/

You see, if you add a new row, row id is "jqg1" but actually it inserts a row with "jqg2" (inspect with firebug, see console output if double clicking the row)

Any Ideas how to solve this issue? or is it a bug?

As i see in this question https://stackoverflow.com/a/9218310/2221820, i think the function is wrong and it should be pre-increment instead of $.jgrid.guid++.

Any thoughts?

UPDATE:

i ended up with using

var rowId = $.jgrid.uidPref + (++$.jgrid.guid);

but any better solution/idea would be appreciated.

Answer

fr4nk picture fr4nk · Sep 19, 2014

After reading the source code and checking against the docs, this is not a bug.

You have to add a single row as an object, then you can specify the rowId. If you add data as an array of objects, the "rowid should contain the name from data object which should act as id of the row" http://www.trirand.com/jqgridwiki/doku.php?id=wiki:methods (addRowData)

Updated the fiddle & it's working: http://jsfiddle.net/quK3s/6/

    ...
    var newData = {id: '78', thingy: "jkl"};
    ...