0

How do I dynamically set a radio button on a custom grid?

so far I have tried: (each attempt has had both value and values)

'value' => array('checked'), //not based on db value but to see if it worked

'value' => $this->getdata('is_default') // var_dump shows it is null

'value' => $this->getIdFieldName('id_default')

none checked the button below is my code if that helps

class [classname] extends Mage_Adminhtml_Block_Widget_Grid
    protected function _prepareColumns()
    {
        $this->addColumn('is_default', array(
            'header'   => $this->__('Default'),
            'filter'   => false,
            'type'     => 'radio',
            'index'    => 'is_default',

        ));

        return parent::_prepareColumns();
    }

EDIT

After further investigation into this issue I have found this class Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Radio and reading the code I believe that it is possible to set the button as checked, am I right? or have I read the code wrong? or am I looking in the wrong place?

/**
 * Renders grid column
 *
 * @param   Varien_Object $row
 * @return  string
 */
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;
}
5
  • so you're trying to tick a radio button in a grid layout? does that even make sense? Commented May 23, 2014 at 12:41
  • my grid returns a number of images. 1 of these images must be the default and that is set by the user. It seems pointless the user having to click on an image to go to an edit page to set the default then check if that has worked when I can get them to set it on the grid (if that's possible)
    – tony09uk
    Commented May 23, 2014 at 12:51
  • mmm so you'd do that to avoid direct interaction with each line, that makes sense, it's quite a pain in Magento... have you considered javascript in your phtml file? Commented May 23, 2014 at 12:55
  • No, the grid I have created is simply generated from the block (I assume using the core .phtml files). I wouldn't know where to start to try and achieve that, can you offer some guidance?
    – tony09uk
    Commented May 23, 2014 at 13:09
  • I don't know, it depends on what your module is overriding. I assume you could tell your Block to use another custom template and have that template be a copy of the original tempalte used by Mage_Adminhtml_Block_Widget_Grid Commented May 23, 2014 at 13:23

1 Answer 1

2

This is what worked for me:

$this->addColumn('column_id_here', array(
    'header'     => $this->__('Label here'),
    'header_css_class' => 'a-center',
    'type'      => 'radio',
    'name'      => 'column_id_here',
    'values'    => $this->_getSelectedValue(),
    'align'     => 'center',
    'index'     => 'column_id_here',
    'html_name' => 'column_id_here',
    'sortable'  => false,
    'filter'    => false
));

And I have a method called _getSelectedValue() that returns the value of the selected radio button.
For test purporses implement it like this:

protected function _getSelectedValue() {
    return 1; //or any other value that exists for your column id.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.