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

Overriding the direction parameter #187

Open
khamyl opened this issue Jan 16, 2022 · 0 comments
Open

Overriding the direction parameter #187

khamyl opened this issue Jan 16, 2022 · 0 comments

Comments

@khamyl
Copy link

khamyl commented Jan 16, 2022

Is there a possibility to override the direction parameter? I needed to have two different buttons for ASC and DESC so the toggle did not work for me. I could not find the workaround, so I suggest the following edit to the code to support this by using the queryParameters['direction']:

Edits

determineDirection() function

  • Added third $queryParameters parameter
  • Added override condition which uses the direction from query parameters if $queryParameters['direction'] exists :
    private static function determineDirection($sortColumn, $sortParameter, $queryParameters)
    {
        $icon = self::selectIcon($sortColumn);

        $override_direction = array_key_exists('direction', $queryParameters) && in_array($queryParameters['direction'], ['asc', 'desc']);

        if (request()->get('sort') == $sortParameter && in_array(request()->get('direction'), ['asc', 'desc']) || $override_direction) {
            $icon .= (request()->get('direction') === 'asc' ? config('columnsortable.asc_suffix', '-asc') : config('columnsortable.desc_suffix', '-desc'));

            if($override_direction){ 
                //Override the direction with the query parameter
                $direction = $queryParameters['direction'];
            }else{
                $direction = request()->get('direction') === 'desc' ? 'asc' : 'desc';
            }    

            return [$icon, $direction];
        } else {
            $icon      = config('columnsortable.sortable_icon');
            $direction = config('columnsortable.default_direction_unsorted', 'asc');

            return [$icon, $direction];
        }
    }

render() function

  • Calling self::determineDirection() function with third $queryParameters parameter.
    public static function render(array $parameters)
    {
        list($sortColumn, $sortParameter, $title, $queryParameters, $anchorAttributes) = self::parseParameters($parameters);

        $title = self::applyFormatting($title, $sortColumn);

        if ($mergeTitleAs = config('columnsortable.inject_title_as', null)) {
            request()->merge([$mergeTitleAs => $title]);
        }

        list($icon, $direction) = self::determineDirection($sortColumn, $sortParameter, $queryParameters);

        $trailingTag = self::formTrailingTag($icon);

        $anchorClass = self::getAnchorClass($sortParameter, $anchorAttributes);

        $anchorAttributesString = self::buildAnchorAttributesString($anchorAttributes);

        $queryString = self::buildQueryString($queryParameters, $sortParameter, $direction);

        $url = self::buildUrl($queryString, $anchorAttributes);

        return '<a'.$anchorClass.' href="'.$url.'"'.$anchorAttributesString.'>'.e($title).$trailingTag;
    }

Usage

View

<ul class="dropdown-menu pull-right">
    <li>@sortablelink('tag', 'Alphabetically [A-Z]', ['direction'=>'asc'])</li>
    <li>@sortablelink('tag', 'Reverse Alphabetically [Z-A]', ['direction'=>'desc'])</li>
    <li><a href="#">TBD: Most documents</a></li>
    <li><a href="#">TBD: Fewest documents</a></li>
    <li class="divider"></li>
    <li><a href="/tags">Reset (by Time Created)</a></li>
</ul>

Added pull request: #188

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

1 participant