Skip to content

Commit 37fd056

Browse files
committed
added default row-columns view, set rowViews() method to this new blade, re-added format() method on column, re-added formatted() method to column with some enhancements to make it easier to use, added asHtml() mutator to control data output, re-added support for getTableRowUrl link, flipped column and text parameters for Column constructor to improve backwards compatability, if columne parameter is missing then set it to the column text snake cased
1 parent cac7983 commit 37fd056

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
@php /** @var \Rappasoft\LaravelLivewireTables\Views\Column[] $columns **/ @endphp
2+
@foreach($columns as $column)
3+
<td>
4+
@if($column->asHtml)
5+
{!! (string)$column->formatted($row) !!}
6+
@else
7+
{{ $column->formatted($row) }}
8+
@endif
9+
</td>
10+
@endforeach
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
<tr {{ $attributes->merge(['class' => 'bg-white']) }}>
1+
<tr {{ $attributes->merge(['class' => 'bg-white']) }}
2+
@if ($url)
3+
onclick="window.location='{{ $url }}';"
4+
style="cursor:pointer"
5+
@endif
6+
>
27
{{ $slot }}
38
</tr>

resources/views/tailwind/datatable.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,7 @@ class="ml-1 text-blue-600 underline text-cool-gray-700 text-sm leading-5 font-me
314314
<x-livewire-tables::table.row
315315
wire:loading.class.delay="opacity-50"
316316
wire:key="table-row-{{ $row->getKey() }}"
317+
url="{{ method_exists($this, 'getTableRowUrl') ? $this->getTableRowUrl($row) : null }}"
317318
class="{{ $index % 2 === 0 ? 'bg-white' : 'bg-gray-50' }}"
318319
>
319320
@if (count($bulkActions))
@@ -329,7 +330,7 @@ class="rounded-md shadow-sm border-cool-gray-300 block transition duration-150 e
329330
</x-livewire-tables::table.cell>
330331
@endif
331332

332-
@include($rowsView, ['row' => $row])
333+
@include($rowsView)
333334
</x-livewire-tables::table.row>
334335
@empty
335336
<x-livewire-tables::table.row>

src/DataTableComponent.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,10 @@ abstract public function query(): Builder;
117117
*
118118
* @return string
119119
*/
120-
abstract public function rowView(): string;
120+
public function rowView(): string
121+
{
122+
return 'livewire-tables::'.config('livewire-tables.theme').'.components.table.row-columns';
123+
}
121124

122125
/**
123126
* TableComponent constructor.

src/Views/Column.php

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Views;
44

5+
use Illuminate\Support\Str;
6+
57
/**
68
* Class Column.
79
*/
@@ -37,16 +39,30 @@ class Column
3739
*/
3840
public bool $blank = false;
3941

42+
/**
43+
* @var
44+
*/
45+
public $formatCallback;
46+
47+
/**
48+
* @var bool
49+
*/
50+
public bool $asHtml = false;
51+
4052
/**
4153
* Column constructor.
4254
*
4355
* @param string|null $column
4456
* @param string|null $text
4557
*/
46-
public function __construct(string $column = null, string $text = null)
58+
public function __construct(string $text = null, string $column = null)
4759
{
48-
$this->column = $column;
4960
$this->text = $text;
61+
if (!$column) {
62+
$this->column = Str::snake($text);
63+
}else{
64+
$this->column = $column;
65+
}
5066

5167
if (! $this->column && ! $this->text) {
5268
$this->blank = true;
@@ -59,9 +75,9 @@ public function __construct(string $column = null, string $text = null)
5975
*
6076
* @return Column
6177
*/
62-
public static function make(string $column = null, string $text = null): Column
78+
public static function make(string $text = null, string $column = null): Column
6379
{
64-
return new static($column, $text);
80+
return new static($text, $column);
6581
}
6682

6783
/**
@@ -151,4 +167,51 @@ public function text(): ?string
151167
{
152168
return $this->text;
153169
}
170+
171+
/**
172+
* @param callable $callable
173+
*
174+
* @return $this
175+
*/
176+
public function format(callable $callable): Column
177+
{
178+
$this->formatCallback = $callable;
179+
180+
return $this;
181+
}
182+
183+
/**
184+
* @param $model
185+
* @param $column
186+
*
187+
* @return mixed
188+
*/
189+
public function formatted($row, $column = null)
190+
{
191+
if ($column && $column instanceof Column) {
192+
$columnName = $column->column();
193+
} elseif ( is_string($column) ) {
194+
$columnName = $column;
195+
} else {
196+
$columnName = $this->column();
197+
}
198+
199+
$value = data_get($row, $columnName) ?? null;
200+
201+
if ($this->formatCallback) {
202+
return app()->call($this->formatCallback, ['value' => $value, 'column' => $column, 'row' => $row]);
203+
}else{
204+
return $value;
205+
}
206+
}
207+
208+
/**
209+
* @return bool
210+
*/
211+
public function asHtml(): self
212+
{
213+
$this->asHtml = true;
214+
215+
return $this;
216+
}
154217
}

0 commit comments

Comments
 (0)