Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Sort Icon in paginator helper #39

Open
sujithtomy opened this issue Nov 13, 2013 · 4 comments
Open

Added Sort Icon in paginator helper #39

sujithtomy opened this issue Nov 13, 2013 · 4 comments

Comments

@sujithtomy
Copy link

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;        
    }
@slywalker
Copy link
Owner

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

@sujithtomy
Copy link
Author

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//issues/39#issuecomment-28463852
.

@stantond
Copy link

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">

@mschoenbein
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants