This issue you can solve by customizing the renderer file. You can fetch the format from database by custom query as well.
For eg.
Prodid.php
<?php
class Mage_Adminhtml_Block_Sales_Order_Renderer_Prodid extendsMage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row){
$url = $this->getUrl('*/*/edit', array(
'store'=>$this->getRequest()->getParam('store'),
'id'=>$row->getId())
);
$exploding = explode('sales_order/edit/id/',$url);
//print_r($exploding);
$id = explode('/',$exploding[1]);
//print_r($id);
$myId = $id[0];
//echo $myId;
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
// now $write is an instance of Zend_Db_Adapter_Abstract
$readresult=$write->query("SELECT product_id FROM `sales_flat_order_item` WHERE `order_id` =$myId ");
while ($row = $readresult->fetch() ) {
$productid = $row['product_id'];
$myprodid .= $productid.',';
}
$myprodid2 = substr($myprodid,0,-1);
return $myprodid2;
}
}
?>
This will give you the desired result in my case it was product id
Then calling it in the grid file would be like
$this->addColumn('ids', array(
'header' => Mage::helper('sales')->__('Product ID'),
'width' => '100px',
'index' => 'ids',
'type' => 'text',
'filter' => false,
'sortable' => false,
'renderer' => 'Mage_Adminhtml_Block_Sales_Order_Renderer_Prodid'
));
The filter=false and sortable false will remove the a href from the headers and will only show you the data.
If you want to make them sortable, then you will need to pass the corresponding unique index id.
Please share if anything is unclear here.
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
Best Answer
A basic example to point you in the right direction:
The render function is returning the final html for the column in question so any string you return from this function is valid. And if you need to pass your column object to the newly created block, just add
$block->setColumn($this)
before calling the ->toHtml() method and the column object will be available to you in your new block.