binc  
          
              
                August 25, 2012, 10:15am
               
              1 
           
         
        
          I encountered a nice functionality in some grid based interfaces, i.e. the possibility to sub-sort on a column by [Ctrl]-clicking a column title.
So if my gridview -which is displaying names and addresses- is sorted on City, I could [Ctrl]-click on the Name column to have it sorted primarily on City and within that on Name.
Did anybody solve this puzzle within Yii/Cgridview? I searched but didn’t find.
         
        
           
         
            
       
      
        
          
          
            binc  
          
              
                October 20, 2012,  7:50pm
               
              2 
           
         
        
          Is nobody interested? Or is it too obvious?
 Binc:
 
I encountered a nice functionality in some grid based interfaces, i.e. the possibility to sub-sort on a column by [Ctrl]-clicking a column title.
So if my gridview -which is displaying names and addresses- is sorted on City, I could [Ctrl]-click on the Name column to have it sorted primarily on City and within that on Name.
Did anybody solve this puzzle within Yii/Cgridview? I searched but didn’t find.
 
 
         
        
           
         
            
       
      
        
          
          
            konapaz  
          
              
                October 20, 2012,  9:07pm
               
              3 
           
         
        
          
I think the only way is to extend the zii/widgets/grid/CGridView.php class and write your own functions for multiply sort.
         
        
           
         
            
       
      
        
          
          
            binc  
          
              
                October 20, 2012,  9:35pm
               
              4 
           
         
        
          
I was afraid that would be the case. I’ll post it here if I manage.
         
        
           
         
            
       
      
        
          
          
            binc  
          
              
                October 21, 2012, 11:42pm
               
              5 
           
         
        
          Well it was too obvious alltogether!!
Just add multiSort=true to your dataprovider, like so:
return new CActiveDataProvider($this, array(
    'criteria' => $criteria,
    'sort'=>array(
	'defaultOrder'=>'name ASC',
	'multiSort'=>true,
    ),
));
It provides a basic multisort on the Gridview right away. Takes a little getting used to, but hey! just one line of code.
(It took me a few hours to find it in the docs, though.)
         
        
           
         
            
       
      
        
          
          
            zaccaria  
          
              
                October 22, 2012,  2:28pm
               
              7 
           
         
        
          The multisort is nice, but has a slighly different logic:
if you click on name and then on surname, you will get the order by surname and the name.
That’s the opposite than you said.
The trick of ctrl+click can be achived with a simple js hack, you have to read the url on the button clicked and switch the order of the sort (more precisely, to put the first item at the bottom).
         
        
           
         
            
       
      
        
          
          
            binc  
          
              
                October 22, 2012,  3:38pm
               
              8 
           
         
        
          
 zaccaria:
 
The multisort is nice, but has a slighly different logic:
if you click on name and then on surname, you will get the order by surname and the name.
That’s the opposite than you said.
The trick of ctrl+click can be achived with a simple js hack, you have to read the url on the button clicked and switch the order of the sort (more precisely, to put the first item at the bottom).
 
 
Thanks for the tip Zaccaria.
I don’t care too much about the Ctrl, so I hacked  extended CSort.
There’s a line in the link function in CSort.php saying
$directions=array_merge(array($attribute=>$descending),$directions);
I changed that to
$directions=array_merge($directions, array($attribute=>$descending));
This way it feels a bit more intuitive.
         
        
           
         
            
       
      
        
          
          
            dhavald99  
          
              
                June 24, 2014,  6:30am
               
              9 
           
         
        
          Can any one know how to display sorting numbers so I know that sorting order!
Thanks in advance
         
        
           
         
            
       
      
        
          
          
            binc  
          
              
                June 3, 2018,  7:11pm
               
              10 
           
         
        
          Almost six years later…
If you are looking for this functionality in Yii2, it is here!
$dataProvider->sort->enableMultiSort=true;