• Resolved subi88

    (@subi88)


    Hi and thanks for the great plugin!

    I am quite new to WP and Javascript. My goal is to get dynamic data from a MySQL DB and display it via datatables. As of now, everything works perfectly, besides that I’d like to get rid of refreshing the whole page to get new table data and use AJAX instead.

    The question is how to call functions with WP DataTable. This is how I use the plugin:

    [wp-datatable id="table"]
        paging: false,
        responsible: true,
        search: true,
        stateSave: true,
     [/wp-datatable]

    datatables.net has the following codesnippet listed as example which should do exactly what I want:

    var table = $('#example').DataTable( {
        ajax: "data.json"
    } );
     
    setInterval( function () {
        table.ajax.reload( null, false ); // user paging is not reset on reload
    }, 30000 );

    Is it possible to implement this code?
    I am using the Ajaxify WordPress Site Plugin.

    Thanks so much in advance!

    All the best,
    Julian

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author samsk

    (@samsk)

    Hi, sorry for late answer – but smth. like this should be possible:

    [wp-datatable id=”table”]
    paging: false,
    responsible: true,
    search: true,
    stateSave: true,
    ajax: {
    url: “https://…AJAX….URL”,
    dataType: ‘json’,
    cache: false,
    type: ‘GET’,
    },
    columns: {
    ….columns…definition…
    }
    [/wp-datatable]

    Data have to be in format as expected by datatables, or you’ll have to modify them in ajax handler. Please consult jQuery datatables for more info.

    Thread Starter subi88

    (@subi88)

    Hey,

    thanks for your helpful answer! I am still interested in finding a solution.

    Your code works, I am able to display JSON data via ajax. So we’re almost there ??

    It is important for me that the table is refreshing/reloading automatically. As I wrote above, I need to implement a datatables function. The ajax.reload function is discribed here: https://datatables.net/reference/api/ajax.reload()

    setInterval( function () {
    table.ajax.reload( null, false );
    }, 30000 );

    Do you have an idea how and where I can call this function in the shortcode?

    Thanks and have a great day.

    Plugin Author samsk

    (@samsk)

    Hi,
    look at one of the datatables callbacks on https://datatables.net/reference/option/ – initComplete might be a good choice.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Calling functions / Refreshing Data with AJAX’ is closed to new replies.