Convert bare JSON response from server to format with 'data' attribute that editor expects

Convert bare JSON response from server to format with 'data' attribute that editor expects

TitaniaTitania Posts: 6Questions: 3Answers: 0
edited May 2016 in Free community support

I have REST service. My method parameters are JSON objects and return value is also JSON.
For example: {'id': 5, 'name':'name'}

First problem is that data is sent to server in this format by default:

action              = edit
data[0][id]       = 5
data[0][name] = name

but I managed to convert it using 'data' function to JSON {'id': 5, 'name':'name'}.

Another problem that I didn't manage to solve is that editor expects response from server in this format:

{
    "data": [
        {
            "DT_RowId":   "row_id",
            "id": 5,
            "name":  "name"
        }
    ]
}

and my method is returning just:
{'id': 5, 'name':'name'}
So I don't have that 'data' attribute in JSON and I get an error.

Can someone please help me solve this problem? Thanks!

This is my editor:

editor = new $.fn.dataTable.Editor({
            ajax: {
                create: .....,
                edit: {
                    type: 'PUT',
                    url: 'rest/categories/_id_',
                    contentType: 'application/json',
                    data: function ( d ) {
                        var obj;
                        for (var key in d.data) {
                            obj = d.data[key];
                            break;
                        }
                        return JSON.stringify( obj );
                    }
                },
                ....
            },
            table : "#datatable",
            fields: [
                {
                    label: "id",
                    name: "id"
                },
                {
                    label: "name",
                    name: "name"
                }
            ]
        });

Edited by Allan - Syntax highlighting. Details on how to highlight code using markdown can be found in this guide.

This question has accepted answers - jump to:

Answers

  • allanallan Posts: 63,791Questions: 1Answers: 10,513 Site admin
    Answer ✓

    You would need to either use ajax as a function so you can specify your own $.ajax call and then manipulate the data you get back from the server, or use postSubmit to manipulate the data returned into the format required.

    Regards,
    Allan

  • TitaniaTitania Posts: 6Questions: 3Answers: 0

    Thank you, I managed to do it with postSubmit as you suggested:

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

    So my response now looks like this:
    Object { id: 1, name: "New category", data: Object, error: "", fieldErrors: Array[0] }

    This works, but after edit(or create) row in the table doesn't update. Shouldn't this be automatically done by editor? Do you maybe know what am I missing?
    Also, I added idSrc : "id" property to editor.

  • allanallan Posts: 63,791Questions: 1Answers: 10,513 Site admin
    Answer ✓

    Yes, it should be done by Editor automatically. Are you returning JSON in the required format or otherwise transposing it to be in the required format?

    Allan

  • TitaniaTitania Posts: 6Questions: 3Answers: 0
    edited May 2016

    I am manipulating the data in postSubmit function.
    I see that server needs to reply like this:

    {
        "data": [
            {
                ....
            }
        ]
    }
    

    I transformed then my response so that "data" is of array type.
    Now (after manipulation in postSubmit), my response looks like this:

    {     
          id: 4, 
          name: "name", 
          data: 
             [{
                id: 4,
                name: "name"
              }]
    }
    

    EDIT:
    My apologies, I wrote postSubmit the wrong way. This is OK and now everything works:

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

    Thank you for your time!

This discussion has been closed.