admin管理员组

文章数量:1022997

I'm trying to add an extra js file to an admin block and I would like to do that by php code.

Scenario
My block is a tab block for admin edit form so it extends Mage_Adminhtml_Block_Widget_Form and implements Mage_Adminhtml_Block_Widget_Tab_Interface

My approach
In the tab block class __prepareLayout() method I have added this line:

 $this->getLayout()->getBlock('head')->addJs('folder/gallery.js');

This should be ok but actually is not importing any js.

I'm trying to add an extra js file to an admin block and I would like to do that by php code.

Scenario
My block is a tab block for admin edit form so it extends Mage_Adminhtml_Block_Widget_Form and implements Mage_Adminhtml_Block_Widget_Tab_Interface

My approach
In the tab block class __prepareLayout() method I have added this line:

 $this->getLayout()->getBlock('head')->addJs('folder/gallery.js');

This should be ok but actually is not importing any js.

Share Improve this question edited Nov 1, 2015 at 9:59 theB 6,7481 gold badge32 silver badges42 bronze badges asked Aug 16, 2012 at 12:01 WonderLandWonderLand 5,7047 gold badges59 silver badges77 bronze badges 3
  • possible duplicate of How to add JS programmatically in Magento? – benmarks Commented Aug 16, 2012 at 12:09
  • I think the problem is similar: ' While your code is successfully updating the head block class instance, it's happening after output has been generated from that instance' this sound as a confirm of what I feel is the problem. – WonderLand Commented Aug 16, 2012 at 12:20
  • I'm guessing if there is any code side solution – WonderLand Commented Aug 16, 2012 at 12:22
Add a ment  | 

2 Answers 2

Reset to default 4

Why not do this using layout XML? Ideally you would specify an adminhtml layout update file for your module, but you can add a local.xml file to the adminhtml/default/default/layout folder and in there do the normal <reference name="head"><action method="addJs">...</reference> call.

As suggested by @benmarks this is a particular situation but similar to the one described How to add JS programmatically in Magento?

The problem is the same 'head' block is already rendered.

I found a solution for this particular case ( add js in a edit tab form block in backend ):

to solve this I have added the above line in the _prepareLayout() method of the block that extends Mage_Adminhtml_Block_Widget_Form

I'm trying to add an extra js file to an admin block and I would like to do that by php code.

Scenario
My block is a tab block for admin edit form so it extends Mage_Adminhtml_Block_Widget_Form and implements Mage_Adminhtml_Block_Widget_Tab_Interface

My approach
In the tab block class __prepareLayout() method I have added this line:

 $this->getLayout()->getBlock('head')->addJs('folder/gallery.js');

This should be ok but actually is not importing any js.

I'm trying to add an extra js file to an admin block and I would like to do that by php code.

Scenario
My block is a tab block for admin edit form so it extends Mage_Adminhtml_Block_Widget_Form and implements Mage_Adminhtml_Block_Widget_Tab_Interface

My approach
In the tab block class __prepareLayout() method I have added this line:

 $this->getLayout()->getBlock('head')->addJs('folder/gallery.js');

This should be ok but actually is not importing any js.

Share Improve this question edited Nov 1, 2015 at 9:59 theB 6,7481 gold badge32 silver badges42 bronze badges asked Aug 16, 2012 at 12:01 WonderLandWonderLand 5,7047 gold badges59 silver badges77 bronze badges 3
  • possible duplicate of How to add JS programmatically in Magento? – benmarks Commented Aug 16, 2012 at 12:09
  • I think the problem is similar: ' While your code is successfully updating the head block class instance, it's happening after output has been generated from that instance' this sound as a confirm of what I feel is the problem. – WonderLand Commented Aug 16, 2012 at 12:20
  • I'm guessing if there is any code side solution – WonderLand Commented Aug 16, 2012 at 12:22
Add a ment  | 

2 Answers 2

Reset to default 4

Why not do this using layout XML? Ideally you would specify an adminhtml layout update file for your module, but you can add a local.xml file to the adminhtml/default/default/layout folder and in there do the normal <reference name="head"><action method="addJs">...</reference> call.

As suggested by @benmarks this is a particular situation but similar to the one described How to add JS programmatically in Magento?

The problem is the same 'head' block is already rendered.

I found a solution for this particular case ( add js in a edit tab form block in backend ):

to solve this I have added the above line in the _prepareLayout() method of the block that extends Mage_Adminhtml_Block_Widget_Form

本文标签: javascriptAdd extra js in backend block (tab edit form) in MagentoStack Overflow