How to use Restful API when the datatable expects more data than was sent in with the create?

How to use Restful API when the datatable expects more data than was sent in with the create?

thsiao@imax.comthsiao@imax.com Posts: 3Questions: 1Answers: 0

The restful api example works great, however, I have two differences in with my returned data compared to what i'm POSTing to the server.

1) I'm posting a dropdown language.id value to the server but my datatable needs to display the name

2) I am not posting a GUID, but am receiving a GUID back from the server in the POST results (my datatable wants to display this value)

I am getting this error:
DataTables warning: table id=track_table_19 - Requested unknown parameter 'language.name' for row 11. For more information about this error, please see http://datatables.net/tn/4

Basically I want the datatable to be updated with the results of the restful POST rather than using the values that I fill out in the editor form. I did serverSide:true and my POST worked perfectly. However, then the search, sort, pagination all stopped working as datatables was expecting the server to handle it (which I don't want to do).

Thanks.

This question has an accepted answers - jump to answer

Answers

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

    Hi,

    When Editor receives the data returned from the server on edit, it expects the data to be returned in a particular format, specifically a JSON object with a row parameter that defines the data for the DataTable row (manual). Is your server responding with that JSON data, or can you modify it to do so?

    Let's consider the Join example to illustrate why this is required. The Editor form will submit a value for users.site, but DataTables needs to display sites.name (basically the same as what you want, but with different parameter names).

    So Editor might submit:

    action=edit
    data[users][first_name]=Alexa
    data[users][last_name]=Wilder
    data[users][phone]=1-727-307-1997
    data[users][site]=2
    id=row_32
    

    And the server will reply with the full data object for that row:

    {
      "row": {
        "DT_RowId": "row_32",
        "users": {
          "first_name": "Alexa",
          "last_name": "Wilder",
          "phone": "1-727-307-1997",
          "site": "2"
        },
        "sites": {
          "name": "London"
        }
      }
    }
    

    If sites.name wasn't returned, then DataTables wouldn't have any information to be able to display anything about the site name.

    Are you able to have your REST API return data in the same format that DataTables is using for your rows?

    Thanks,
    Allan

  • thsiao@imax.comthsiao@imax.com Posts: 3Questions: 1Answers: 0

    Hi Allan,

    My API does return JSON, but it doesn't wrap the return with "row". Is that a requirement from the server or can I use some javascript to wrap the results before the datatable gets it?

    For example I was playing around with doing some like this on my editor form:

    .on( 'postSubmit', function ( e, json, data ) {
            var wrapped_data = {row: json}
            data = wrapped_data;
        } );
    

    The json variable has the data from the server, but doesn't have the "row" wrapper.

    Thanks again.

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

    Hi,

    You are absolutely correct that you can use postSubmit to manipulate the data returned by the server before Editor then uses it. What we need to be careful with though is the assignment of variables. json is an object that is passed in and it a pointer (or reference) to the object. So we can't just assign the value we want to the json variable, but rather manipulate the object it points to.

    That is all a slightly complicated way of saying, try this:

    .on( 'postSubmit', function ( e, json, data ) {
            json.row = {row: json};
    } );
    

    This works in Javascript as the assignment isn't made until the right hand side has been evaluated. The resulting object will end up with all the original items still present, but Editor will just ignore them (as long as they aren't called error or fieldErrors!).

    Regards,
    Allan

  • thsiao@imax.comthsiao@imax.com Posts: 3Questions: 1Answers: 0

    Thanks Allan,

    That worked:

    }).on( 'postSubmit', function ( e, json, data ) {
            json.row = json;
        } );
    
  • allanallan Posts: 61,840Questions: 1Answers: 10,134 Site admin

    Oops - yes, thanks for spotting that error. Your code is absolutely spot on.

    Great to hear that helps!

    Regards,
    Allan

  • nb10nb10 Posts: 2Questions: 0Answers: 0

    I tried

           editor.on('postSubmit', function( e, json, data, action ) {
               json.row = json;
           })
    

    for editor version 1.4, too, the editing window is closed (as before, so JSON format is recognized), but the table is not updated. And if I try to debug that with a break point in browser, it don't show that this is used. What can be wrong here?
    What has to be used for version 1.5, if the server responses with multiple records (JSON is recognized), but without the expected "data: "? json.data=json seems to be not correct.

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

    data is the property that Editor 1.5 looks for (rather than row) - docs, so I'm a little surprised that doesn't work. Although you need to make sure that it is an array, since 1.5 expects an array of data objects - is that the case?

    Allan

  • nb10nb10 Posts: 2Questions: 0Answers: 0

    I found the problem, my update object and my table have a different structure and I returned the structure from the edit window, not the structure from the table row. I changed that and now it is working. Thanksl!

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

    Good to hear you got it working!

    Allan

This discussion has been closed.