Skip to content

Commit 79b459a

Browse files
committed
Merge branch 'feature-1.0.0' of https://github.com/DeltaSystems/laravel-livewire-tables into DeltaSystems-feature-1.0.0
# Conflicts: # resources/views/tailwind/datatable.blade.php - Use HtmlString renderer - Use table cell component in row-columns - Add URL as prop to row click - Add row hover if row url is enabled - Update readme
2 parents 860fedf + a5ec94e commit 79b459a

File tree

6 files changed

+162
-34
lines changed

6 files changed

+162
-34
lines changed

README.md

Lines changed: 62 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ php artisan vendor:publish --provider="Rappasoft\LaravelLivewireTables\LaravelLi
3333

3434
### Creating Tables
3535

36-
To create the most basic of a table, you need a new Livewire component that extends the DataTable component, and you need to define a list of a columns, the base query, and the view that renders an individual row.
36+
To create the most basic of a table, you need a new Livewire component that extends the DataTable component, and you need to define a list of a columns and a base query.
3737

3838
Example:
3939

@@ -52,13 +52,13 @@ class Table extends DataTableComponent
5252
public function columns(): array
5353
{
5454
return [
55-
Column::make('type', 'Type')
55+
Column::make('Type')
5656
->sortable()
5757
->multiColumn(),
58-
Column::make('name', 'Name')
58+
Column::make('Name')
5959
->sortable()
6060
->multiColumn(),
61-
Column::make('permissions', 'Permissions'),
61+
Column::make('Permissions'),
6262
Column::blank(),
6363
];
6464
}
@@ -67,18 +67,49 @@ class Table extends DataTableComponent
6767
{
6868
return Role::query();
6969
}
70-
71-
public function rowView(): string
72-
{
73-
return 'location.to.my.row.view';
74-
}
7570
}
7671
```
7772

7873
### Creating rows
7974

75+
By default, the rows will be generated by the column name, so if you have a users table, and the column is 'type', the cell generated for that column will be `$row['type']`.
76+
77+
If you would like to format the cell inline, you can use the format helper:
78+
79+
```php
80+
Column::make('Name')
81+
->sortable()
82+
->multiColumn()
83+
->format(function($value) {
84+
return timezone()->convertToLocal($value);
85+
}),
86+
```
87+
88+
**Note:** If you need more control, the full parameter list for the format callback is `$value, $column, $row`.
89+
90+
If you would like to render HTML from the format method, you may call `asHtml` on the column.
91+
92+
```php
93+
Column::make('Name')
94+
->sortable()
95+
->multiColumn()
96+
->format(function($value) {
97+
return '<strong>'.timezone()->convertToLocal($value).'</strong>';
98+
})
99+
->asHtml(),
100+
```
101+
102+
If you would like full control over your rows without using the Column formatter, than you can define a `rowView` and return the string to the view to render the rows. The view will be passed the current $row.
103+
80104
**row.blade.php**
81105

106+
```php
107+
public function rowView(): string
108+
{
109+
return 'location.to.my.row.view';
110+
}
111+
```
112+
82113
```html
83114
<x-livewire-tables::table.cell>
84115
{{ ucfirst($row->type) }}
@@ -109,7 +140,18 @@ class Table extends DataTableComponent
109140

110141
The row view will be passed the current model named as **$row**.
111142

112-
### Using the included blade components:
143+
#### Making the table row clickable
144+
145+
Add this method to your component if you would like to be able to click a row to go to a URL:
146+
147+
```php
148+
public function getTableRowUrl($row): string
149+
{
150+
return route('my.edit.route', $row);
151+
}
152+
```
153+
154+
### Using the included blade components in the row view:
113155

114156
To create cells, you should use the `<x-livewire-tables::table.cell>` table cell component, which will be rendered to:
115157

@@ -131,14 +173,14 @@ A list of all available methods are displayed below:
131173
public function columns(): array
132174
{
133175
return [
134-
Column::make('type', 'Type') // Column name and heading label
176+
Column::make('Type') // Column text and optional column name, column name will be snake case of text if not defined
135177
->sortable() // Whether or not the heading can be clicked to sort
136178
->multiColumn(),
137-
Column::make('name', 'Name')
179+
Column::make('Name')
138180
->sortable()
139181
->multiColumn(),
140-
Column::make('permissions', 'Permissions'),
141-
Column::make('other', 'Other')
182+
Column::make('Permissions'),
183+
Column::make('Other', 'my_other_column')
142184
->sortable() // Allows the column to interact with the sorting methods
143185
->multiColumn() // Adds visual cues for multi sorting this column with other columns
144186
->addClass('hidden md:table-cell'), // Adds to the existing class list
@@ -419,25 +461,25 @@ class UsersTable extends DataTableComponent
419461
public function columns(): array
420462
{
421463
return [
422-
Column::make('type', 'Type')
464+
Column::make('Type')
423465
->sortable()
424466
->multiColumn()
425467
->addClass('hidden md:table-cell'),
426-
Column::make('name', 'Name')
468+
Column::make('Name')
427469
->sortable()
428470
->multiColumn(),
429-
Column::make('email', 'E-mail')
471+
Column::make('E-mail', 'email')
430472
->sortable()
431473
->multiColumn(),
432-
Column::make('active', 'Active')
474+
Column::make('Active')
433475
->sortable()
434476
->multiColumn()
435477
->addClass('hidden md:table-cell'),
436-
Column::make('email_verified_at', 'Verified')
478+
Column::make('Verified', 'email_verified_at')
437479
->sortable()
438480
->multiColumn()
439481
->addClass('hidden md:table-cell'),
440-
Column::make('two_factor_secret', '2FA')
482+
Column::make('2FA', 'two_factor_secret')
441483
->sortable()
442484
->multiColumn()
443485
->addClass('hidden md:table-cell'),
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
@foreach($columns as $column)
2+
<x-livewire-tables::table.cell>
3+
@if ($column->asHtml)
4+
{{ new \Illuminate\Support\HtmlString($column->formatted($row)) }}
5+
@else
6+
{{ $column->formatted($row) }}
7+
@endif
8+
</x-livewire-tables::table.cell>
9+
@endforeach
Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
<tr {{ $attributes->merge(['class' => 'bg-white']) }}>
1+
@props(['url' => null])
2+
3+
<tr
4+
{{ $attributes->merge(['class' => 'bg-white']) }}
5+
6+
@if ($url)
7+
onclick="window.location='{{ $url }}';"
8+
style="cursor:pointer"
9+
@endif
10+
>
211
{{ $slot }}
312
</tr>

resources/views/tailwind/includes/table.blade.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class="ml-1 text-blue-600 underline text-cool-gray-700 text-sm leading-5 font-me
6565
<x-livewire-tables::table.row
6666
wire:loading.class.delay="opacity-50"
6767
wire:key="table-row-{{ $row->getKey() }}"
68-
class="{{ $index % 2 === 0 ? 'bg-white' : 'bg-gray-50' }}"
68+
:url="method_exists($this, 'getTableRowUrl') ? $this->getTableRowUrl($row) : null"
69+
:class="$index % 2 === 0 ? 'bg-white' . (method_exists($this, 'getTableRowUrl') ? ' hover:bg-gray-100' : '') : 'bg-gray-50' . (method_exists($this, 'getTableRowUrl') ? ' hover:bg-gray-100' : '')"
6970
>
7071
@if (count($bulkActions))
7172
<x-livewire-tables::table.cell>
@@ -80,7 +81,7 @@ class="rounded-md shadow-sm border-cool-gray-300 block transition duration-150 e
8081
</x-livewire-tables::table.cell>
8182
@endif
8283

83-
@include($rowView, ['row' => $row])
84+
@include($rowView)
8485
</x-livewire-tables::table.row>
8586
@empty
8687
<x-livewire-tables::table.row>

src/DataTableComponent.php

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,6 @@ abstract public function columns(): array;
119119
*/
120120
abstract public function query(): Builder;
121121

122-
/**
123-
* The view to render each row of the table.
124-
*
125-
* @return string
126-
*/
127-
abstract public function rowView(): string;
128-
129122
/**
130123
* TableComponent constructor.
131124
*
@@ -164,6 +157,16 @@ public function getRowsProperty(): LengthAwarePaginator
164157
return $this->applyPagination($this->rowsQuery);
165158
}
166159

160+
/**
161+
* The view to render each row of the table.
162+
*
163+
* @return string
164+
*/
165+
public function rowView(): string
166+
{
167+
return 'livewire-tables::'.config('livewire-tables.theme').'.components.table.row-columns';
168+
}
169+
167170
/**
168171
* @return mixed
169172
*/

src/Views/Column.php

Lines changed: 68 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,17 +39,32 @@ 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;
5061

62+
if (! $column && $text) {
63+
$this->column = Str::snake($text);
64+
} else {
65+
$this->column = $column;
66+
}
67+
5168
if (! $this->column && ! $this->text) {
5269
$this->blank = true;
5370
}
@@ -59,9 +76,9 @@ public function __construct(string $column = null, string $text = null)
5976
*
6077
* @return Column
6178
*/
62-
public static function make(string $column = null, string $text = null): Column
79+
public static function make(string $text = null, string $column = null): Column
6380
{
64-
return new static($column, $text);
81+
return new static($text, $column);
6582
}
6683

6784
/**
@@ -128,6 +145,16 @@ public function addClass(string $class): self
128145
return $this;
129146
}
130147

148+
/**
149+
* @return Column
150+
*/
151+
public function asHtml(): Column
152+
{
153+
$this->asHtml = true;
154+
155+
return $this;
156+
}
157+
131158
/**
132159
* @return string|null
133160
*/
@@ -151,4 +178,41 @@ public function text(): ?string
151178
{
152179
return $this->text;
153180
}
181+
182+
/**
183+
* @param callable $callable
184+
*
185+
* @return $this
186+
*/
187+
public function format(callable $callable): Column
188+
{
189+
$this->formatCallback = $callable;
190+
191+
return $this;
192+
}
193+
194+
/**
195+
* @param $row
196+
* @param null $column
197+
*
198+
* @return array|mixed|null
199+
*/
200+
public function formatted($row, $column = null)
201+
{
202+
if ($column instanceof self) {
203+
$columnName = $column->column();
204+
} elseif (is_string($column)) {
205+
$columnName = $column;
206+
} else {
207+
$columnName = $this->column();
208+
}
209+
210+
$value = data_get($row, $columnName);
211+
212+
if ($this->formatCallback) {
213+
return app()->call($this->formatCallback, ['value' => $value, 'column' => $column, 'row' => $row]);
214+
}
215+
216+
return $value;
217+
}
154218
}

0 commit comments

Comments
 (0)