Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/Concerns/Sorting.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,15 @@ public function updatedSortField(): void
}
}

public function resolveSortField(string $sortField): string
{
if (str_contains($sortField, '.') || $this->ignoreTablePrefix) {
return $sortField;
}

return $this->currentTable.'.'.$sortField;
}

/**
* Get the sort callback for a given field from the columns definition.
* Returns null if no custom callback is defined.
Expand Down
14 changes: 2 additions & 12 deletions src/DataSource/Processors/Database/Pipelines/Sorting.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Str;
use PowerComponents\LivewirePowerGrid\PowerGridComponent;

class Sorting
Expand Down Expand Up @@ -40,7 +39,7 @@ private function applySingleSort(EloquentBuilder|MorphToMany|QueryBuilder $query
return;
}

$query->orderBy($this->makeSortField($sortField), $direction);
$query->orderBy($this->component->resolveSortField($sortField), $direction);
}

private function applyMultipleSort(EloquentBuilder|MorphToMany|QueryBuilder $results): void
Expand All @@ -54,16 +53,7 @@ private function applyMultipleSort(EloquentBuilder|MorphToMany|QueryBuilder $res
continue;
}

$results->orderBy($this->makeSortField($sortField), $direction);
$results->orderBy($this->component->resolveSortField($sortField), $direction);
}
}

private function makeSortField(string $sortField): string
{
if (Str::of($sortField)->contains('.') || $this->component->ignoreTablePrefix) {
return $sortField;
}

return $this->component->currentTable.'.'.$sortField;
}
}
16 changes: 15 additions & 1 deletion src/Traits/ExportableJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ private function prepareToExport(array $properties = []): Eloquent\Collection|Co
$filtered = $processDataSource->component->filtered ?? [];
$currentTable = $processDataSource->component->currentTable;

/** @var array{sortField?: string, sortDirection?: string} $queryOptions */
$queryOptions = data_get($this->exportable, 'queryOptions', []);

// data_get's default only applies when the key is missing, so guard against malformed query options.
if (! is_array($queryOptions)) {
$queryOptions = [];
}

$property = function (string $property) use ($processDataSource, $currentTable) {
$property = $processDataSource->component->{$property};

Expand All @@ -61,6 +69,12 @@ private function prepareToExport(array $properties = []): Eloquent\Collection|Co
: $currentTable.'.'.$property;
};

$sortField = $queryOptions['sortField']
?? $processDataSource->component->resolveSortField($processDataSource->component->sortField);

$sortDirection = $queryOptions['sortDirection']
?? $processDataSource->component->sortDirection;

$results = $processDataSource->datasource
->where(function ($query) {
app()->makeWith(SearchHandlerContract::class, [
Expand All @@ -73,7 +87,7 @@ private function prepareToExport(array $properties = []): Eloquent\Collection|Co
})
->offset($this->offset)
->limit($this->limit)
->orderBy($property('sortField'), $processDataSource->component->sortDirection)
->orderBy($sortField, $sortDirection)
->get();

$dataTransformer = new DataTransformer($processDataSource->component);
Expand Down
5 changes: 3 additions & 2 deletions src/Traits/WithExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ public function prepareToExport(bool $selected = false): Eloquent\Collection|Col
->when($filtered, function ($query, $filtered) use ($property) {
return $query->whereIn($property('primaryKey'), $filtered);
})
->when($this->sortField, function ($query) use ($property, $processDataSource, $queryOptions) {
$sortField = $queryOptions['sortField'] ?? $property('sortField');
->when($this->sortField, function ($query) use ($processDataSource, $queryOptions) {
$sortField = $queryOptions['sortField']
?? $processDataSource->component->resolveSortField($processDataSource->component->sortField);
$sortDirection = $queryOptions['sortDirection'] ?? $processDataSource->component->sortDirection;

return $query->orderBy($sortField, $sortDirection);
Expand Down
48 changes: 47 additions & 1 deletion tests/Feature/ExportTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Facades\DB;
use OpenSpout\Reader\XLSX\Reader;
use PowerComponents\LivewirePowerGrid\{Button, Column, Components\SetUp\Exportable, PowerGridFields};
use PowerComponents\LivewirePowerGrid\Facades\PowerGrid;
Expand Down Expand Up @@ -224,6 +226,50 @@ public function columns(): array
'html' => [$exportWithHtml::class],
]);

$exportWithJoinedAliasSort = new class() extends ExportTable
{
public function datasource(): Builder
{
return parent::datasource()
->join('categories', function ($categories) {
$categories->on('dishes.category_id', '=', 'categories.id');
})
->select('dishes.*', DB::raw('categories.name as category_name'));
}

public function fields(): PowerGridFields
{
return PowerGrid::fields()
->add('category_name');
}

public function columns(): array
{
return [
Column::add()
->title('Category')
->field('category_name')
->sortable(),
];
}
};

it('properly prepares export data sorted by a joined alias column', function (string $component) {
$component = livewire($component)
->set('checkboxValues', [
0 => '1',
1 => '3',
])
->call('sortBy', 'category_name');

expect($component->instance()->prepareToExport(true)->pluck('category_name')->values()->all())
->toBe(['Carnes', 'Sobremesas']);
})->with('export_with_joined_alias_sort');

dataset('export_with_joined_alias_sort', [
'joined alias' => [$exportWithJoinedAliasSort::class],
]);

$exportWithQueryOptions = new class() extends ExportTable
{
public string $testSortField = '';
Expand Down Expand Up @@ -302,7 +348,7 @@ public function columns(): array
data_get($downloadEffect, 'name')
);

$content = str_replace(PHP_EOL, '<csv-divider>', base64_decode(data_get($downloadEffect, 'content')));
$content = str_replace(["\r\n", "\n", "\r"], '<csv-divider>', base64_decode(data_get($downloadEffect, 'content')));

$expected = collect(array_merge([$headings], $rows))
->transform(function ($heading) use ($delimiter, $separator) {
Expand Down
Loading