2

I want to sort custom rendere column in magento grid.

See below image--

enter image description here

As in above image Sorting is done using Status column, but it is is showing wrong. I want to sort it like active,on hold,out of stock.

Here is the code in Grid.php--

$this->addColumn('stock_status', array(
            'header' => Mage::helper('allstock')->__('Status'),
            'width' => '200px',
            'align' => 'center',
            'index' => 'stock_status',
            'renderer' => 'allstock/adminhtml_allstock_renderer_stockstatus'
        ));

here is the renderer code-

public function render(Varien_Object $row)
    {
        $stock_status=$row->getStockStatus();
        if($stock_status=='stock on hold')
        {
            $html="on hold";
        }
        else
        {
            $html=$stock_status;
        }
        return $html;
    }
4

Finally I got my answer after working on it a lot, Here is How I made it possible-

/* overwrite to sort stock_status column in grid*/
    protected function _setCollectionOrder($column)
    {
        $collection = $this->getCollection();
        if ($collection) {
             switch ($column->getId()) { 
                  case 'stock_status':
                    $arr = array();
                    $i=0;
                    foreach($collection as $item)  
                    { 
                        if($item->getStockStatus()=="stock on hold")  // check value of column
                        {
                            $item->setStockStatus("on hold");        // replace value of column
                        }   

                        $arr[$i] = $item;   
                        $i++    ;        
                    }

                    $collectionItems = $collection->getItems();
                    usort($collectionItems, array($this, '_sortItems'));  // re-sort collection
                    $newCollection = new Varien_Data_Collection();        // create new collection

                    foreach ($collectionItems as $item) {
                        $newCollection->addItem($item);                // assign each item to new collection
                    }
                    $this->setCollection($newCollection);

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

    /* custom function to sort stock_status column in grid*/
    public function _sortItems($a, $b)
    {
        $columnId = "stock_status";
        $dir      = $this->getParam($this->getVarNameDir(), $this->_defaultDir);

        $al = strtolower($a->getData($columnId));
        $bl = strtolower($b->getData($columnId));

        if ($al == $bl) {
            return 0;
        }

        if ($dir == 'asc') {
            return ($al < $bl) ? -1 : 1;
        } else {
            return ($al > $bl) ? -1 : 1;
        }
    } 

Following links helped me alot--

https://stackoverflow.com/questions/5673093/sort-magento-collection-after-load https://stackoverflow.com/questions/7705007/magento-grid-sorting-on-rendered-column

0

As it looks that content of your wh1 column is generated in renderer, built-in sorting based on sortable grid config value won't work.

Try to echo you collection select query somewhere in _prepareCollection() method to see how produced SQL query looks:

echo $this->getCollection()->getSelect()

Try to implement sorting in _afterLoadCollection() method after your data is fetched from database. Please find example in answer for following question:

https://stackoverflow.com/questions/7705007/magento-grid-sorting-on-rendered-column

Column isn't Sorting in Custom Admin Report

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.