TableTools - Update sURL on aButtons with fn.Settings()
TableTools - Update sURL on aButtons with fn.Settings()
I am trying to update a setting in my datatable that has to do with tabletools export. When a button is clicked on the page, the server side DT is reloaded via the api().ajax.url(x).load method. This works great to reload the DT with a new URL. The data is displayed.
However, we also use an export button (oInit.tableTools.aButtons[0].sUrl) and I'd like to update that value. I am able to get the value with fn.Settings() and update it. When I push the button on the UI to export, it uses the old sURL for some reason.
var ftableFound = $('#sameFID').dataTable();
if (ftableFound) {
var newUrltes = '/api/DataTable/load?psrc=TAX-EMPLOYER-SUMMARY&qs=' + vm.parsedQuerystring();
var oSettings = ftableFound.fnSettings();
alert("surl before" + oSettings.oInit.tableTools.aButtons[0].sUrl);
ftableFound.api().ajax.url(newUrltes).load();
console.log(oSettings.oInit);
oSettings.oInit.tableTools.aButtons[0].sUrl = '/api/DataTable/csv?psrc=TAX-EMPLOYER-SUMMARY&qs=' + vm.parsedQuerystring();
oSettings.oInit.tableTools.aButtons[0].sButtonText = "CSV Dude";
alert("surl after change " + oSettings.oInit.tableTools.aButtons[0].sUrl);
ftableFound.fnDraw();
alert("surl after draw " + oSettings.oInit.tableTools.aButtons[0].sUrl);
console.log(oSettings.oInit.tableTools);
}
Is there some other parameter I need to set? Does fnDraw() take care of updating this setting? When I dump the settings to console, I can see that sURL is updated with the new value.
Answers
Sorry, the code is a little hard to read because of my alerts and console logging. Initially I was just using ajax.url.load to rebuild the data table with new data. Then I realized that while the table rebuilt fine, the export still was using the old URL.
Hi,
Unfortunately there is no public API to be able to dynamically change the
sUrl
option of the DataTables button.What I would suggest is that you use a custom
fnClick
function that will trigger an$.ajax
call and that way you can have complete control over where you get the URL from (a function call, a variable, etc).Allan
Thanks Allan, I will try and pursue your recommendation.
Interestingly enough, I am updating sURL and redrawing. After redrawing, sURL is updated, but doesn't seem to be applied. Is that what you mean by "no public API to dynamically change"?
What I meant is that there is no documented way of doing it. I suspect your update isn't working because of a closure somewhere, but it isn't something I've tried I must confess.
Allan