Magento – Adding onclick event and id selector for an Admin Grid column of type radio

admincolumngridmagento-1.7product

I have an admin grid which shows up product details(like the one in Catalog/Manage Products) with the first column as radio type.

How to add onclick event, which should call a js function with a parameter of dynamic value(such as value of radio button itself), and id selector for the radio button, whose value should be a combination of static and dynamic value(such as column_id+radio_button_value).

The class which defines the column declaration is in the class which extends from Mage_Adminhtml_Block_Widget_Grid and my column declarations are in the _prepareColumns method, and the code for the first two columns in my grid has been declared as follows:

$this->addColumn('products_grid_radio',
         array(
                'header' => '',
                'type'      => 'radio',
                'html_name' => 'products_grid_radio',
                'value'    => array('1'),
                'align' =>'center',
                //'id'   => 'products_grid_radioproducts_grid_radio_value',
                //'onclick'   =>   'setProduct(products_grid_radio_value);',
           ));

   $this->addColumn('entity_id',
        array(
            'header'=> Mage::helper('catalog')->__('ID'),
            'width' => '50px',
            'type'  => 'number',
            'index' => 'entity_id',
    ));

What is the correct way to add onclick event and id selector to a column of radio type?

Many Thanks in Advance.

Best Answer

If you have a look at the renderer for the column type radio, Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Radio, you can see that there is no way to add an onclick event listener:

public function render(Varien_Object $row)
{
    $values = $this->getColumn()->getValues();
    $value  = $row->getData($this->getColumn()->getIndex());
    if (is_array($values)) {
        $checked = in_array($value, $values) ? ' checked="checked"' : '';
    } else {
        $checked = ($value === $this->getColumn()->getValue()) ? ' checked="checked"' : '';
    }
    $html = '<input type="radio" name="' . $this->getColumn()->getHtmlName() . '" ';
    $html .= 'value="' . $row->getId() . '" class="radio"' . $checked . '/>';
    return $html;
}

If you are looking to duplicate the functionality of the customer or product grid mass actions, have a look at the method Mage_Adminhtml_Block_Widget_Grid::_prepareMassactionColumn().
It adds a column with the type massaction.
It is called automatically whenever you add a mass action to a grid, but can also be used independently.