Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change AJAX data in DataTables

I have a DataTable which is able to successfully fetches AJAX data once while initialising the table.
Now I need to be able to "refetch" the AJAX data to update the DataTable, but without re-initialising the table.

After some research I found out I need to use the following line "as a function":

ajax.data( data, settings )

Explained here: http://datatables.net/reference/option/ajax.data

However. I cannot find how to use this "as a function". It has not one example on the page.

I tried it as follows.
My original DataTable creation:

launch_datatable_ajax = function(){
    get_ajax_data();

    dyn_t = $('#dynamic_table').DataTable({
            "ajax": {
                "url":ajax_url,
                "data":data,
                "dataSrc":""
            },
            "columns": [
                { "data" : "ID" },
                { "data" : "post_title" },
                { "data" : "supplier_company" },
                { "data" : "img_src" },
                { "data" : "tags" },
                { "data" : "post_meta" },
            ],

    });

}

Then I tried to reload the Data with new parameters. Here's where I'm stuck.

dyn_t.ajax.data(data,dyn_t.settings);

I think I'm correct for the parameters:
dyn_t.settings gives some info on: console.log(dyn_t.settings);
'data' gives my object (the get variables) on: console.log(data);

But console.log says:

Uncaught TypeError: dyn_t.ajax.data is not a function(…)

like image 585
mesqueeb Avatar asked Jan 16 '26 22:01

mesqueeb


1 Answers

Reloading can be done with dyn_t.ajax.reload();.

However your requirement is to send updated URL parameters to the server also.

To do this, you need to use the function argument for ajax: http://datatables.net/reference/option/ajax#function

The function re-validates in every ajax.reload() call. This is why the new params will be sent. So assuming you have an HTML form with id my-form, try something like this:

            ajax: function ( data, callback, settings ) {
                $.ajax({
                    url: "ajax_url?" + $("my-form").serialize(),
                    data: { start: data.start, count: data.length }
                });
            },
like image 116
k.liakos Avatar answered Jan 19 '26 19:01

k.liakos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!