sushantsahay
sushantsahay

Reputation: 361

Magento grid filter index for custom column

In Magento Grid

a) While preparing collection I did some calculations in query and got those values as extra column-

ex - select 1 as extracolumn

Now how can I set filter_index and sorting on this column

OR

b) I am using rendering to show custom data in a column, How can I set filter_index and sorting on this column

Upvotes: 3

Views: 8927

Answers (3)

John Cleary
John Cleary

Reputation: 21

I tried the suggestion from shadowice222 but it didn't allow me to filter on the field. Looked at the core code and it complains about the 'my_column' not being defined. Using the addExpressionAttributeToSelect does the same as thing internally but also adds the column.

The empty array is to bypass the code which does the variable replacement as that would try to cast the Zend_Db_Expr to a string. Perhaps a better approach would be to extend the class to have an addZendDbExptToSelect method.

protected function _prepareCollection()
{
    ...
    $collection->addExpressionAttributeToSelect('my_column', new Zend_Db_Expr("(some expression)"), array());
    ....
}

Upvotes: 2

Moshe Brodsky
Moshe Brodsky

Reputation: 315

protected function _getFlatExpressionColumn($key) {
    switch ($key) {
        case 'my_column':
            $result = new Zend_Db_Expr("(some expression)");
            break;
    }
}

protected function _prepareCollection()
{
    ...
    $col->getSelect()
        ->columns(array(
            'my_column' => $this->_getFlatExpressionColumn('my_column'),
        ))
    ;
    ...
}

protected function _prepareColumns()
{
    ...
    $this->addColumn('my_column', array(
        'header'        => $hlp->__('My Column Title'),
        'index'         => 'my_column',
        'filter_index'  => $this->_getFlatExpressionColumn('my_column'),
    ));
    ...
}

Upvotes: 1

Irsale-Net
Irsale-Net

Reputation: 1

show how can work this function :

Mage_Adminhtml_Block_Widget_Grid::_addColumnFilterToCollection

Is here you can make your filter for the renderers data

Upvotes: 0

Related Questions