我最近(昨天其实)不得不在同一个网格上添加一个列.部分原因是因为实践不好,主要是因为另一个模块已经使用它自己的覆盖,我不想完全替换或覆盖该类.相反,这里是通过事件修改产品网格的干净方法.
应用程序/代码/地方/我的/模块的/ etc / config.xml中
<config> <adminhtml> <events> <adminhtml_block_html_before> <observers> <mymodule> <!-- Add column to catalog product grid --> <class>mymodule/adminhtml_observer</class> <method>onBlockHtmlBefore</method> </mymodule> </observers> </adminhtml_block_html_before> <eav_collection_abstract_load_before> <observers> <mymodule> <!-- Add column to product list --> <class>mymodule/adminhtml_observer</class> <method>onEavLoadBefore</method> </mymodule> </observers> </eav_collection_abstract_load_before> </events> </adminhtml> </config>
应用程序/代码/地方/我的/模块/型号/ Adminhtml /观察员
class My_Module_Model_Adminhtml_Observer { public function onBlockHtmlBefore(Varien_Event_Observer $observer) { $block = $observer->getBlock(); if (!isset($block)) return; switch ($block->getType()) { case 'adminhtml/catalog_product_grid': /* @var $block Mage_Adminhtml_Block_Catalog_Product_Grid */ $block->addColumn('COLUMN_ID',array( 'header' => Mage::helper('mymodule')->__('COLUMN HEADER'),'index' => 'COLUMN_ID',)); break; } } public function onEavLoadBefore(Varien_Event_Observer $observer) { $collection = $observer->getCollection(); if (!isset($collection)) return; if (is_a($collection,'Mage_Catalog_Model_Resource_Eav_MysqL4_Product_Collection')) { /* @var $collection Mage_Catalog_Model_Resource_Eav_MysqL4_Product_Collection */ // Manipulate $collection here to add a COLUMN_ID column $collection->addExpressionAttributeToSelect('COLUMN_ID','...Some sql goes here...'); } } }