Skip to content

Commit 051e5e4

Browse files
feat(export): add query options for sorting in export feature (#1978)
* feat(export): add query options for sorting in export feature * feat(export): add query options for sorting in export feature
1 parent 4f47065 commit 051e5e4

File tree

3 files changed

+70
-3
lines changed

3 files changed

+70
-3
lines changed

src/Components/SetUp/Exportable.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ final class Exportable implements Wireable
2828

2929
public bool $stripTags = false;
3030

31+
public array $queryOptions = [];
32+
3133
public function __construct(public string $fileName = 'export') {}
3234

3335
public function type(string ...$types): self
@@ -95,6 +97,13 @@ public function onConnection(string $connection): self
9597
return $this;
9698
}
9799

100+
public function queryOptions(array $options): self
101+
{
102+
$this->queryOptions = $options;
103+
104+
return $this;
105+
}
106+
98107
public function toLivewire(): array
99108
{
100109
return (array) $this;

src/Traits/WithExport.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
195195
: $currentTable.'.'.$property;
196196
};
197197

198+
$queryOptions = data_get($this->setUp, 'exportable.queryOptions', []);
199+
198200
$results = $processDataSource->component->datasource()
199201
->where(function ($query) {
200202
(new SearchHandler($this))->apply($query);
@@ -203,8 +205,11 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Sup
203205
->when($filtered, function ($query, $filtered) use ($property) {
204206
return $query->whereIn($property('primaryKey'), $filtered);
205207
})
206-
->when($this->sortField, function ($query) use ($property, $processDataSource) {
207-
return $query->orderBy($property('sortField'), $processDataSource->component->sortDirection);
208+
->when($this->sortField, function ($query) use ($property, $processDataSource, $queryOptions) {
209+
$sortField = $queryOptions['sortField'] ?? $property('sortField');
210+
$sortDirection = $queryOptions['sortDirection'] ?? $processDataSource->component->sortDirection;
211+
212+
return $query->orderBy($sortField, $sortDirection);
208213
})
209214
->get();
210215

tests/Feature/ExportTest.php

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use OpenSpout\Reader\XLSX\Reader;
4-
use PowerComponents\LivewirePowerGrid\{Button,Column, PowerGridFields};
4+
use PowerComponents\LivewirePowerGrid\{Button, Column, Components\SetUp\Exportable, PowerGridFields};
55
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
66
use PowerComponents\LivewirePowerGrid\Tests\Concerns\Components\ExportTable;
77

@@ -224,6 +224,59 @@ public function columns(): array
224224
'html' => [$exportWithHtml::class],
225225
]);
226226

227+
$exportWithQueryOptions = new class() extends ExportTable
228+
{
229+
public string $testSortField = '';
230+
231+
public string $testSortDirection = '';
232+
233+
public function setUp(): array
234+
{
235+
$this->showCheckBox();
236+
237+
return [
238+
PowerGrid::exportable('export')
239+
->type(Exportable::TYPE_XLS, Exportable::TYPE_CSV)
240+
->queryOptions([
241+
'sortField' => $this->testSortField,
242+
'sortDirection' => $this->testSortDirection,
243+
]),
244+
];
245+
}
246+
247+
public function fields(): PowerGridFields
248+
{
249+
return PowerGrid::fields()
250+
->add('name', function ($dish) {
251+
return $dish->name;
252+
});
253+
}
254+
255+
public function columns(): array
256+
{
257+
return [
258+
Column::add()
259+
->title('name')
260+
->field('name'),
261+
];
262+
}
263+
};
264+
265+
it('properly exports with query options', function ($component, $options, $expectedRows) {
266+
$downloadedFile = livewire($component, $options)
267+
->set('checkboxValues', [0 => '1', 1 => '3'])
268+
->call('exportToCsv', true)
269+
->assertFileDownloaded('export.csv');
270+
271+
expect($downloadedFile)->toBeCsvDownload(['name'], $expectedRows);
272+
273+
})->with('export_with_query_options')->requiresOpenSpout();
274+
275+
dataset('export_with_query_options', [
276+
'desc' => [$exportWithQueryOptions::class, ['testSortField' => 'name', 'testSortDirection' => 'desc'], [['Pastel de Nata'], ['Carne Louca']]],
277+
'asc' => [$exportWithQueryOptions::class, ['testSortField' => 'name', 'testSortDirection' => 'asc'], [['Carne Louca'], ['Pastel de Nata']]],
278+
]);
279+
227280
/*
228281
|--------------------------------------------------------------------------
229282
| Expectations for this test

0 commit comments

Comments
 (0)