Welcome, Guest
Username: Password: Remember me

TOPIC: Setting default sort order

Setting default sort order 30 Jan 2012 15:39 #1131

  • giori
  • giori's Avatar
  • Offline
  • Senior Member
  • Posts: 73
  • Thank you received: 14
  • Karma: 6
Users are asking me to have tables fixed sorted by newest date. So the date field does not need to be sortable, but simply sorted by newest date.

I see that we can make a column either fixed or sortable, but we cannot define a default sort order!

I tried to fiddle around in the code in the models folder and in the views, but without success. In the views I can set the field header fixed, but in the models files I do not know which functions need to be changed to add an "Order by a.date asc/desc" clause.
Last Edit: 13 Feb 2012 14:18 by admin.
The administrator has disabled public write access.

Re: Setting default sort order 30 Jan 2012 16:50 #1133

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
Interested in the answer too ;-)
The administrator has disabled public write access.

Re: Setting default sort order 31 Jan 2012 00:12 #1137

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
After a bit of search, here it is (i think :whistle:)

First, you have to set a field that will be ordered by default. To do this, in the Builder, go into your Collection View, click just right of the Grid section (on the icon to get the propreties) and select a field in GROUP BY. Do the same for both collection views (backend and frontend).

Then

In Administrator/models/NAME_OF_YOUR_MODEL.php, search for
function _buildQueryOrderBy()
	{
		$order = array();
		$pre_order = 'a.field_to_order';

		return parent::_buildQueryOrderBy($order, $pre_order);
	}

and change this with :
function _buildQueryOrderBy()
	{
		$order = array();
		$pre_order = 'a.field_to_order DESC';

		return parent::_buildQueryOrderBy($order, $pre_order);
	}
Last Edit: 12 Feb 2012 13:55 by admin.
The administrator has disabled public write access.
The following user(s) said Thank You: admin, giori

Re: Setting default sort order 31 Jan 2012 06:53 #1140

  • mossss
  • mossss's Avatar
  • Offline
  • Premium Member
  • Posts: 100
  • Thank you received: 7
  • Karma: 9
I was doing it another way, but I see this is better. Thanks
Last Edit: 01 Feb 2012 05:05 by mossss.
The administrator has disabled public write access.

Re: Setting default sort order 11 Feb 2012 10:29 #1234

  • giori
  • giori's Avatar
  • Offline
  • Senior Member
  • Posts: 73
  • Thank you received: 14
  • Karma: 6
VeCrea,

thank you for the solution. Obviously I checked the wrong function.

Jocelyn,

Is it possible to add the "Default sort order: Asc/Desc" next to the field attribute "Sortable Yes/No"?
Shouldn't it be an a easy change? Thank you!
The administrator has disabled public write access.

Re: Setting default sort order 11 Feb 2012 11:40 #1235

  • VeCrea
  • VeCrea's Avatar
  • Offline
  • Platinum Member
  • Absolute JCook fan
  • Posts: 473
  • Thank you received: 100
  • Karma: 30
One other sorting question that i couldn't solve is :
how can you sort "DESC" the values of a combo box listing values from a FK ?
Giori, happy i could help
The administrator has disabled public write access.

Re: Setting default sort order 12 Feb 2012 14:03 #1245

  • admin
  • admin's Avatar
  • Offline
  • Administrator
  • Chef
  • Posts: 3711
  • Thank you received: 987
  • Karma: 140
Values from a FK comes from the model as exactly the same way than a list.

In the view.php.html file, find the declaration of this list (edit input or filter, or else...)

Exemple : here a FK combo initialization in $lists:
$model_attribut =& JModel::getInstance('attributs', 'Demo120Model');
$model_attribut->addOrder('a.title');   //ADD THIS
$lists['fk']['attribut'] = $model_attribut->getData();


You can also create a PREDEFINED reusable query in your model (collection)
function _buildQuery_my_predefined_query()
{
	$this->addOrder('a.title');    //POSSIBLE HERE

	$query = ' SELECT a.*'
	
		. $this->_buildQuerySelect()
		. $this->_buildQueryJoin() . ' '
		. $this->_buildQueryWhere()

		. $this->_buildQueryOrderBy()
		. $this->_buildQueryExtra()
	;

	return $query;
}


and call it in the VIEW file :
$model_attribut =& JModel::getInstance('attributs', 'Demo120Model');
$model_attribut->active('predefined', 'my_predefined_query');   //CALL IT HERE
$lists['fk']['attribut'] = $model_attribut->getData();
Coding is now a piece of cake
Last Edit: 12 Feb 2012 14:10 by admin.
The administrator has disabled public write access.
Time to create page: 0.131 seconds

Get Started