Add custom column and get custom renderer value in Magento grid

Home / Magento / Add custom column and get custom renderer value in Magento grid

This post is helpful to add custom column and get custom renderer value in Magento grid admin.

In /app/code/core/Mage/Adminhtml/Block/Catalog/Product/Grid.php file.

<?php protected function _prepareCollection() {
 $collection = Mage::getResourceModel($this->_getCollectionClass());

//$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('postcode')); // Added my developer
 //$collection->getSelect()->joinLeft('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id','method'); // Added my developer
 $collection->getSelect()->joinLeft('sales_flat_order_status_history', 'main_table.entity_id = sales_flat_order_status_history.entity_id','comment'); // Added my developer
 $this->setCollection($collection);
 return parent::_prepareCollection();
}
?>
<?php protected function _prepareColumns() {
 /**** start *****/
 $this->addColumn('comment', array(
 'header' => Mage::helper('sales')->__('comment'),
 'index' => 'comment',
 'filter' => false,
 'sortable' => false,
 'renderer' => 'Mage_Adminhtml_Block_sales_Order_Renderer_Red',
 ));
 /**** end ******/
}
?>

render_column

Make directory called Renderer inside directory where your Grid.php is located and make file Red.php
Make class
Mage_Adminhtml_Block_Catalog_Product_Renderer_Red extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract

<?php
class Mage_Adminhtml_Block_sales_Order_Renderer_Red extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract {
 public function render(Varien_Object $row) {
 $value = $row->getData($this->getColumn()->getIndex());
 return '<span style="color: red;">'.$value.'</span>';
 }
}
?>

Related Posts

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *