<?php
/**
* 为CSort类增加setAttributes方法,简化表关联查询的排序代码
*
* <code>
* $sort = ToySort('modelName');
* $sort->setAttributes(array(
* 'id' => array('asc'=>'t.id', 'desc'=>'t.id DESC'),
* 'taxis' => array('asc'=>'t.taxis', 'desc'=>'t.taxis DESC'),
* ));
* $sort->setAttributes('menu_id',
* array('asc'=>'t.menu_id', 'desc'=>'t.menu_id DESC')
* );
* $sort->defaultOrder = 't.menu_id, t.taxis DESC, t.id';
* $sort->applyOrder($criteria);
* </code>
*
* @author 毛松<wfmaosong[AT]gmail.com>
*/
class ToySort extends CSort {
/**
* 批量设置attributes
*
* @param string|array $attributes e.g. 'taxis' or array('taxis' => array('asc'=>'t.taxis', 'desc'=>'t.taxis DESC'))
* @param mixed $value e.g. array('asc'=>'t.taxis', 'desc'=>'t.taxis DESC')
*/
public function setAttributes($attributes, $value = null) {
if($this->modelClass !== null && $this->attributes === array())
$this->attributes = CActiveRecord::model($this->modelClass)->attributeNames();
if (!is_array($attributes)) {
$attributes = array($attributes => $value);
}
$new = array();
foreach($attributes as $key => $value) {
unset($this->attributes[$key]);
$new[$key] = $value;
}
$this->attributes = array_merge($new, $this->attributes);
}
/**
* Generates a hyperlink that can be clicked to cause sorting.
* 增加第三次点击链接时取消排序
*
* @param string the attribute name. This must be the actual attribute name, not alias.
* If it is an attribute of a related AR object, the name should be prefixed with
* the relation name (e.g. 'author.name', where 'author' is the relation name).
* @param string the link label. If null, the label will be determined according
* to the attribute (see {@link resolveLabel}).
* @param array additional HTML attributes for the hyperlink tag
* @return string the generated hyperlink
*/
public function link($attribute,$label=null,$htmlOptions=array()) {
if($label===null)
$label=$this->resolveLabel($attribute);
if($this->resolveAttribute($attribute)===false)
return $label;
$directions=$this->getDirections();
$noSort = false; //第三次点击时取消字段排序
if(isset($directions[$attribute])) {
$class=$directions[$attribute] ? 'desc' : 'asc';
if(isset($htmlOptions['class']))
$htmlOptions['class'].=' '.$class;
else
$htmlOptions['class']=$class;
$descending=!$directions[$attribute];
if ($directions[$attribute]) {
$noSort = true;
}
unset($directions[$attribute]);
}
else
$descending=false;
if (!$noSort) {
if($this->multiSort)
$directions=array_merge(array($attribute=>$descending),$directions);
else
$directions=array($attribute=>$descending);
}
$url=$this->createUrl(Yii::app()->getController(),$directions);
return $this->createLink($attribute,$label,$url,$htmlOptions);
}
}
The second functional:
Page init:
552
[](https://www.forum.yiiframework.com/ipb_uploads/monthly_02_2010/post-3922-12670956166877.png)
First click:
553
[](https://www.forum.yiiframework.com/ipb_uploads/monthly_02_2010/post-3922-12670956361867.png)
Second click:
554
[](https://www.forum.yiiframework.com/ipb_uploads/monthly_02_2010/post-3922-12670956970715.png)
Third click:
555
[](https://www.forum.yiiframework.com/ipb_uploads/monthly_02_2010/post-3922-12670957148797.png)