2

I'm having trouble figuring out how to load a custom grid with a radio button pre-selected.

For example, I added a custom "Tax Class" tab to the Admin Product Edit page, with a grid that loads the tax/class collection.

I already know the current product's assigned tax_class_id, Mage::registry('product')->getTaxClassId(), I just need to figure out how to select that corresponding row in the grid.

Any ideas are much appreciated, relevant code snippet below:

class Kmdsax_ProductTaxClassTab_Block_Adminhtml_Catalog_Product_Tab 
extends Mage_Adminhtml_Block_Widget_Grid
implements Mage_Adminhtml_Block_Widget_Tab_Interface {

public function __construct()
{
    parent::__construct();
    $this->setId('taxClassGrid');
    $this->setDefaultSort('class_name');        
    $this->setDefaultDir('ASC');
    $this->setSaveParametersInSession(true);
    $this->setUseAjax(true);
}

protected function _prepareCollection()
{
    $collection = Mage::getModel('tax/class')
        ->getCollection()                        
        ->setClassTypeFilter('PRODUCT');

    //$product = Mage::registry('product');
    //$taxClassId = $product->getTaxClassId();

    $this->setCollection($collection);
    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    $this->addColumn('tax_class_id', array(
        'header'    => Mage::helper('adminhtml')->__('Selected'),
        'type'      => 'radio',
        'html_name' => 'taxClassId',
        'align'     => 'center'
    ));

    $this->addColumn('op_avatax_code', array(
        'header'    => Mage::helper('tax')->__('Avatax Code'),
        'align'     => 'left',
        'index'     => 'op_avatax_code'
    ));

    $this->addColumn('class_name', array(
            'header'    => Mage::helper('tax')->__('Class Name'),
            'align'     => 'left',
            'index'     => 'class_name'
    ));

    return parent::_prepareColumns();
}

screenshot for context: enter image description here

EDIT Thanks to Sohel Rana I got it working with the following modifications:

protected function _prepareColumns()
{
    $this->addColumn('tax_class_id', array(
        'header'    => Mage::helper('adminhtml')->__('Selected'),
        'type'      => 'radio',
        'html_name' => 'taxClassId',
        'align'     => 'center',
        'values'    => $this->_getSelectedTax(),
        'index'     => 'class_id'
    ));

    $this->addColumn('op_avatax_code', array(
        'header'    => Mage::helper('tax')->__('Avatax Code'),
        'align'     => 'left',
        'index'     => 'op_avatax_code'
    ));

    $this->addColumn('class_name', array(
            'header'    => Mage::helper('tax')->__('Class Name'),
            'align'     => 'left',
            'index'     => 'class_name'
    ));

    return parent::_prepareColumns();
}

protected function _addColumnFilterToCollection($column)
{
    // Set custom filter for selected tax-class
    if ($column->getId() == 'tax_class_id') {
        $taxClassIdArray = $this->_getSelectedTax();
        if (empty($taxClassIdArray)) {
            $taxClassIdArray = 0;
        }
        if ($column->getFilter()->getValue()) {
            $this->getCollection()->addFieldToFilter('class_id', array('in' => $taxClassIdArray));
        } else {
            if ($taxClassIdArray) {
                $this->getCollection()->addFieldToFilter('class_id', array('nin' => $taxClassIdArray));
            }
        }
    } else {
        parent::_addColumnFilterToCollection($column);
    }
    return $this;
}

protected function _getSelectedTax()
{
    $taxClassArray = array();
    $taxClassId = $this->getProduct()->getTaxClassId();

    $taxClassArray[0] = $taxClassId;
    return $taxClassArray;
}

protected function getProduct()
{
    $product = Mage::registry('product');
    if ($product == null) {
        $product = Mage::getModel('catalog/product')->load($this->getRequest()->getParam('id'));
    }
    return $product;
}

1 Answer 1

2

You should add values in following column

$this->addColumn('tax_class_id', array(
    'header'    => Mage::helper('adminhtml')->__('Selected'),
    'type'      => 'radio',
    'html_name' => 'taxClassId',
    'align'     => 'center',
    'values'            => $this->_getSelectedTax(),
));

Pass data to _getSelectedTax method.

Check app/code/core/Mage/Adminhtml/Block/Catalog/Product/Edit/Tab/Related.php as an example.

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.