Skip to content

Commit 84a763c

Browse files
committed
implements
1 parent feb2442 commit 84a763c

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

src/Attributes/Structure.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ class Structure
1111
'crtNo', 'dataRouteSuffix', 'debounce', 'dtRowId', 'filters', 'flatten',
1212
'fullInfoRecordLimit', 'lengthMenu', 'method', 'model', 'name', 'preview',
1313
'responsive', 'searchMode', 'searchModes', 'selectable', 'strip', 'templateCache',
14-
'defaultSort', 'totalLabel',
14+
'defaultSort', 'defaultSortDirection', 'totalLabel',
1515
];
1616
}

src/Exceptions/Template.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ public static function invalidSearchModes()
3737
return new static(__('"searchModes" attribute must be an associative array'));
3838
}
3939

40+
public static function invalidSortDirection()
41+
{
42+
return new static(__('"defaultSortDirection" attribute must be either "asc" or "desc'));
43+
}
44+
4045
public static function invalidDebounce()
4146
{
4247
return new static(__('"debounce" attribute must be an integer'));

src/Services/Data/Sorts/Sort.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LaravelEnso\Tables\Services\Data\Sorts;
44

55
use Illuminate\Database\Eloquent\Builder;
6+
use Illuminate\Support\Str;
67
use LaravelEnso\Tables\Services\Data\Config;
78

89
class Sort
@@ -20,7 +21,13 @@ public function handle(): void
2021
if ($sort->applies()) {
2122
$sort->handle();
2223
} elseif (! $this->query->getQuery()->orders) {
23-
$this->query->orderBy($this->config->template()->get('defaultSort'));
24+
$column = $this->config->template()->get('defaultSort');
25+
26+
$direction = $this->config->template()->has('defaultSortDirection')
27+
? Str::lower($this->config->template()->get('defaultSortDirection'))
28+
: 'asc';
29+
30+
$this->query->orderBy($column, $direction);
2431
}
2532
}
2633
}

src/Services/Template/Validators/Structure/Attributes.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace LaravelEnso\Tables\Services\Template\Validators\Structure;
44

5+
use Illuminate\Support\Str;
56
use LaravelEnso\Helpers\Services\Obj;
67
use LaravelEnso\Tables\Exceptions\Template as Exception;
78

@@ -16,6 +17,7 @@ public function validate(): void
1617
$this->lengthMenu()
1718
->appends()
1819
->searchMode()
20+
->defaultSortDirection()
1921
->debounce()
2022
->method()
2123
->selectable()
@@ -58,6 +60,20 @@ private function searchMode()
5860
return $this;
5961
}
6062

63+
private function defaultSortDirection()
64+
{
65+
$allowed = ['asc', 'desc'];
66+
67+
if (
68+
$this->template->has('defaultSortDirection')
69+
&& ! in_array(Str::lower($this->template->get('defaultSortDirection')), $allowed)
70+
) {
71+
throw Exception::invalidSortDirection();
72+
}
73+
74+
return $this;
75+
}
76+
6177
private function debounce()
6278
{
6379
if (

tests/units/Services/Table/Builders/DataTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,15 @@ public function can_get_data_with_default_sort()
194194
TestModel::orderBy('name')->first()->name,
195195
$response->first()->get('name')
196196
);
197+
198+
$this->config->template()->set('defaultSortDirection', 'desc');
199+
200+
$response = $this->requestResponse();
201+
202+
$this->assertEquals(
203+
TestModel::orderBy('name')->latest()->first()->name,
204+
$response->last()->get('name')
205+
);
197206
}
198207

199208
/** @test */

0 commit comments

Comments
 (0)