Replicate the ajax request inside button action

Replicate the ajax request inside button action

souljacksouljack Posts: 7Questions: 2Answers: 0

Hello,

I have a serverSide implementation of a datatable and would like to build a custom excel export. In the datatable I've got an column search as well as an global search. There are also additional 3rd party search modifiers that are all passed into the ajax POST request and works seemleasly.

However, for my custom excel export, I would need to recreate this request and add additional needed params. Is ist possible to access/replicate the datatable ajax request with all its payload?

example code

var table = new DataTable('#example', {
  processing: true,
  serverSide: true,
  ajax: {  // the request would contain column infos / search, order, regex... etc
    url:  '/ssp/server_processing.php',
    type: 'post',
    data:  (d) => {
      d.additionl3rdPartyQuery = 'example 3rd party additional param' ,
    },
  },
});
buttons:[
    {
    text: 'Export button',
    action: function ( e, dt, button, config ) {
                 // help needed here :
                  // do the ajax again but add another param to it
             // maybe:
               dt.ajax.data[] = { newParam : 'newParamData' }
               dt.ajax.send
    }
  },
]

This question has an accepted answers - jump to answer

Answers

  • kthorngrenkthorngren Posts: 21,261Questions: 26Answers: 4,934
    Answer ✓

    I think the plugin referenced in this FAQ is what you are looking for. You may need to modify the plugin to support the additional parameters.

    Kevin

  • souljacksouljack Posts: 7Questions: 2Answers: 0

    It is exactly it! I wont be needing the plugin, but the source code is enough to understand what I need to do. Thank you very much for your reply!

    For anyone wondering, I'll be getting the params with

    dt.ajax.params()
    ------
    buttons:[
        {
        text: 'Export button',
        action: function ( e, dt, button, config ) {
           let params = dt.ajax.params()
    
          // send it with ajax/axios to the server or customizing it more
        }
    ]
    

    and doing with them as I see fit!

    Thanks again Kevin!

Sign In or Register to comment.