Magento – Custom column in Magento 1.9 admin grid

collection;gridmagento-1.9

I am creating a custom grid in an admin module. This grid represents a table almost column for column, however, I want to add a completely custom column with a bit of logic to this column. Here's my (simplified) code…

protected function _prepareCollection() 
{
    $collection = Mage::getModel('me/mymod')
        ->getCollection();

    $this->setCollection($collection);

    // I need to perform some logic here to 'inject' into the _prepareColumns()
    $testString = Mage::app()->getRequest()->getParam('testString');

    return parent::_prepareCollection();
}

protected function _prepareColumns()
{
    $this->addColumn('username', array(
        'header' => Mage::helper('mymod')->__('Username'),
        'index' => 'username',
    ));

    // I want to get a value from 'prepareColumns() here
    $this->addColumn('test_string', array(
        'header' => Mage::helper('mymod')->__('Test String'),
        'index' => 'testString',
    ));

    return parent::_prepareColumns();
}

I know I can use a custom renderer here, but in my prepareColumns() method, I set a mode based on the request – so would need to pass that custom $testString through to the renderer instead – which seems even harder.

The easiest way I could thing was to just add a literal column to the collection call in prepareCollection() as asked here…

Add custom 'select' statement to Collection Magento 1.9

But I don't think it's possible.

I know I could put some custom logic in _prepareColumns() but it just seems messy – prepareCollection is obviously there to do exactly that.

Any ideas?

Best Answer

Unfortunately, I'm not able to get your target here. But I'll stick my 2 cents in hope that it will help you. For a faster answer, I didn't create the new entity and used sales order model and I used "key" param from request because of the same reason.

    $collection = Mage::getResourceModel($this->_getCollectionClass());

    //get the param here
    $key = Mage::app()->getRequest()->getParam('key');
    //set param to collection here
    $collection->getSelect()
        ->columns(
            array(
                new Zend_Db_Expr("\"{$key}\" as `key`")
            )
        );
    //set some logic to collection here
    $collection->getSelect()
        ->columns(
            array(
                new Zend_Db_Expr("IF (`grand_total` >= 500, \"{$key}\", \"test\") as `test`")
            )
        );
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

/**
 * @return $this
 * @throws Exception
 */
protected function _prepareColumns()
{

    $this->addColumn('real_order_id', array(
        'header'=> Mage::helper('sales')->__('Order #'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'increment_id',
    ));

    //add here new column with your column name in index
    $this->addColumn('key', array(
        'header'=> Mage::helper('sales')->__('Key'),
        'type'  => 'text',
        'index' => 'key',
    ));

    //add here new column with your column name in index
    $this->addColumn('test', array(
        'header'=> Mage::helper('sales')->__('Test'),
        'type'  => 'text',
        'index' => 'test',
    ));

Result is here

P.S. Please keep in mind that in spite of the fact that I edited core files in this example - I strongly don't recommend you to do this ever!

Related Topic