Reputation: 3874
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.
Upvotes: 1
Views: 679
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