Search code examples
infragisticsignite-uiiggrid

Update newly created record on client after successfully processing the Create request on server


Please, consider following scenario:

  • IgniteUI 16.1 igGrid powered with igGridUpdating feature and RESTDataSource
  • User creates a new record through modal dialog
  • Post request is initiated with form data
  • Server processes the create request and returns an object, populated with correct ID
  • In success handler on the client side, the newly added in the grid row has to be found and updated with correct ID returned from the server.
  • The ID column serves as a grid's primary key and it's hidden

What happens when a new row is adding?

We are watching infragistics.lob-16.1.js

In _dialogOpening(), row 68167, _originalValues are computed via $.extend(this._originalValues, values, this._originalValues), where values = _getDefaultValues() or with other words values.id = this._pkVal. And _pkVal is a counter that is incremented each time when a new row appears.

Keeping that in mind, later, _endEditDialog() is called, where newValues, representing the entered data by the user, are merged with default values of the input form: newValues = this._getNewValuesForRow(colElements) followed by newValues = $.extend({}, prevValues, newValues) and prevValues are the same _originalValues from above.

Then an _addRow() is called, which calls on its run grid.dataSource.addRow() and a transaction is created.

My point here is the updating feature generates ID automatically for the new row and ID = CurrentRowsCount + 1.

So, if the grid contains 8 records, then newly created record will automatically be assigned with ID = 9. And imagine, if one of existing records has an ID = 9, then igGridUpdating's updateRow(rowId, values) will update both rows, existing and the new one. And I realy want to call this method in order to update the row with the data, returned from the server.

How could I intervene in the whole picture and accomplish the update of the new row?


Solution

  • The auto-generated primary keys are only meant to cover the most basic scenarios. If your app supports row deletion you should change them with something that will keep them unique using the generatePrimaryKeyValue event.

    Using updateRow after receiving the permanent keys from the server is the way to go, however, remember to pop the transaction from the allTransactions array so the update doesn't go to the server on the next saveChanges call.