2

I have created a grid in Magento and there is a column which is not coming from database instead I have computed it's value from other columns using renderer. lets say custom column in grid is total = columnA from DB - columnB from DB, have to sort the grid based on custom column. I overrided setCollectionToOrder function in my grid., sorted the collection received from prepareCollection function and put the sorted collection in new collection object but I then my grid doesnt show any row , though I can echo the sorted collection and it works fine but no rows come in grid.

   protected function _setCollectionOrder($column)

    {
        $collection = $this->getCollection();
        if ($collection) {
             switch ($column->getId()) {
                  case 'total':

                    $arr = array();
                    foreach($collection as $item)  { 
                        $colA= $item->getcolumnA();
                        $colB= $item->getcolumnB()


                        $total=  $colA- $colB

                        $item->setTotal($total);            
                        $arr[$i] = $item;   $i++    ;        
                    }


                        if($column->getDir()=='asc') {
                            $sorted = usort($arr, array('Grid_Class', '_cmpAscTotal'));
                        } else {
                            $sorted = usort($arr, array('Grid_Class', '_cmpDescTotal'));                
                        }
                        $collection = $this->_tempCollection(); // A blank collection 

                        for($i=0;$i<count($arr);$i++)   {   
                            $arr[$i]->setTotal(1);  
                            $collection->addItem($arr[$i]);                         
                        }   
                        $this->setCollection($collection);  


                    break;
                default:
                    parent::_setCollectionOrder($column);
                    break;
            }
        }
        return $this;
    }

tempCollection function just gives me a blank collection object (same what prepare collection function gives) _cmpAscTotal is callback function which defines my custom sorting.

protected function _prepareCollection()

    {
         $collection  = Mage::getModel('module/model')->getCollection();
$collection->getSelect()->joinLeft(array('table1' => 'table1'),
                                             'table1.sku = main_table.sku_id',
                                             Array('columnA, columnB, (1) as total')
                                            );
        $this->setCollection($collection);
        return parent::_prepareCollection();    
        }

Is there a better way of achieving sorted collection on a custom column, if not what I am doing wrong while modifying the collection that grid becomes empty

sushantsahay
  • 361
  • 2
  • 7
  • 15

2 Answers2

1

Actually, you should extend collection class and add your custom sorting in your collection _afterLoad method. If for some reason it isn't possible - you should do it in your grid _afterLoadCollection method. In any case, you can't/shouldn't/haven't do it in collection _setCollectionOrder method. Because if you look into Mage_Adminhtml_Block_Widget_Grid::_prepareCollection() code - you will see that _setCollectionOrder is called before collection load.

Updated:

protected function _afterLoadCollection() 
{
    foreach ($this->getCollection() as $item)  { 
        $item->setTotal($item->getcolumnA() - $item->getcolumnB());
    }
    usort($this->getCollection()->getIterator(), array('Grid_Class', '_cmpAscTotal'));
    return $this;
}
Dmytro Zavalkin
  • 5,265
  • 1
  • 30
  • 34
  • @ Zyava I have tried printing my custom collection object (which I am setting in setCollectionOrder function) in _afterLoadCollection, I am able to retrieve it properly in a sorted way, but still no rows are coming in grid.Problem here is I guess, If I modify the same collection object which I got from db, grid can understand it but If I create a blank collection object frm db and add items one by one, grid is not able to understand that. Any help will be appreciated!! – sushantsahay Oct 10 '11 at 06:36
  • Hmm, try sort without temp collection. – Dmytro Zavalkin Oct 10 '11 at 08:47
  • I am using Usort for sorting that does not call callback function If I input collection, tht is why I had to take all collection items in an object array, sort it using usort then add all new items in a new collection object one by one. Could you tell me some way to sort the collection directly? – sushantsahay Oct 10 '11 at 09:17
  • Still don't understand why you don't wish create o=your own collection class and do all data processing there. I updated my answer with piece of code, try something like that. – Dmytro Zavalkin Oct 10 '11 at 13:45
  • How can i get the total of rendered column – Milople Inc Aug 26 '15 at 14:20
0

Thanks Zyava I had to do it like -

    $arr = $this->getCollection()->getItems();
    if($dir=='asc') {
        $sorted = usort($arr, array('Grid_Class', '_cmpAscSuggestion'));
    } else {
        $sorted = usort($arr, array('Grid_Class', '_cmpDescSuggestion'));               
    }
    $this->getCollection()->setItems($arrt); // created a set item function in collection class for this module.

It sorts the items in collection.

sushantsahay
  • 361
  • 2
  • 7
  • 15