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
 Titania            
            
                Posts: 6Questions: 3Answers: 0
Titania            
            
                Posts: 6Questions: 3Answers: 0            
            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
You would need to either use
ajaxas a function so you can specify your own$.ajaxcall and then manipulate the data you get back from the server, or usepostSubmitto manipulate the data returned into the format required.Regards,
Allan
Thank you, I managed to do it with postSubmit as you suggested:
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.
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
I am manipulating the data in postSubmit function.
I see that server needs to reply like this:
I transformed then my response so that "data" is of array type.
Now (after manipulation in postSubmit), my response looks like this:
EDIT:
My apologies, I wrote postSubmit the wrong way. This is OK and now everything works:
Thank you for your time!