ajax reload FULL example

ajax reload FULL example

coderman1coderman1 Posts: 6Questions: 1Answers: 0
edited July 2013 in General
Does someone have a link to a working fnReloadAjax example? Every example I see on here is either missing the HTML or missing all the javascript as people only post snippets of their code.

If someone can provide a full example (html + javascript) it would really help me out!

Thanks!

Replies

  • coderman1coderman1 Posts: 6Questions: 1Answers: 0
    Also, I was trying to get the table to refresh every X seconds using AJAX if that helps any
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    It should be that all you need to do is:

    [code]
    setInterval( function () {
    table.fnReloadAjax();
    }, x * 1000 );
    [/code]

    where `table` is your DataTable instance and `x` is the number of seconds.

    If that doesn't work for you, please link to the page so we can see what is going wrong.

    Allan
  • coderman1coderman1 Posts: 6Questions: 1Answers: 0
    Allan,

    Below is my code. It is running without any errors, but not displaying any data in the table

    [code]


    var oTable = null;

    $(document).ready(function()
    {
    oTable = $('#activeCampaignTable').dataTable( {
    "bProcessing": true,
    "bServerSide": true,
    "bRetrieve": true,
    "sAjaxSource": "get_data.php"
    } );

    setTimeout(refreshTable, 1000);
    });

    $.fn.dataTableExt.oApi.fnReloadAjax = function ( oSettings, sNewSource, fnCallback, bStandingRedraw )
    {
    ...
    }

    function refreshTable()
    {
    setInterval( function () {
    oTable.fnReloadAjax("get_data.php");
    }, 3 * 1000 );
    }

    get_data.php:

    <?php
    include ("business_logic/data.php");
    $campaigns = db_get_customer_list();

    $json_array = [];
    $rows = 0;
    while($c = query::fetch_next_array($campaigns))
    {
    $row_set[] = $c;
    $rows += 1;
    }

    $output = array(
    "sEcho" => "",
    "iTotalRecords" => $rows,
    "iTotalDisplayRecords" => $rows,
    "aaData" => $row_set
    );

    $response = json_encode($output, JSON_FORCE_OBJECT);
    echo $response;
    ?>

    [/code]

    Output of get_data.php is something like this:
    {"sEcho":"","iTotalRecords":101,"iTotalDisplayRecords":101,"aaData":{"0":{"0":"4","custid":"4","1":"Catheter Sites","companyname":"Catheter Sites"},"1":{"0":"5","custid":"5","1":

    One thing I need to figure out is why the query results return both the index to the column and the name. I'd imagine that would throw the binding off?
  • allanallan Posts: 63,516Questions: 1Answers: 10,472 Site admin
    > "sEcho" => ""

    That's not a valid server-side processing response. See: http://datatables.net/usage/server-side if you want to implement server-side processing.

    Allan
This discussion has been closed.