Skip to content

Commit fed8ad6

Browse files
thyseusHerbert Maschke
andauthored
allow to have more than just one label column or checkbox column per (MedicOneSystems#293)
table Co-authored-by: Herbert Maschke <thyseus@pm.me>
1 parent c5618ac commit fed8ad6

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

src/Column.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ class Column
3333
public $width;
3434
public $exportCallback;
3535

36+
/** @var array list all column types that are not sortable by SQL here */
37+
public const UNSORTABLE_TYPES = ['label', 'checkbox'];
38+
3639
public static function name($name)
3740
{
3841
$column = new static;

src/Http/Livewire/LivewireDatatable.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Livewire\Component;
1818
use Livewire\WithPagination;
1919
use Maatwebsite\Excel\Facades\Excel;
20+
use Mediconesystems\LivewireDatatables\Column;
2021
use Mediconesystems\LivewireDatatables\ColumnSet;
2122
use Mediconesystems\LivewireDatatables\Exports\DatatableExport;
2223
use Mediconesystems\LivewireDatatables\Traits\WithCallbacks;
@@ -438,8 +439,12 @@ public function getFreshColumnsProperty()
438439
{
439440
$columns = $this->processedColumns->columnsArray();
440441

441-
if (($name = collect($columns)->pluck('name')->duplicates()) && collect($columns)->pluck('name')->duplicates()->count()) {
442-
throw new Exception('Duplicate Column Name: ' . $name->first());
442+
$duplicates = collect($columns)->reject(function ($column) {
443+
return in_array($column['type'], Column::UNSORTABLE_TYPES);
444+
})->pluck('name')->duplicates();
445+
446+
if ($duplicates->count()) {
447+
throw new Exception('Duplicate Column Name(s): ' . implode(', ', $duplicates->toArray()));
443448
}
444449

445450
return $columns;
@@ -501,8 +506,7 @@ public function initialiseSort()
501506
$this->sort = $this->defaultSort()
502507
? $this->defaultSort()['key']
503508
: collect($this->freshColumns)->reject(function ($column) {
504-
// list all column types that are not sortable by SQL:
505-
return in_array($column['type'], ['checkbox', 'label']) || $column['hidden'];
509+
return in_array($column['type'], Column::UNSORTABLE_TYPES) || $column['hidden'];
506510
})->keys()->first();
507511

508512
$this->getSessionStoredSort();

0 commit comments

Comments
 (0)