Unable to reload server-side data into table

Unable to reload server-side data into table

ThaMe90ThaMe90 Posts: 4Questions: 1Answers: 0
edited September 2014 in Free community support

I am trying to reload the data in a DataTable upon an action by a user (be it add or delete).
The table is initially created when the page is loaded as follows:

table.DataTable({
    "processing": true,
    "serverSide": true,
    "drawCallback": function(settings) {
        var api = this.api();
        thisTable.DrawCallback(settings, api);
    },
    "ajax": {
        "url": 'apps/app.py',
        "data": function (d) {
            d.Action = 'Table';
            d.Table = thisTable.description.Table;
            d.Name = thisTable.appDescription.Name;
            d.Database = thisTable.appDescription.Database;
        }
    },
    "columns": columns
});

Eventually, when the user adds a new entry, the following code is executed:

var table = $(this.id + "-table").DataTable();
var data = new Object;
data["Action"] = "Add";
data["Database"] = this.appDescription.Database;
data["Table"] = this.description.Table;
data["Values"] = $(this.theForm).serializeArray();

$.ajax( {
    dataType: "json",
    url: "/apps/app.py",
    data: data,
    success: function(data) {
        if (data["AddedRows"] > 0) {
            // Refresh page...
            table.clear();
            table.ajax.reload();
            table.draw();
        }
        // else?
    },
    error: function() {
        alert("Unable to add data to table in database.");
        // Check error on db?   
    }
} );

The code in the success function does execute, but I don't see any data requests on my server upon executing the ajax.reload().
Any ideas?

Answers

  • ThaMe90ThaMe90 Posts: 4Questions: 1Answers: 0

    I am unable to provide a link to an example, so I hope this debug info helps.

  • ThaMe90ThaMe90 Posts: 4Questions: 1Answers: 0

    Is it possible that there is a limitation where datatables can't request new data via Ajax when this new request is done based on the result of a different request?

  • ThaMe90ThaMe90 Posts: 4Questions: 1Answers: 0

    The issue was caused by the fact that I did not include a "#" character in the jQuery selector:

    var table = $("#" + this.id + "-table").DataTable();
    

    instead of

    var table = $(this.id + "-table").DataTable();
    

    This resulted in a DataTable object on an empty jQuery object (hoorah for jQuery I suppose).

This discussion has been closed.