Jose Prendes
Jose Prendes

Reputation: 1

how to change rowid

I add new rows with id "new". After I save the row with a new value in the column designated as id (say "code") it remains with id "new" unless I reload the grid or delete the row & add it again with the new id.

Is there another function to change the rowid after the row is saved ?

Thanks.

Upvotes: 0

Views: 4139

Answers (3)

Andrus
Andrus

Reputation: 27955

The is changeRowId function in jqgrid:

function aftersavefunc(rowId, response) {

var json = $.parseJSON(response.responseText);
var $tr = $("#" + rowId);

    setTimeout(function () {
        $grid.jqGrid("changeRowid", rowId, json.Id);
        $grid.jqGrid('setSelection', json.Id);
        setFocusToGrid();
    }, 1000);
}

setTimeout is required since jqgrid restores old row id after aftersavefunc is called.

Upvotes: -1

Mateus Padua
Mateus Padua

Reputation: 193

To change the id is required a few steps, since jqGrid does not change the primary key of the grid, so we have to manually do all the steps:

var new_id = 39; //for example
aftersavefunc: function( old_id ) {

   //get data param
   var row = grid.jqGrid('getLocalRow', old_id);
   console.log(row); //use for firefox test
   row._id_ = new_id;

   grid.jqGrid('setRowData',old_id,{my_id:new_id});
   $("#"+response).attr("id", new_id); //change TR element in DOM

   //very important to change the _index, some functions using the                  
   var _index = grid.jqGrid('getGridParam', '_index');
   var valueTemp = _index[old_id];
   delete _index[old_id];
   _index[new_id] = valueTemp;
}

Upvotes: 1

Oleg
Oleg

Reputation: 222017

You can use jQuery.attr to set any attribute inclusive id. You should be only very careful with changing of id attribute. For example if you use loadonce: true option or if you use datatype: 'local' there are exist internally data and _index parameters which cache the current id to row data mapping. So in the case you will be need to update _index parameters of jqGrid.

If you will have problems with the implementation you should post the code which you currently use. Some options of jqGrid (like datatype, loadonce) are very important to know. Additionally is important to know which editing mode and in which for you use.

Upvotes: 2

Related Questions