When develop a E-Commerce site base on magento, you will find mostly all information you can
get on the magento admin panel display as a Grid. Some situation we have to customize the displaying
information of the column.
There are two solution can do that:
1. Use the " format " attribute of column.
2. Use the " renderer " attribute of column .
Such as : on the Report->Shopping cart -> product in carts list grid ----Magento admin. we want the name of the product display as a link we can click it to the product detail page.
First Solution:
1. Rewrite adminhtml/ report_shopcart_product_grid (refernce to magento rewrite block ):
2. Change the grid block class like this:
Replace: $this ->addColumn( 'entity_id' , array ( 'header' =>Mage::helper( 'reports' )->__( 'ID' ), 'width' => '50px' , 'align' => 'right' , 'index' => 'entity_id' )); $this ->addColumn( 'name' , array ( 'header' =>Mage::helper( 'reports' )->__( 'Product Name' ), 'index' => 'name' ));
With: $this ->addColumn( 'entity_id' , array ( 'header' =>Mage::helper( 'reports' )->__( 'ID' ), 'width' => '50px' , 'align' => 'right' , 'index' => 'entity_id' )); $this ->addColumn( 'name' , array ( 'header' =>Mage::helper( 'reports' )->__( 'Product Name' ), 'index' => 'name' , 'format' => '<a href="/catalog/product/view/id/$entity_id" target="_blank">$name</a>' )); |
Second Solution:
1. Rewrite adminhtml/ report_shopcart_product_grid (refernce to magento rewrite block ):
2. Rewrite adminhtml/ report_grid_column_renderer_ link like this
<?php class Red _Adminhtml_Block_Report_Grid_Column_Renderer_ Link extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract { public function render(Varien_Object $row ) { $id = $row ->getData( $this ->getColumn( 'entity_id' )->getIndex()); $value = $row ->getData( $this ->getColumn()->getIndex());; Return " <a href="/catalog/product/view/id/ { $ id} " target="_blank">$ value </a> " ; } } |
3. Change the grid block class like this:
Replace: $this ->addColumn( 'entity_id' , array ( 'header' =>Mage::helper( 'reports' )->__( 'ID' ), 'width' => '50px' , 'align' => 'right' , 'index' => 'entity_id' )); $this ->addColumn( 'name' , array ( 'header' =>Mage::helper( 'reports' )->__( 'Product Name' ), 'index' => 'name' ));
With: $this ->addColumn( 'entity_id' , array ( 'header' =>Mage::helper( 'reports' )->__( 'ID' ), 'width' => '50px' , 'align' => 'right' , 'index' => 'entity_id' )); $this ->addColumn( 'name' , array ( 'header' =>Mage::helper( 'reports' )->__( 'Product Name' ), 'index' => 'name' , 'renderer' => 'adminhtml/report_grid_column_renderer_ link '//the block you change to deal with the data )); |
NOTE: The first solution just can be used when you want to change the string to anthoer string type: such as adding htm, add other string.......If you want to make some operation with this column value, you need use the second one.