2

I am using AJAX to load (and subsequently refresh) a JS datatable (v1.10.0). I don't think I'm doing this properly because while I can get the initial table to load, subsequent AJAX requests do not update the table.

I've tried various combinations of the .clear(), .draw() and .rows.add() JS Datatables methods, but nothing seems to work.

jquery code below:

$(document).ready(function() {

    $('#results').html('<table id="table-output" class="display" cellspacing="0" width="100%"></table>');

    var table_config = {
        "bDestroy": true,
        "paging": false,
        "language": {
            "zeroRecords": "No results found",
            "processing": "<div align='center'><img src='/static/ajax-loader.gif'></div>",
            "loadingRecords": "<div align='center'><img src='/static/ajax-loader.gif'></div>"
        }
    };       

    $("form").submit(function(e) {

        e.preventDefault();

        var form_data = JSON.stringify($(this).serializeArray());

        $.ajax({
            type: 'POST',
            url: /the_url,
            data: form_data,
            contentType: 'application/json',
            dataType: 'json',
            success: function(response) {

                table_config.data = response.data;
                table_config.columns = response.columns;

                $('#table-output').dataTable(table_config);

            }
        });
    });
});
2
  • Are you sure the AJAX call is succeeding? Commented Aug 11, 2014 at 20:24
  • Yep, checked the response in the browser. Commented Aug 11, 2014 at 21:15

2 Answers 2

1

I figured it out. Modifying the success function as follows works:

success: function(response) {
                    $('#results').html('<table id="table-output" class="display" cellspacing="0" width="100%"></table>');

                    table_config.columns = response.columns;

                    var table = $('#table-output').DataTable(table_config);
                    table.clear();
                    table.rows.add(response.data);
                    table.draw();

                }
Sign up to request clarification or add additional context in comments.

Comments

0

You can try :

var table1 = $('#youtblename').DataTable();
var pathp = "/server_processing_pie.php";

tablel.clear();
tablel.draw();
tablel.ajax.url(pathp).load();

1 Comment

this wouldn't work for me as you are using datatable's ajax method instead of jquery's (a requirement)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.