Overriding the direction parameter
khamyl opened this issue · 0 comments
khamyl commented
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