Search code examples
phpmagentodrop-down-menu

Add custom select dropdown in magento grid


I'm trying to add a custom dropdown with (select/options) in my magento grid but it doesn't work, I tried with

$this->addColumn('dropdown', array(
'header' => Mage::helper('catalog')->__('Dropdown'),
'filter'    => false,
'sortable'  => false,
'type'=> 'options',
'options' => array('First'=>'firstvalue', 'second' =>'secondvalue')
));

I'm not using value from database but from an API, at first I want to display this select dropdown on my grid but all I see is a blank space.
Thanks.

EDIT :

Example

Actually what I need to do is the first row with the dropdown, and the second row is what i get with the code I provided.
I can add a link like the "view" column but it doesn't work for a dropdown


Solution

  • There is two ways of doing what you want:

    First way:

    $this->addColumn('dropdown', array(
        'header' => Mage::helper('catalog')->__('dropdown'),
        'filter'    => false,
        'sortable'  => false,
        'index' => 'stores',
        'type' => 'select',
        'values' => array('First'=>'firstvalue', 'second' =>'secondvalue')
    ));
    

    So it will display directly on your grid

    Second way:

    $this->addColumn('dropdown', array(
        'header' => Mage::helper('catalog')->__('dropdown'),
        'filter'    => false,
        'sortable'  => false,
        'index' => 'stores',
        'renderer' => 'Module_ModuleName_Block_Adminhtml_Renderer_Dropdown',
    ));
    

    Then you need to create a Dropdown.php file in Modulename\Block\Adminhtml\Renderer\ with :

    <?php
    class Module_Modulename_Block_Adminhtml_Renderer_Dropdown extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract{
    
    public function render(Varien_Object $row) {
        $html = '<select>';
        $html .= '<option value="First">First value</option>';
        $html .= '<option value="Second">Second value</option>';
        $html .= '</select>';
        return $html;
      }
    }
    

    You just have to replace Module, Modulename with yours and Dropdown with what you want.