Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6700213
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:47:17+00:00 2026-05-26T06:47:17+00:00

I have created a grid in Magento and there is a column which is

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:47:17+00:00Added an answer on May 26, 2026 at 6:47 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to have a grid (created by program, not by xml) of equally
I have created a Silverlight User Control. The markup is: <StackPanel Grid.Row=4 Grid.Column=0 Orientation=Horizontal
Based on the answers from this question , I have created a grid from
I have created a simple grid which shows different images with an edit option.
I have created a custom dijit widget which contains a grid and some buttons.
I have created a custom class derived from Canvas, which contain elements which will
I have created a simple grid of divs by left floating them and an
I have created a few small flash widgets that stream .mp3 audio from an
I have created a grid view that displays six cells of content. In each
I have created a grid and want to access it when I click on

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.