cakephp-plugin-boost_cake icon indicating copy to clipboard operation
cakephp-plugin-boost_cake copied to clipboard

Added Sort Icon in paginator helper

Open sujithtomy opened this issue 11 years ago • 4 comments

Added sort function to show sort icons in pagination header links Please review my code, if use full and according to standards please add this to BoostCake :)

public function sort($key, $title = null, $options = array()){               

        $options = array_merge(array('url' => array(), 'model' => null), $options);
        $url = $options['url'];
        unset($options['url']);

        if (empty($title)) {
            $title = $key;

            if (strpos($title, '.') !== false) {
                $title = str_replace('.', ' ', $title);
            }

            $title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
        }
        $dir = isset($options['direction']) ? $options['direction'] : 'asc';
        unset($options['direction']);

        $sortKey = $this->sortKey($options['model']);
        $defaultModel = $this->defaultModel();
        $isSorted = (
            $sortKey === $key ||
            $sortKey === $defaultModel . '.' . $key ||
            $key === $defaultModel . '.' . $sortKey
        );

        //$dir_icon = '<i class="icon-sort muted pull-right ">';
        $dir_icon = '';
        if ($isSorted) {
            $dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
            $class = $dir === 'asc' ? 'desc' : 'asc';
            if (!empty($options['class'])) {
                $options['class'] .= ' ' . $class;
            } else {
                $options['class'] = $class;
            }
            // Set Icon for Link 
            switch($dir){
                case 'asc':
                    $dir_icon = ' <i class="icon-chevron-up text-info pull-right "></i>';
                    break;
                case 'desc':
                    $dir_icon  = ' <i class="icon-chevron-down text-info pull-right"></i>';
                    break;
            }
        }
        if (is_array($title) && array_key_exists($dir, $title)) {
            $title = $title[$dir];
        }
        $url = array_merge(array('sort' => $key, 'direction' => $dir), $url, array('order' => null));
        return parent::link($title, $url, $options) . $dir_icon;        
    }

sujithtomy avatar Nov 13 '13 03:11 sujithtomy

Writing this way if I

echo $this->Paginator->sort(
    'name', 
    array(
        'asc' => __('Name') . ' <i class="icon-chevron-up text-info pull-right"></i>',
        'desc' => __('Name') . ' <i class="icon-chevron-down text-info pull-right"></i>'
    ),
    array(
        'escape' => false
   )
);

slywalker avatar Nov 14 '13 07:11 slywalker

Wow!! great, I wasn't aware its can be done in params.

On Thu, Nov 14, 2013 at 3:13 PM, Yasuo Harada [email protected]:

Writing this way if I

echo $this->Paginator->sort( 'name', array( 'asc' => __('Name') . ' ', 'desc' => __('Name') . ' ' ), array( 'escape' => false ));

— Reply to this email directly or view it on GitHubhttps://github.com/slywalker/cakephp-plugin-boost_cake/issues/39#issuecomment-28463852 .

sujithtomy avatar Nov 14 '13 08:11 sujithtomy

Is there a way to do this with multiple pagination headings (such as in a table, using column headings), whereby only the Paginator->sort that is active has an icon?

Also, for anyone using standard bootstrap 3 glyphicons:

<i class="glyphicon glyphicon-chevron-down text-info pull-right">

stantond avatar Jan 22 '14 00:01 stantond

Maybe like this:

$sKey = 'vsnr'; $sName = 'Vers. Nr.';
if(isset($this->request->params['paging']['Order']['options']['sort']) && $this->request->params['paging']['Order']['options']['sort'] == $sKey){
    echo $this->Paginator->sort(
                    $sKey,
                    array(
                        'asc' => $sName . ' <i class="fa fa-chevron-up text-info"></i>',
                        'desc' => $sName . ' <i class="fa fa-chevron-down text-info"></i>'
                    ),
                    array(
                        'escape' => false,
                    )
                );
            }else{
                echo $this->Paginator->sort('vsnr', 'Vers. Nr.');
            }
            unset($sKey); unset($sName);

Order = Modelname

Sorry for the bad format and english... Greetz

mschoenbein avatar Oct 29 '14 16:10 mschoenbein