/** * 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 CActiveRecord::getAttributeLabel}). * @param array additional HTML attributes for the hyperlink tag * @return string the generated hyperlink */ public function link($attribute, $label = null, $htmlOptions = array()) { $directions = $this->getDirections(); if (isset($directions[$attribute])) { $direction = $directions[$attribute] == 'asc' ? 'desc' : 'asc'; unset($directions[$attribute]); } else { $direction = 'asc'; } if ($this->multiSort) { $directions = array_merge(array($attribute => $direction), $directions); } else { $directions = array($attribute => $direction); } if ($label === null) { $label = $attribute; } $url = $this->createUrl(Yii::app()->getController(), $directions); if ($this->postVars) { if (self::$generateJs) { $data = CJSON::encode($this->postVars); $script = ' function setSort(_field, _direction) { var data = ' . $data . '; data.' . $this->sortVar . ' = _field + "." + _direction; ' . (Yii::app()->getRequest()->getParam('page') ? 'data.page = ' . Yii::app()->getRequest()->getParam('page') : '') . ' ' . (Yii::app()->getRequest()->getParam('pageSize') ? 'data.pageSize = ' . Yii::app()->getRequest()->getParam('pageSize') : '') . ' $.post("' . Yii::app()->createUrl($this->route) . '", data, function(responseText) { $("div.ui-layout-center").html(responseText); init(); }); } '; Yii::app()->getClientScript()->registerScript('Sort_setSort', $script); self::$generateJs = false; } return CHtml::link($label, 'javascript:void(0)', array('onclick' => 'setSort("' . $attribute . '", "' . $direction . '");')); } else { return $this->createLink($attribute, $label, $url, $htmlOptions); } }