Skip to content

Commit 3dc9809

Browse files
committed
introduce headerAlign...() and contentAlign...()
1 parent 24c359b commit 3dc9809

File tree

10 files changed

+61
-24
lines changed

10 files changed

+61
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ All notable changes to `livewire-datatables` will be documented in this file
88

99
## 0.9.0 ( 2022-03-22 )
1010

11-
- Breaking Change: 'unsortable' has been renamed to 'sortable', which is more intuitive. Please adjust your overwritten
12-
- views, if any (thyseus).
11+
- Breaking Change: 'unsortable' has been renamed to 'sortable', which is more intuitive. Please adjust your overwritten views, if any (thyseus).
12+

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,12 @@ class ComplexDemoTable extends LivewireDatatable
202202
|**filterOn**|*String/Array* $statement|Allows you to specify a column name or sql statement upon which to perform the filter (must use SQL syntax, not Eloquent eg. ```'users.name'``` instead of ```'user.name'```). Useful if using a callback to modify the displayed values. Can pass a single string or array of strings which will be combined with ```OR```|```Column::callback(['name', 'allegiance'], function ($name, $allegiance) { return "$name is allied to $allegiance"; })->filterable(['Rebellion', 'Empire'])->filterOn('users.allegiance')```|
203203
|**view**|*String* $viewName| Passes the column value, whole row of values, and any additional parameters to a view template | _(see below)_|
204204
|**editable**| | Marks the column as editable | _(see below)_|
205-
|**alignCenter**| | Center-aligns column header and contents |```Column::delete()->alignCenter()```|
206-
|**alignRight**| | Right-aligns column header and contents |```Column::delete()->alignRight()```|
205+
|**alignCenter**| | Center-aligns column header _and_ contents |```Column::delete()->alignCenter()```|
206+
|**alignRight**| | Right-aligns column header _and_ contents |```Column::delete()->alignRight()```|
207+
|**contentAlignCenter**| | Center-aligns column contents |```Column::delete()->contentAlignCenter()```|
208+
|**contentAlignRight**| | Right-aligns column contents |```Column::delete()->contentAlignRight()```|
209+
|**headerAlignCenter**| | Center-aligns column header |```Column::delete()->headerAlignCenter()```|
210+
|**headerAlignRight**| | Right-aligns column header |```Column::delete()->headerAlignRight()```|
207211
|**editable**| | Marks the column as editable | _(see below)_|
208212
|**exportCallback**| Closure $callback | Reformats the result when exporting | _(see below)_ |
209213
|**excludeFromExport**| | Excludes the column from export |```Column::name('email')->excludeFromExport()```|

resources/views/livewire/datatables/datatable.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class="px-3 py-2 text-xs font-medium tracking-wider text-green-500 uppercase bg-
163163
@elseif($column['type'] === 'label')
164164
@include('datatables::label')
165165
@else
166-
<div class="table-cell px-6 py-2 @unless($column['wrappable']) whitespace-nowrap truncate @endunless @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @else text-left @endif {{ $this->cellClasses($row, $column) }}">
166+
<div class="table-cell px-6 py-2 @unless($column['wrappable']) whitespace-nowrap truncate @endunless @if($column['contentAlign'] === 'right') text-right @elseif($column['contentAlign'] === 'center') text-center @else text-left @endif {{ $this->cellClasses($row, $column) }}">
167167
{!! $row->{$column['name']} !!}
168168
</div>
169169
@endif
@@ -180,7 +180,7 @@ class="px-3 py-2 text-xs font-medium tracking-wider text-green-500 uppercase bg-
180180
@foreach($this->columns as $column)
181181
@unless($column['hidden'])
182182
@if ($column['summary'])
183-
<div class="table-cell px-6 py-2 whitespace-no-wrap @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @else text-left @endif {{ $this->cellClasses($row, $column) }}">
183+
<div class="table-cell px-6 py-2 @unless ($column['wrappable']) whitespace-nowrap truncate @endunless @if($column['contentAlign'] === 'right') text-right @elseif($column['contentAlign'] === 'center') text-center @else text-left @endif {{ $this->cellClasses($row, $column) }}">
184184
{{ $this->summarize($column['name']) }}
185185
</div>
186186
@else

resources/views/livewire/datatables/header-inline-hide.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class="w-32 hidden group-hover:inline-block absolute z-10 top-0 left-0 ml-3 bg-b
1818
@if($column['sortable'])
1919
<button wire:click="sort('{{ $index }}')"
2020
class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider flex justify-between items-center focus:outline-none">
21-
<span class="inline flex-grow @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @endif"">{{ str_replace('_', ' ', $column['label']) }}</span>
21+
<span class="inline flex-grow @if($column['headerAlign'] === 'right') text-right @elseif($column['headerAlign'] === 'center') text-center @endif"">{{ str_replace('_', ' ', $column['label']) }}</span>
2222
<span class="inline text-xs text-blue-400">
2323
@if($sort === $index)
2424
@if($direction)
@@ -31,7 +31,7 @@ class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-xs leadi
3131
</button>
3232
@else
3333
<div class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider flex justify-between items-center focus:outline-none">
34-
<span class="inline flex-grow @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @endif"">{{ str_replace('_', ' ', $column['label']) }}</span>
34+
<span class="inline flex-grow @if($column['headerAlign'] === 'right') text-right @elseif($column['headerAlign'] === 'center') text-center @endif"">{{ str_replace('_', ' ', $column['label']) }}</span>
3535
</div>
3636
@endif
3737

resources/views/livewire/datatables/header-no-hide.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@if (isset($column['tooltip']['text'])) title="{{ $column['tooltip']['text'] }}" @endif
44
class="relative table-cell h-12 overflow-hidden align-top" @include('datatables::style-width')>
55
@if($column['sortable'])
6-
<button wire:click="sort('{{ $index }}')" class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider flex items-center focus:outline-none @if($column['align'] === 'right') justify-end @elseif($column['align'] === 'center') justify-center @endif">
6+
<button wire:click="sort('{{ $index }}')" class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider flex items-center focus:outline-none @if($column['headerAlign'] === 'right') justify-end @elseif($column['headerAlign'] === 'center') justify-center @endif">
77
<span class="inline ">{{ str_replace('_', ' ', $column['label']) }}</span>
88
<span class="inline text-xs text-blue-400">
99
@if($sort === $index)
@@ -16,7 +16,7 @@ class="relative table-cell h-12 overflow-hidden align-top" @include('datatables:
1616
</span>
1717
</button>
1818
@else
19-
<div class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider flex items-center focus:outline-none @if($column['align'] === 'right') justify-end @elseif($column['align'] === 'center') justify-center @endif">
19+
<div class="w-full h-full px-6 py-3 border-b border-gray-200 bg-gray-50 text-left text-xs leading-4 font-medium text-gray-500 uppercase tracking-wider flex items-center focus:outline-none @if($column['headerAlign'] === 'right') justify-end @elseif($column['headerAlign'] === 'center') justify-center @endif">
2020
<span class="inline ">{{ str_replace('_', ' ', $column['label']) }}</span>
2121
</div>
2222
@endif
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
<div class="table-cell px-6 py-2 whitespace-no-wrap @if($column['align'] === 'right') text-right @elseif($column['align'] === 'center') text-center @else text-left @endif {{ $this->cellClasses($row, $column) }}">
1+
<div class="table-cell px-6 py-2 whitespace-no-wrap @if($column['headerAlign'] === 'right') text-right @elseif($column['headerAlign'] === 'center') text-center @else text-left @endif {{ $this->cellClasses($row, $column) }}">
22
{!! $column['content'] ?? '' !!}
33
</div>

src/Column.php

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ class Column
3232
public $params = [];
3333
public $additionalSelects = [];
3434
public $filterView;
35-
public $align = 'left';
35+
public $headerAlign = 'left';
36+
public $contentAlign = 'left';
3637
public $preventExport;
3738
public $width;
3839
public $minWidth;
@@ -399,14 +400,44 @@ public function hide()
399400

400401
public function alignRight()
401402
{
402-
$this->align = 'right';
403+
$this->headerAlign = 'right';
404+
$this->contentAlign = 'right';
403405

404406
return $this;
405407
}
406408

407-
public function alignCenter()
409+
public function alignCener()
408410
{
409-
$this->align = 'center';
411+
$this->headerAlign = 'center';
412+
$this->contentAlign = 'center';
413+
414+
return $this;
415+
}
416+
417+
public function headerAlignRight()
418+
{
419+
$this->headerAlign = 'right';
420+
421+
return $this;
422+
}
423+
424+
public function contentAlignRight()
425+
{
426+
$this->contentAlign = 'right';
427+
428+
return $this;
429+
}
430+
431+
public function headerAlignCenter()
432+
{
433+
$this->headerAlign = 'center';
434+
435+
return $this;
436+
}
437+
438+
public function contentAlignCenter()
439+
{
440+
$this->contentAlign = 'center';
410441

411442
return $this;
412443
}

src/Http/Livewire/LivewireDatatable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,8 @@ public function getViewColumns()
278278
'group',
279279
'summary',
280280
'content',
281-
'align',
281+
'headerAlign',
282+
'contentAlign',
282283
'type',
283284
'filterable',
284285
'hideable',

src/NumberColumn.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
class NumberColumn extends Column
66
{
77
public $type = 'number';
8-
public $align = 'right';
8+
public $headerAlign = 'right';
9+
public $contentAlign = 'right';
910
public $round;
1011

11-
public function round($places = 0)
12+
public function round($places = 0): self
1213
{
1314
$this->round = $places;
1415

@@ -19,7 +20,7 @@ public function round($places = 0)
1920
return $this;
2021
}
2122

22-
public function format($places = 0)
23+
public function format(int $places = 0): self
2324
{
2425
$this->callback = function ($value) use ($places) {
2526
return number_format($value, $places, '.', ',');

tests/ColumnTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public function it_returns_an_array_from_column()
9090
'select' => null,
9191
'joins' => null,
9292
'aggregate' => 'group_concat',
93-
'align' => 'left',
93+
'headerAlign' => 'left',
9494
'preventExport' => null,
9595
'width' => null,
9696
'exportCallback' => function () {
@@ -134,7 +134,7 @@ public function it_returns_an_array_from_raw()
134134
'filterView' => null,
135135
'select' => DB::raw('SELECT column FROM table'),
136136
'joins' => null,
137-
'align' => 'left',
137+
'headerAlign' => 'left',
138138
'preventExport' => null,
139139
'width' => null,
140140
'exportCallback' => null,
@@ -176,7 +176,7 @@ public function it_returns_width_property_from_column()
176176
'select' => null,
177177
'joins' => null,
178178
'aggregate' => 'group_concat',
179-
'align' => 'left',
179+
'headerAlign' => 'left',
180180
'preventExport' => null,
181181
'width' => '1em',
182182
'exportCallback' => null,
@@ -216,7 +216,7 @@ public function check_invalid_width_unit_not_returning_value()
216216
'select' => null,
217217
'joins' => null,
218218
'aggregate' => 'group_concat',
219-
'align' => 'left',
219+
'headerAlign' => 'left',
220220
'preventExport' => null,
221221
'width' => null,
222222
], $subject);
@@ -249,7 +249,7 @@ public function check_adding_px_to_numeric_width_input()
249249
'select' => null,
250250
'joins' => null,
251251
'aggregate' => 'group_concat',
252-
'align' => 'left',
252+
'headerAlign' => 'left',
253253
'preventExport' => null,
254254
'width' => '5px',
255255
], $subject);

0 commit comments

Comments
 (0)