Magento 2 – How to Add Action on New Button in Backend Form Container

actionadminformadminhtmlajaxmagento2

I've successfully added a new button on a form container (near New, Save, Save and Continue) in backend. I tried to use ajax… first adding JS code in adminhtml/layout/default. But the js code I write does not recognize require or define statements, so I don't have access to requirejs/jquery. How can I properly include javascript in admin or how can I map a controller action to my custom button?

Any ideas?

Best Answer

please try:

Add javascript to content:

<referenceContainer name="content">
        <block class="Magento\Framework\View\Element\Template" name="protabs.script" template="Vendor_Module::script.phtml"/>
    </referenceContainer>

You can change Magento\Framework\View\Element\Template to your block

script.phtml (templates/script.phtml):

<script type="text/javascript">
require([
    'jquery'
], function(jQuery){
    (function($) {
        your script here...
    })(jQuery);
});