Hello,
how do I load a custom js in adminhtml, in customer edit form?
thanks a lot
Solved! Go to Solution.
You need to create custom module.
In custom module, create customer_index_edit.xml file under Vendor/Modulename/view/adminhtml/layout/customer_index_edit.xml
Now keep content below,
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <script src="Vendor_Modulename::js/customer.js"/> </head> </page>
Create customer.js file under Vendor/Modulename/view/adminhtml/web/js/customer.js
require([ 'jquery' ], function ($) { 'use strict'; $(document).ready(function(){ // custom js }); });
Run command,
php bin/magento setup:static-content:deploy -f
You need to create custom module.
In custom module, create customer_index_edit.xml file under Vendor/Modulename/view/adminhtml/layout/customer_index_edit.xml
Now keep content below,
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <head> <script src="Vendor_Modulename::js/customer.js"/> </head> </page>
Create customer.js file under Vendor/Modulename/view/adminhtml/web/js/customer.js
require([ 'jquery' ], function ($) { 'use strict'; $(document).ready(function(){ // custom js }); });
Run command,
php bin/magento setup:static-content:deploy -f
Hello @agiorgini
Magento 2 does not adding js using layout xml.
you need to add using requirejs-config.js
var config = { map: { '*': { customAdmin: 'Namespace_Modulename/js/customAdmin' } }, deps: ["jquery"] };
More info:- https://magento.stackexchange.com/questions/153378/magento-2-load-custom-javascript-with-requirejs
Hope it will help you.
If works then mark as solution.
Hi @agiorgini
AKAIK - you must need to use requirejs-config-js concept of magento 2 in this case.
here assuming you are creating custom module so you need to create your custom js and needs to including in your requirejs-config-js file
As you wanted to use this in admin side - so you need to add requirejs in admin area
Below is the full path :
VendorName_ModuleName/view/adminhtml/requirejs-config.js file and add below code in it :
var config = { map: { '*': { customJs: 'Namespace_Modulename/js/customAdmin' } }, deps: ["jquery"] };
Now open your customer_edit template phtml file and load your custom js into it.
<script> require(['jquery', 'Namespace_Modulename/js/customAdmin'], function ($) { return ...; } ); </script>
you can refer this link for more details - http://inchoo.net/magento-2/custom-javascript-in-magento-2-with-requirejs/
Hello @agiorgini
Solution does not work for all cases with js merge.
Use Magento 2 best approach for same.
With above solutions there are not any merging issue will be happend. Its working for all cases of Js Merge, bundling and so on.
So Don't worry about Js Merging or any other stuff.
@agiorgini I have already test before posted this solutions for all cases.
If you have still any issue let me know.
I already faced a similar issue.
Hope it will work
It's working for me