1

I'm trying to add a custom js file in magento 2 in the admin but all I can see are sample on how to add in the frontend. I found some that shows how to add in admin but none works. Below are my code that I copied from some sources

//app/code/COMPANY/PRODUCT/view/adminhtml/requirejs-config.js
var config = {
    "map": {
        "*": {
            "generateMenu": "TCM_HeaderMenu/js/generateMenu"
        }
    }
}

and in the file located in app/code/COMPANY/PRODUCT/view/adminhtml/web/js/generateMenu.js

  /**
   * Copyright © Magento, Inc. All rights reserved.
   * See COPYING.txt for license details.
   */
  require([
      'jquery',
      'logger'

  ],  function($, logger) {
      'use strict';
      logger.log('inchoo.js is loaded!!');

      //creating jquery widget
      $.widget('generateMenu.js', {
          _create: function() {

              this.element.on('click', function(e){
                  logger.log("You click on element: " + e.target);
              });
          }

      });

    return $.generateMenu.js;
  });

But it seems that this is not being called when I refresh the page.

Note: I'm trying to load the custom js in Catalog->category page in the admin panel

1

Try :

app/code/Company/Product/view/adminhtml/requirejs-config.js

var config = {

    deps: [
        "Company_Product/js/generateMenu"
    ]

};

app/code/Company/Product/view/adminhtml/web/js/generateMenu.js

define(["jquery"], function($) {
  "use strict";
 //Your code here
});

clean cache

clean var/view_preprocessed content

clean pub/static content

deploy the static content : php bin/magento setup:static-content:deploy -f

  • 1
    You're the star thanks a lot you saved me been looking for this answer for hours now – MadzQuestioning Aug 1 at 13:11
0

please try with below code :

var config = {
    map: {
        '*': {
            generateMenu:'TCM_HeaderMenu::js/generateMenu'
        }
    }
};

i hope its work for you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.