ListView sort order

Hello All,

Been reading about the various hacks to get the list view of the Cases module to sort descending using a specific field. Does anyone have a sec to help me with the proper way to do this? Also seems that these changes are not upgrade safe. Here are a few of the posts I have been reading.

https://community.sugarcrm.com/sugarcrm/topics/how_to_change_default_sort_field_in_list_view
http://urdhva-tech.blogspot.com/2013/02/extending-list-view-query.html

You can do this per-user by using the sort order in the advanced search, but I presume that is not a solution to this change.

When sort is set at the user level, is it persistent for that user?

NO… when they click “clear” then “search” it goes back.

I’ve been fighting this for over a year, and haven’t been able to resolve it.

My most recent attempt is a file is /custom/modules/Project/views called custom.view.list.php and the following code:


<?php
// Attempt at setting the default sort order for Projects - ADS 10/2/15
require_once('modules/Projects/views/view.list.php');
class CustomProjectsViewList extends ProjectsViewList
{
        function CustomProjectsViewList(){
          parent::ProjectsViewList();
        }
       function listViewPrepare() {    
          if(empty($_REQUEST['orderBy'])) { 
 
              $_REQUEST['orderBy'] = strtoupper('aa_file_c');            
              $_REQUEST['sortOrder'] = 'DESC'; 
           } 
           parent::listViewPrepare(); 
       }
}
?>
1 Like

Hello.

I’m after something similar.

My users want to have a sort in the account view. They always want them to be sorted by the account numbers going down irrespective of what options they have chosen to search by. So I was wondering if I can try this… Can I just change the aa_file_c to the name of the field I want to sort by in accounts and of course change any projects references to Accounts.

Thanks Sieberta that worked a treat for me in accounts.

They now sort by the field I want, and in the correct order.

So FYI, that code is almost correct except for a couple of glitches:

  1. The “Projects” module is “Project” not “Projects” despite every other module name being plural, so that needs corrected. It should also be named view.list.php and put in /custom/modules/project/views.
  2. I don’t know where it is coming from, but my $_REQUEST[‘orderBy’] is never empty, so that code never fires (drives me crazy because I don’t know why it isn’t empty). For me, it is a column that you would NEVER want to sort by (a bool) so I just edited the code to also include that if it is that value, also repalce it with the desired sort field.

Otherwise, this code was pretty close. Took me nearly 2 years to finally get it working, I guess it wasn’t that big of a priority for me.

sieberta

In each module Listview.
Click the column header that you like to be sorted on, once or twice (up/down sort) and then click Search. This makes the current sorting get saved. Just like a normal text search also can be saved with Search until it is Cleared.

If you want to respect user preferences then use the following code to apply default sorting order:

File location: custom/modules/<module_name>/views/view.list.php

public function preDisplay()
{
   $userPreferenceOrder = $GLOBALS['current_user']->getPreference('listviewOrder', 'AOK_KnowledgeBase2_AOK_KNOWLEDGEBASE'); // you can get required category name from "user_preferences" table. 

       if(empty($userPreferenceOrder['orderBy']))
        {
	      $_REQUEST['orderBy'] = 'field_name';
	      $_REQUEST['sortOrder'] = 'DESC';
         }

         parent::preDisplay();
    }