mnieto
mnieto

Reputation: 3874

configure datatables but call ajax after filter

My question is simmilar to this: jQuery Datatable - changing data url but I don't want to make the ajax call while setting the datatable

I have this setting for the datatable:

p._createDataTableMappings = function (application, field) {
        $('#data_mappings').DataTable({
            "dom": 'rtip',
            "ajax": {
                "url": "/api/applications/" + application + "/mappings/" + field,
                "dataSrc": ""
            },
            "columns": [
                { "data": "Application1" }, 
                { "data": "Field1" },
                { "data": "Value1" },
                { "data": "Application2" },
                { "data": "Field2" },
                { "data": "Value2" }
            ]
        });

The data returned by the url is an array of objects, so I put the dataSrc to ""

But I don't want to make the ajax call until application and field values have data. These values are set from this function:

$("#search").on("click", function (event) {
    var application_value = $("#application").val();
    var field_value = $("#field").val();
    if (application_value && field_value) {
        var table = $("#data_mappings").DataTable();
        table.ajax.url("/api/applications/" + application_value + "/mappings/" + field_value).load();
    }
});

My doubt is how to configure the ajax call

        "ajax": {
            "url": "/api/applications/" + application + "/mappings/" + field,
            "dataSrc": ""
        },

Upvotes: 1

Views: 679

Answers (1)

colin0117
colin0117

Reputation: 1542

As @Yuri said, the best way is probably to destroy the table and rebuild it. This live example may help - you press a button to load the data, so this would be analogous to your click event.

Upvotes: 1

Related Questions