CSort Features

I have two feature request as I couldn't find the functionality.

When using the $sort->link('field_name'), the link sorts the field ascendingly by default (unless already). I'd like all links to default to descending (its a game scoreboard, everyone wants to see the top, not the bottom).

Also with $sort->link('field_name'), I'd like to be able to create an alias, so that my MySQL fields are not shown in the URL, especially when they are ugly ones. e.g. "sort/a_dmg_per_rnd_real.desc".

Thanks!  ;)

  • Twelve-60

See CSort::defaultOrder and CSort::attributes

Ah, not sure how I missed the aliasing feature, thanks!

I need to make all links created with $sort->link('field_name') default to /player/list/sort/field_name.desc instead of /player/list/sort/field_name

CSort::defaultOrder is only used when the list is not sorted at all.

Thanks!

  • Twelve-60

I think I'll have to extend and override:

	public function link($attribute,$label=null,$htmlOptions=array())


	{


		$directions=$this->getDirections();


		if(isset($directions[$attribute]))


		{


			$descending=!$directions[$attribute];


			unset($directions[$attribute]);


		}


		else


			$descending=false; // THIS LINE


		if($this->multiSort)


			$directions=array_merge(array($attribute=>$descending),$directions);


		else


			$directions=array($attribute=>$descending);





		if($label===null)


			$label=$this->resolveLabel($attribute);


		$url=$this->createUrl(Yii::app()->getController(),$directions);





		return $this->createLink($attribute,$label,$url,$htmlOptions);


	}

It defaults to false here, and I can’t see a way to change it :(

Edit: Just tried it then, works perferfectly changing $descending=true;. I think this should be a feature by default though :D

Edit again: This is the code I came up with, giving me control to edit the default when creating a link:

<?php





class CDescSort extends CSort


{


	public function link($attribute,$label=null,$htmlOptions=array(),$ascSort=false)


	{


		$directions=$this->getDirections();


		if(isset($directions[$attribute]))


		{


			$descending=!$directions[$attribute];


			unset($directions[$attribute]);


		}


		else


			$descending=($ascSort?false:true); // THIS LINE


		if($this->multiSort)


			$directions=array_merge(array($attribute=>$descending),$directions);


		else


			$directions=array($attribute=>$descending);


		


		if($label===null)


			$label=$this->resolveLabel($attribute);


		$url=$this->createUrl(Yii::app()->getController(),$directions);


		


		return $this->createLink($attribute,$label,$url,$htmlOptions);


	}


}
  • Twelve-60

I would really love to see this fixed in the next release. I have had some problems myself.