Editor Create - put UUID in table

Editor Create - put UUID in table

rottenrotten Posts: 14Questions: 4Answers: 1

I have a table which originates from a database. The primary key in the table is a UUID that I do not display to the users.

I was able to use idSrc on the dataTable.Editor() function to map the hidden column so that I can do updates ('edit' actions). This part works fine.

I can create a new UUID when a new row is created, easily enough, so my insert works ('create' action).

However, I can't figure out how to get the new primary key back into the new row in the table on the browser without a location.reload().

ajax:  function ( method, url, newRowData, successCallback, errorCallback ) {
    ...
    if (newRowData.action == 'create') { location.reload(); };```
}

This creates a less-than-optimal user experience.

Without the PK, subsequent edits to the new row don't work.

I tried setting newRowData.data.my_id to my new UUID, but that never gets passed back up. The row in "table" never gets the value. This would be my preferred solution since the visible columns all get correctly updated from the user perspective and everything looks fine.

I also tried to do a "table.ajax.reload()", which would be at least slightly better than a full page reload. Unfortunately, the editor function gets defined before "table" exists, so it can't make that call.

This question has an accepted answers - jump to answer

Answers

  • rottenrotten Posts: 14Questions: 4Answers: 1

    I've noticed when I do an update I'm also running into this issue - if I update the same row twice without doing a full page reload - The second time I edit the row, it loses the primary key.

  • allanallan Posts: 61,840Questions: 1Answers: 10,134 Site admin

    I can't figure out how to get the new primary key back into the new row in the table on the browser

    The uuid should be included in the row object of data that is returned to the client-side on row creation.

    The client / server communication protocol that Editor uses is documented int he manual and specifically take a look at the create example - in that case the DT_RowId parameter is being used. If your parameter is called uuid you would use that as the parameter name (i.e. match what you have used for the idSrc option).

    Allan

  • rottenrotten Posts: 14Questions: 4Answers: 1

    I'm running DataTables entirely in the client. The server side is a REST API that either returns data for the table, or accepts a POST to update the table back in the database.

    I generate a new UUID with Javascript in the client before doing the POST that inserts a new row in the database in the $.fn.dataTable.Editor( {ajax: function() ) definition.

    Or, I grab the UUID from the idSrc mapping before I do a POST that updates the existing row in the database.

    In both cases, the table in my browser automatically refreshes as soon as Editor completes. However after that magic refresh, the column identified in idSrc gets set to 'undefined'. This makes subsequent updates invalid (there is no UUID to grab any more).

    I'm not sure how I would access the "row" object and then inject values from it into hidden columns for the existing table, during whatever is updating my table after the Editor function completes.

    Maybe it isn't possible and I'll have to stick with reloading the entire page every time an Edit function runs.

  • allanallan Posts: 61,840Questions: 1Answers: 10,134 Site admin
    Answer ✓

    Is the uuid generated by the server-side, and sent back from the REST API? I presume you are calling the REST API via Aja, and are using ajax as a function to make your own Ajax call?

    Either way, Editor still requires its basic protocol to be fulfilled - if the server doesn't tell it the uuid in the same form that DataTables was told during the data load, it won't have that uuid to perform the next edit.

    Allan

  • rottenrotten Posts: 14Questions: 4Answers: 1

    Now it makes sense.

    I'm using a custom ajax function to align with the api.

    I needed to set up the successCallback function to build the new row object as described in that documentation. When I do that, it seems to work.

    Thanks!

This discussion has been closed.