Yii Framework Forum: <?php class ToySort extends CSort {} ?> - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

<?php class ToySort extends CSort {} ?> Rate Topic: -----

#1 User is offline   Cwan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 09-February 10

Posted 23 February 2010 - 12:18 PM

<?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);
    }

}


0

#2 User is offline   Onman 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 175
  • Joined: 26-December 09
  • Location:The Netherlands

Posted 23 February 2010 - 02:22 PM

is there any question to this?
0

#3 User is offline   Cwan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 09-February 10

Posted 24 February 2010 - 08:02 AM

View PostOnman, on 23 February 2010 - 02:22 PM, said:

is there any question to this?


yes
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 24 February 2010 - 08:07 AM

View PostCwan, on 24 February 2010 - 08:02 AM, said:

yes


Great! What is it? :)
0

#5 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 751
  • Joined: 02-February 09

Posted 24 February 2010 - 08:08 AM

View PostCwan, on 24 February 2010 - 08:02 AM, said:

yes



and where is it?
0

#6 User is offline   Cwan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 09-February 10

Posted 25 February 2010 - 06:05 AM

Sorry, my English not good.

The first functional:

$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);


The second functional:

Page init:
[attachment=552:1.png]
First click:
[attachment=553:2.png]
Second click:
[attachment=554:3.png]
Third click:
[attachment=555:4.png]

Attached File(s)

  • Attached File  1.png (1.28K)
    Number of downloads: 10
  • Attached File  2.png (1.31K)
    Number of downloads: 9
  • Attached File  3.png (1.29K)
    Number of downloads: 8
  • Attached File  4.png (1.24K)
    Number of downloads: 8

0

#7 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 751
  • Joined: 02-February 09

Posted 25 February 2010 - 07:11 AM

$sort = new ToySort;
may be ???
0

#8 User is offline   Cwan 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 9
  • Joined: 09-February 10

Posted 25 February 2010 - 11:14 AM

View Postbettor, on 25 February 2010 - 07:11 AM, said:

$sort = new ToySort;
may be ???

:lol: thanks
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users