Editor Datatable sends Post data in original format

Editor Datatable sends Post data in original format

haxxorsidhaxxorsid Posts: 6Questions: 2Answers: 1
edited July 2017 in Free community support

This is code:

       editor = new $.fn.dataTable.Editor( {
            ajax: {
                create: {
                    type: 'POST',
                    url:  '/api/table',
                    headers: {
                        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                    },
                    data: {
                        column1: $("input[name=column1]").val(),
                        column2: $("input[name=column2]").val(),
                        column3: $("input[name=column3]").val(),
                        column4: $("input[name=column4]").val(),
                        column5: $("input[name=column5]").val()
                    },
                    success: function () {
                        $.notify('New entry added.', 'success');
                    },
                    error: function (response) {
                        var json = $.parseJSON(response.responseText);
                        for (var key in json) {
                            $.notify(json[key]);
                        }
                    }
                },
               //similarly for edit & remove
          },
         //fields and stuff...
    });

It sends data like this:

action:edit
data[1][id]:1
data[1][column1]:Pinkie Quitzon
data[1][column2]:II
data[1][column3]:houston71@schowalter.com
data[1][column4]:01402037
data[1][column5]:dibbert.biz

But I want it to be like this:

column1:Pinkie Quitzon
column2:II
column3:houston71@schowalter.com
column4:01402037
column5:dibbert.biz

How to change it??

Answers

  • rduncecbrduncecb Posts: 125Questions: 2Answers: 28

    Create a function that binds to the preSubmit and modifies the data accordingly. The data is the second parameter passed to the preSubmit event handler, modify the object in place and it will be passed through to the ajax call.

  • allanallan Posts: 63,480Questions: 1Answers: 10,467 Site admin

    You can also modify data in ajax.data as well as preSubmit. I would use something like:

    $.each( data.data, function ( value, key ) {
      $.extend( data, data.data[ key ] );
    } );
    

    The loop is just a quick and easy way to get the key and values. It obviously won't work with multiple row editing though, which is why Editor uses the format it does by default.

    Allan

This discussion has been closed.