reload ajax with modified POST parameters ?

reload ajax with modified POST parameters ?

zoltarzoltar Posts: 4Questions: 1Answers: 0

Hi !

I've been looking around but I can't seem to find how to do this easilly.
Here's what I'd like to do :
- I have a PHP script that returns JSON data which fills a table.
- I call it with a url (which doesn't change)
- I have parameters that will change according to buttons or fields edited by the user

I'd like to be able to reload the table's data with the current state of those parameters.

Right now, I've seen that I can use GET and modify the url by hand ie script.php?param1=1&param2=2 etc
but's it's not really satisfying, and I'd like to use POST instead.

Is it possible ?
I'd either like to have param1 etc bound to a js variable (easy to work with, no need to update anything inside the table api)
or I could update those parameters inside the api, but I don't know where !

Am I making any sense ? :)

thanks !!

Replies

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Use ajax.data as a function. That function is executed on each request and therefore you can change the data as required at any point.

    Allan

  • zoltarzoltar Posts: 4Questions: 1Answers: 0

    oh thanks ! I'll try that :)

  • Mikis93Mikis93 Posts: 2Questions: 0Answers: 0

    Hi!
    I try use ajax.data as a function, but I have this error: table.ajax.data is not a function (table is my DataTable object).
    Do you have a solution?

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Yes I probably do - but I would need to be able to see your code. Please link to a test case showing the issue, per the forum rules.

    Allan

  • Mikis93Mikis93 Posts: 2Questions: 0Answers: 0

    Hi!
    This is JSBin link for testing case: https://jsbin.com/zijivav/edit?html,js,console,output.
    We simulate GET Ajax request.
    You click on the "Change AJAX Request" button to show the "ajax.data" error (ajax.data is not a function).
    Thanks.

  • allanallan Posts: 63,498Questions: 1Answers: 10,471 Site admin

    Thanks for the test case. There is no ajax.data method in DataTables. The API methods available are listed in the documentation,

    You would need to use the initialisation option ajax.data as a function to be able to dynamically change the options.

    Allan

  • EugeneOlEugeneOl Posts: 1Questions: 0Answers: 0

    You can use function as a value for ajax.data option as shown below.

    That way your code will be run every time the client makes request to the server and not once as with your initial code.

    $('#table1').DataTable({
    "serverSide": true,
    "ajax": {
    "url": "Home/Search",
    "type": "POST",
    "data": function(d){
    d.searchType = GetSearchType();
    d.searchText = GetSearchText();

    }
    }
    });

    Then use $('#table1').DataTable().ajax.reload() when you need to reload the table or $('#table1').DataTable().ajax.reload(null, false) if you don't want to reset the current page.

This discussion has been closed.