Trigger button action from code.

Trigger button action from code.

tangerinetangerine Posts: 3,342Questions: 35Answers: 394

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

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    If you do:

    $.fn.dataTable.ext.buttons.restore = {
        className: 'restoreButton',
        name: 'restoreButton',
        action: function ( e, dt, node, config ) {
    ....
    

    You can do:

    $('.restoreButton').click();
    

    or

    $('restoreButton:name').click();
    

    Alternatively you can wrap the "action" into a function that you can then also call directly from your code.

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    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.

  • rf1234rf1234 Posts: 2,801Questions: 85Answers: 406

    Those two "clicks" don't seem to do anything - no action, no errors.

    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.).

    tableWithTheCustomButton
        .on ('init', function () {
            if ( yourCondition ) {
                $('restoreButton:name').click();
            }
        })
    
  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    You can use button().trigger() to trigger the Datatables buttons.

    Kevin

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    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.

  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    By outside of the table do you mean something like this?
    http://live.datatables.net/zizeyolo/1/edit

    Kevin

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    Yes, Kevin.
    My "restore" button is one of several occupying a div, like so:

        <div class="row pt-3 pb-3"><!-- padding top and bottom 3 spacing units -->
            <div id="buttons-filter-by" class="col-md-2"></div>
            <div id="buttons-general-filters" class="col-md-2"></div>
            <div id="buttons-manage-rows" class="col-md-2"></div>
            <div id="buttons-export" class="col-md-2"></div>
            <div id="button-restore" class="col-md-2 text-right"></div>
        </div>
    
  • kthorngrenkthorngren Posts: 20,144Questions: 26Answers: 4,736

    Have you tried using the buttons.buttons.name and then use that name as the button-selector, for example? See my example, the Trigger Reload Button uses this notation to get the button to trigger.

    Kevin

  • tangerinetangerine Posts: 3,342Questions: 35Answers: 394

    Have you tried using the buttons.buttons.name....

    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.

Sign In or Register to comment.