2012-02-27 24 views
0

我添加了id爲「new」的新行。在我將該行保存爲指定爲id的列(稱爲「代碼」)後,將保留id爲「new」,除非我重新加載網格或刪除行&再次使用新的ID添加它。如何更改rowid

是否有另一個功能來保存該行後更改rowid?

謝謝。

回答

1

您可以使用jQuery.attr設置任何屬性,包括id。您應該只更改id屬性時非常小心。例如,如果您使用loadonce: true選項,或者如果您使用datatype: 'local',則內部存在data_index參數,它們將當前ID緩存到行數據映射。所以在這種情況下,您需要更新jqGrid的_index參數。

如果您在執行過程中遇到問題,您應該發佈您當前使用的代碼。 jqGrid的某些選項(如datatypeloadonce)非常重要。此外重要的是要知道哪些editing mode和哪些你使用。

1

更改ID需要幾個步驟,因爲jqGrid的不改變電網的主鍵,所以我們必須手動完成所有步驟:

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; 
} 
0

的是jqGrid的changeRowId功能:

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是必需的,因爲jqgrid在調用aftersavefunc後恢復舊的行ID。