Looking for Editor edit examples overriding ajax function

Looking for Editor edit examples overriding ajax function

morganrobmorganrob Posts: 18Questions: 7Answers: 2

Hi, I have existing web api's for updating that use a PUT that I have no access to modify to accept the Editor data format. The api is simple VS2015 web api 2 controller with actions using entity framework. Not using DataTables Editor .NET library.

I have a table: pickup primary key: pickupid

PUT http://localhost/api/pickups/2754244
{"pickupid":2754244,"run":"DAVE",...}

successfully updates my sqlserver table and returns

{"pickupid":2754244,"run":"DAVE",...}

Is there a template mechanism to reformat / manipulate the Editor's ajax call or will I have to do some json manipulation myself?

editor = new $.fn.dataTable.Editor({
            ajax: function ( method, url, d, success, error ) {
                var output = { data: [] };
                if (d.action === 'edit') {
                    $.ajax({
                        type: 'PUT',
                        url: 'api/pickups/_id_' , // ** _id_ isn't populated in ajax call**
                        data: d, //** data sent is not simple json **
                        dataType: "json",
                        success: function (json) {
                            success(json);
                        },
                        error: function (xhr, error, thrown) {
                            error(xhr, error, thrown);
                        }
                    });

                }
               
            },
            idSrc: 'pickupid',  
            table: "#workTable",
            fields: [{
                label: "Name:",
                name: "pd_name"
            }, {
                label: "Run:",
                name: "run"
            }, {
                label: "Notes:",
                name: "notes"
            }, {
                label: "Description:",
                name: "description"
            }
            ]
        });

Datatable code is fine and editor activates and makes a PUT call but clearly not with the dataformat my api demands

Thank you, Rob

This question has an accepted answers - jump to answer

Answers

  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin

    or will I have to do some json manipulation myself?

    This I'm afraid. Every system that doesn't conform to what Editor expects would need different manipulation which is why there is no generic example for it.

    In your code above, you would modify the json in between lines 10 and 11 before calling Editor's success callback.

    This page documents what Editor expects and I'm happy to answer any questions you have about it!

    Allan

  • morganrobmorganrob Posts: 18Questions: 7Answers: 2

    I suspected as much.

    So something along these lines, or is there a better/preferred technique?

    editor = new $.fn.dataTable.Editor({
                ajax: function (method, url, d, success, error) {
                    var output = { data: [] };
                    if (d.action === 'edit') {
                        $.each(d.data, function (key, value) {
                            value.pickupid = key; 
                            $.ajax({
                                type: 'PUT',
                                url: 'api/pickups/' + key, 
                                data: value, 
                                dataType: "json",
                                success: function (json) {
                                    output.data.push(json); 
                                    success(output);
                                },
                                error: function (xhr, error, thrown) {
                                    error(xhr, error, thrown);
                                }
                            });
                        });
    
                    }
    
                },
    
  • allanallan Posts: 63,350Questions: 1Answers: 10,443 Site admin
    Answer ✓

    Yes - I think that probably is the way to go about it.

    There is another option with 1.6 - you could use Editor without an ajax option which would result it it just editing the local table. Then use events such as preSubmit to trigger your own Ajax request to the server.

    The only issue with that would be if validation fails - there would need to be some mechanism to let Editor know about that, which there isn't currently (not without chaining events).

    Does that sound like it might be an option if I have a think about that?

    Allan

  • morganrobmorganrob Posts: 18Questions: 7Answers: 2

    I'm happy enough with the solution I have now, thanks

  • bvelasquezbvelasquez Posts: 28Questions: 7Answers: 0

    Great post!

This discussion has been closed.