1

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?

2 Answers 2

1

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 the sales order model and I used the "key" param from the 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 the 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 a new column with your column name in the index
    $this->addColumn('test', array(
        'header'=> Mage::helper('sales')->__('Test'),
        'type'  => 'text',
        'index' => 'test',
    ));

Result is here

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

0

Please refer this one.

1) Adding a column to Magento orders grid - alternative way using layout handles

2) In your grid may be you need renderer.

Add custom renderer for a custom column in Magento grid

This is going to help you.

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.