Add 'data_callback' to grid via php not xml - is it possible in Magento?
Let me preface this question by declaring my newbie status when it comes to front end development...
I'm writing an admin module with a new screen in the admin panel, consisting of a number of tabs. Each tab is added using the $this-addTab(...) function in a Tabs.php file e.g.
$this->addTab('form_products', array(
'label' => Mage::helper('mymodule')->__('All The Products'),
'title' => Mage::helper('mymodule')->__('All The Products'),
'content' => $this->getLayout()->createBlock ('mymodule/adminhtml_something_edit_tab_productsform')->toHtml(),
));
This tab has a list of products, with a checkbox field populated via a function getSelectedProducts(). The block file for this tab extends Mage_Adminhtml_Block_Widget_Grid
I see from core Magento that you use a declaration something like the following to declare the hidden field name and data callback function :
<block type="adminhtml/widget_grid_serializer" name="related_grid_serializer">
<reference name="related_grid_serializer">
<action method="initSerializerBlock">
<grid_block_name>catalog.product.edit.tab.related</grid_block_name>
<data_callback>getSelectedRelatedProducts</data_callback>
<hidden_input_name>links[related]</hidden_input_name>
<reload_param_name>products_related</reload_param_name>
</action>
<action method="addColumnInputName">
开发者_开发技巧 <input_name>position</input_name>
</action>
</reference>
</block>
My question is, is it possible to declare the hidden input name and callback function etc. during construction of the block within the php?
If not, then I need help with the layout xml.......
At the moment my layout.xml file looks like:
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_adminhtml_something_index>
<reference name="content">
<block type="mymodule/adminhtml_something" name="something" />
</reference>
</mymodule_adminhtml_something_index>
</layout>
I have tried adding a nested block declaration within this e.g.
<?xml version="1.0"?>
<layout version="0.1.0">
<mymodule_adminhtml_something_index>
<reference name="content">
<block type="mymodule/adminhtml_something" name="something" />
<block type="mymodule/adminhtml_something_edit_tab_productsform" name="mymodule.adminhtml.something.edit.tab.productsform"/>
<block type="adminhtml/widget_grid_serializer" name="something_grid_serializer">
<reference name="something_grid_serializer">
<action method="initSerializerBlock">
<grid_block_name>mymodule.adminhtml.something.edit.tab.productsform</grid_block_name>
<data_callback>getSelectedProducts</data_callback>
<hidden_input_name>links[selected]</hidden_input_name>
<reload_param_name>products_selected</reload_param_name>
</action>
</reference>
</block>
</reference>
</mymodule_adminhtml_something_index>
</layout>
The above errors, as it is loading my block file at the same time as main block file (which obviously is not correct). I have messed around with nesting the blocks above, and declaring in seperate tags e.g.
<mymodule_adminhtml_something_edit_form>
The driving force behind this is trying to get the data of any checkboxes selected on the products form posted back when the form is saved, which at the moment is not happening.
You need to know that <action method=...>
in layout files is just calling the named method on the instantiated block.
In the above example related_grid_serializer
is an object of Mage_Adminhtml_Block_Widget_Grid_Serializer
and that this,
<action method="initSerializerBlock">
<grid_block_name>catalog.product.edit.tab.related</grid_block_name>
<data_callback>getSelectedRelatedProducts</data_callback>
<hidden_input_name>links[related]</hidden_input_name>
<reload_param_name>products_related</reload_param_name>
</action>
is exactly equivalent to:
->initSerializerBlock('catalog.product.edit.tab.related',
'getSelectedRelatedProducts',
'links[related]',
'products_related');
You can call that method directly from your own class.
(Note the <grid_block_name>
, <data_callback>
, <hidden_input_name>
and <reload_param_name>
tag names are ignored. They exist for readability reasons only.)
精彩评论