[SOLVED] CSort.link & visualize sort direction

Hello,

I was wondering on how to visualize the sort direction using CSort.link?

One solution would be to use CSort.directions and check for each column if that column is currently used for sorting and add css class if so.

A quick prototype(nice way would be to extend CSort):



function sort_link($sort, $col){


  echo '<div class="'.(isset($sort->directions[$col]) ? ($sort->directions[$col] ? 'asc' : 'desc') : '').'">';


  echo $sort->link($col);


  echo '</div>';


}


So, is there a proper way to do this?

This is the way to go. You could also extend CSort and override its createLink() method.

thanks  ;)

Just to keep this thread complete, here is my implementation of extended CSort class:




class ExtSort extends CSort

{

    public function createLink($attribute, $label, $url, $htmlOptions)

    {

        if (is_array($this->attributes) && !array_key_exists($attribute, $this->attributes)) {

            return parent::createLink($attribute, $label, $url, 

                array_merge(array('class'=>'sortnolink'), $htmlOptions)

            );

        }


        $directions = $this->directions;

        $linkClass = isset($directions[$attribute]) 

                     ? ($directions[$attribute] ? 'sortdesc' : 'sortasc') 

                     : 'sort';

                     

        if (empty($directions)) {

            if ($this->defaultOrder == $attribute || $this->defaultOrder == $attribute." asc") {

                $linkClass = "sortasc";    

            } else if ($this->defaultOrder == $attribute." desc") {

                $linkClass = "sortdesc";

            }

        }              

                     

        return parent::createLink($attribute, $label, $url, 

            array_merge(array('class'=>$linkClass), $htmlOptions)

        );

    }


}


?>



This class adds “nolink” class to links not present in attributes (see <a href=‘http://www.yiiframework.com/forum/index.php/topic,1679.msg9439.html#msg9439’ class=‘bbc_url’ title=‘External link’ rel=‘nofollow’>http://www.yiiframew…39.html#msg9439</a> ).

Classes "sortdesc" and "sortasc" are added for columns with sorting and "sort" class to other links.

Also it supports “defaultOrder” CSort’s property.