Trigger button action from code.
Trigger button action from code.
I have this button definition
$.fn.dataTable.ext.buttons.restore = {
action: function ( e, dt, node, config ) {
....
triggered by a button click.
Now it seems it would be useful to sometimes trigger it from code. Is this possible, and if so how?
Answers
If you do:
You can do:
or
Alternatively you can wrap the "action" into a function that you can then also call directly from your code.
Thank you, Roland - I've learned something new.
However, the problem persists. Those two "clicks" don't seem to do anything - no action, no errors. I had already tried copying a chunk of code from the "restore" button into the other function, again with no result and no errors.
I'm beginning to wonder if I'm even looking in the right place. I might try some scoping investigations.
In case it's relevant, the function concerned is a "editor.on("create.....)" function, part of a small "editor functions" file which is included into each individual table's script. Other functions in the same file are working ok.
The data table that has the custom button must have been initialized at that time. Any other event that implies that the data table exists can be used as well ('"select" etc.).
You can use
button().trigger()
to trigger the Datatables buttons.Kevin
I've been round in several circles on this. Had to take some time out.
The "button().trigger()" syntax suggests that the button must be inside the grid. My "restore" button sits outside the table.
I can't get any response to " $('restoreButton:name').click();" although the button definition is visible to the calling function.
I'm conceding defeat on this. But thanks for your support, guys, I appreciate it.
By outside of the table do you mean something like this?
http://live.datatables.net/zizeyolo/1/edit
Kevin
Yes, Kevin.
My "restore" button is one of several occupying a div, like so:
Have you tried using the
buttons.buttons.name
and then use that name as thebutton-selector
, for example? See my example, the Trigger Reload Button uses this notation to get the button to trigger.Kevin
No, Kevin. I got diverted down a new rabbit-hole. It seems that my "create" function isn't even called sometimes, which raises a further dimension of bafflement. I have a lot of stripping-down of functions to do, to reach the most rudimentary levels of testing.
Thanks for your help, but I think you should write this one off now.