Magento 1.9 – Displaying Data in Custom Order Grid

magento-1.9order-grid

Using How to create a custom grid from scratch article, I have created a custom order grid in Magento admin.

Previously I had added one custom column with name 'my_data' in sales_flat_ordertable.

Now, to display data from this column in Order Grid, I have added following code:

$this->addColumn('my_data', array(
            'header'   => $helper->__('My Data'),
            'index'    => 'my_data',
            'filter_index'=>'main_table.my_data'
        ));

Data is displaying correctly from that column.

My problem is, I have stored values like 1,2,3 in 'my_data' column and now I want to display different values in Order grid depending on the values from 'my_data' column.

For example :

If 1 then 'A' should be displayed.
If 2 then 'B' should be displayed.
If 3 then 'C' should be displayed.

Best Answer

Magento Grid is a very handy tool for flexible data display. In Magento it is usually presented as a table data.There is a possibility to modify tables data if necessary. To do this, you need to add a column to Magento grid and overwrite the default renderer with a custom renderer in magento. your code will follow like

$this->addColumn('my_data', array(
    'header'   => $helper->__('My Data'),
    'index'    => 'my_data',
    'filter_index'=>'main_table.my_data'
    'renderer'  => 'Yourpackagename_Yourmodulename_Adminhtml_Sales_Order_Renderer_Customvalue',
));

Make directory called Renderer inside directory where your Grid.php is located and make file Customvalue.php

<?php
class Yourpackagename_Yourmodulename_Adminhtml_Sales_Order_Renderer_Customvalue extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Action
{
    public function render(Varien_Object $row)
    {
        return $this->_getvalue($row->getData($this->getColumn()->getIndex()));
    }

    public function _getvalue($value){
        if($value == 1){
            return 'A';
        }elseif($value == 2){
            return 'B';
        }elseif($value == 3){
            return 'C';
        }
    }
}
?>
Related Topic