AJ.
AJ.

Reputation: 16719

Can I change the ID of a newly added row in jqGrid?

I'm using jqGrid with inlineNav so that users can edit/add/delete rows locally and then submit all changes to the server when they are finished. I'd like to be able to add multiple new rows to the grid locally, but due to other requirements, I need the newly added rows to have unique IDs rather than the default new_row. Also, I can't use an ajax call to persist new rows immediately on add due to foreign key constraints. I've attempted the following, but the ID value doesn't change:

<input type="hidden" id="newRowIndex" />

$("#thisGrid").jqGrid('inlineNav', '#thisGridPager', {
    edit: false,
    addtext: "Add",
    save: false,
    cancel: false,
    addParams: {
        position: 'last',
        addRowParams: {
            keys: true,
            oneditfunc: function (rowid) {
                var newRowIndex = $("#newRowIndex").val();
                if (!newRowIndex)
                    newRowIndex = 1;
                $("#thisGrid").jqGrid('setCell', rowid, 'id', rowid + "_" + newRowIndex, '', '');
                newRowIndex++;
                $("#newRowIndex").val(newRowIndex);
            }
        }
    }
});

I would simply like to set the newly added row's ID to new_row_1, incrementing the index for each newly added row. Is this possible, and if so, how?

Solution

In addition to Dean's answer, I discovered that it doesn't work doing it in the oneditfunc of addRowParams. I found that using jqGrid's afterInsertRow event works:

afterInsertRow: function (rowid, rowdata, rowelem) {
    if (rowid == 'new_row') {
        var newRowIndex = $("#newRowIndex").val();
        if(!newRowIndex)
            newRowIndex = 1;
        var newRowId = rowid + "_" + newRowIndex;
        $("#new_row").attr('id', newRowId);
        newRowIndex++;
        $("#newRowIndex").val(newRowIndex);
    }
}

Upvotes: 3

Views: 5263

Answers (1)

Dean
Dean

Reputation: 1550

To set the new row's id use:

 $("#new_row").attr('id',newId);

Referencing this question: Can I change the primary ID of a row in JQGrid without reloading?

Upvotes: 3

Related Questions