how to target table in stateRestore ajax request
how to target table in stateRestore ajax request
With the ajax: function(data, callback)
from stateRestore.ajax, can you target a specific table for the request. I have 5 tables on my page and I'm trying to use $.fn.dataTable.defaults
to extend the savedStates
button. All 5 tables will use the same ajax script but I need to identify the table from whence the request comes from in order to process correctly in my script. I know I can duplicate the buttons
code I have from the defaults section and copy that entire section over 5 times, but I'm hoping I don't need to do that, since that would require me to copy several buttons worth of code, where the only thing that needs to change between them is the tableID.
Here's the code I'm using:
{
extend: 'savedStates',
config:
{
ajax: function (data, callback) {
if (data.action === 'rename') {
$.ajax({
type: 'POST',
url: 'ajaxStateSave.php',
data: {
"tableID": "table1",
"activeuser": "<?=$activeuser?>",
"action": "rename",
"stateRestore": JSON.parse(JSON.stringify(data.stateRestore))
},
});
callback();
}
else if (data.action === 'remove') {
$.ajax({
type: 'POST',
url: 'ajaxStateSave.php',
data: {
"tableID": "table1",
"activeuser": "<?=$activeuser?>",
"action": "remove",
"stateRestore": JSON.parse(JSON.stringify(data.stateRestore))
},
});
callback();
}
else if (data.action === 'save') {
$.ajax({
type: 'POST',
url: 'ajaxStateSave.php',
data: {
"tableID": "table1",
"activeuser": "<?=$activeuser?>",
"action": "save",
"stateRestore": JSON.parse(JSON.stringify(data.stateRestore))
},
});
callback();
}
}
}
}
Within the above code, I want to replace "table1" with the actual table ID and then send that as part of my ajax request.
Thanks for any help you can offer.
Tanner
This question has an accepted answers - jump to answer
Answers
That's not possible, unfortunately. You could reduce the code duplication by just adding a wrapper, something like:
and then have your original code in
wrapperFunction()
. Not ideal, but not too much duplication,Colin
Thanks, Colin. That will work and does cut down on code duplication quite a bit
That's possible: