1

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": ""
        },
  • If I remove the url property I get a warning from datatables saying: Invalid JSON response

  • If I remove the entire ajax property in the setting function, calling table.ajax.url(myUrl).load() expects an object with a property named data, but I have an array of objects.

1
  • I think it's not as easy as changing the url parameter.. The plugin is also doing some caching I guess.. You should rather call table.fnDestroy(); and then you re-init your Datatables with new ajax url. Also check this other SO answer Commented Mar 20, 2018 at 7:38

1 Answer 1

1

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.

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

Comments

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.