Skip to content

Commit

Permalink
improvement of default sorting, see #26
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyslik committed Apr 4, 2016
1 parent 8ae4b81 commit ef6f198
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
11 changes: 6 additions & 5 deletions src/ColumnSortable/Sortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ private function queryOrderBuilder($query, array $a)

$order = array_get($a, 'order', 'asc');
if (!in_array($order, ['asc', 'desc'])) {
$order = 'asc';
$order = Config::get('columnsortable.default_order', 'asc');
}

$sort = array_get($a, 'sort', null);
Expand Down Expand Up @@ -105,12 +105,12 @@ private function queryOrderBuilder($query, array $a)
*/
private function formatDefaultArray(array $a)
{
$order = 'asc';
$order = Config::get('columnsortable.default_order', 'asc');
reset($a);

if ((bool) count(array_filter(array_keys($a), 'is_string'))) {
$sort = key($a);
$order = array_get($a, $sort, 'asc');
$order = array_get($a, $sort, $order);
} else {
$sort = current($a);
}
Expand Down Expand Up @@ -157,15 +157,16 @@ public static function link(array $parameters) //Extending Blade; Blade sends ar
if (Input::get('sort') == $sortOriginal && in_array(Input::get('order'), ['asc', 'desc'])) {
$asc_suffix = Config::get('columnsortable.asc_suffix', '-asc');
$desc_suffix = Config::get('columnsortable.desc_suffix', '-desc');

$icon = $icon . (Input::get('order') === 'asc' ? $asc_suffix : $desc_suffix);
$order = Input::get('order') === 'desc' ? 'asc' : 'desc';
} else {
$icon = Config::get('columnsortable.sortable_icon');
$order = Config::get('columnsortable.default_order_unsorted', 'asc');
}

$parameters = [
'sort' => $sortOriginal,
'order' => Input::get('order') === 'desc' ? 'asc' : 'desc',
'order' => $order,
];

$queryString = http_build_query(array_merge(Request::except('sort', 'order', 'page'), $parameters));
Expand Down
12 changes: 11 additions & 1 deletion src/config/columnsortable.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,16 @@
/*
allow request modification, when default sorting is set but is not in URI (first load)
*/
'allow_request_modification' => true
'allow_request_modification' => true,

/*
default order for: $user->sortable(['id']) usage
*/
'default_order' => 'asc',

/*
default order for non-sorted columns
*/
'default_order_unsorted' => 'asc'

];

0 comments on commit ef6f198

Please sign in to comment.