Skip to content

Commit 44d3b44

Browse files
committed
Add clickable rows
1 parent 533afd3 commit 44d3b44

File tree

9 files changed

+95
-15
lines changed

9 files changed

+95
-15
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ All notable changes to `laravel-livewire-tables` will be documented in this file
77
### Added
88

99
- Turkish Translation - https://github.com/rappasoft/laravel-livewire-tables/pull/686
10+
- Added missing table row click functionality
11+
- Added ability to mark column as unclickable if you need a cell to have another clickable element with clickable rows turned on.
1012

1113
### Changed
1214

resources/views/components/table/td.blade.php

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,36 @@
88
@endphp
99

1010
@if ($theme === 'tailwind')
11-
<td {{
12-
$attributes->merge($customAttributes)
13-
->class(['px-6 py-4 whitespace-nowrap text-sm font-medium dark:text-white' => $customAttributes['default'] ?? true])
14-
->class(['hidden sm:table-cell' => $column && $column->shouldCollapseOnMobile()])
15-
->class(['hidden md:table-cell' => $column && $column->shouldCollapseOnTablet()])
16-
->except('default')
17-
}}>{{ $slot }}</td>
11+
<td
12+
@if ($column->isClickable())
13+
onclick="window.open('{{ $component->getTableRowUrl($row) }}', '{{ $component->getTableRowUrlTarget($row) ?? '_self' }}')"
14+
@endif
15+
16+
{{
17+
$attributes->merge($customAttributes)
18+
->class(['px-6 py-4 whitespace-nowrap text-sm font-medium dark:text-white' => $customAttributes['default'] ?? true])
19+
->class(['hidden sm:table-cell' => $column && $column->shouldCollapseOnMobile()])
20+
->class(['hidden md:table-cell' => $column && $column->shouldCollapseOnTablet()])
21+
->except('default')
22+
}}
23+
>
24+
{{ $slot }}
25+
</td>
1826
@elseif ($theme === 'bootstrap-4' || $theme === 'bootstrap-5')
19-
<td {{
20-
$attributes->merge($customAttributes)
21-
->class(['' => $customAttributes['default'] ?? true])
22-
->class(['d-none d-sm-table-cell' => $column && $column->shouldCollapseOnMobile()])
23-
->class(['d-none d-md-table-cell' => $column && $column->shouldCollapseOnTablet()])
24-
->except('default')
25-
}}>{{ $slot }}</td>
27+
<td
28+
@if ($column->isClickable())
29+
onclick="window.open('{{ $component->getTableRowUrl($row) }}', '{{ $component->getTableRowUrlTarget($row) ?? '_self' }}')"
30+
style="cursor:pointer"
31+
@endif
32+
33+
{{
34+
$attributes->merge($customAttributes)
35+
->class(['' => $customAttributes['default'] ?? true])
36+
->class(['d-none d-sm-table-cell' => $column && $column->shouldCollapseOnMobile()])
37+
->class(['d-none d-md-table-cell' => $column && $column->shouldCollapseOnTablet()])
38+
->except('default')
39+
}}
40+
>
41+
{{ $slot }}
42+
</td>
2643
@endif

resources/views/components/table/tr.blade.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
$attributes->merge($customAttributes)
2020
->class(['bg-white dark:bg-gray-700 dark:text-white' => ($customAttributes['default'] ?? true) && $rowIndex % 2 === 0])
2121
->class(['bg-gray-50 dark:bg-gray-800 dark:text-white' => ($customAttributes['default'] ?? true) && $rowIndex % 2 !== 0])
22+
->class(['cursor-pointer' => $component->hasTableRowUrl()])
2223
->except('default')
2324
}}
2425
>
@@ -27,7 +28,7 @@
2728
@elseif ($theme === 'bootstrap-4' || $theme === 'bootstrap-5')
2829
<tr
2930
wire:loading.class.delay=""
30-
31+
3132
@if ($component->reorderIsEnabled() && $component->currentlyReorderingIsEnabled())
3233
wire:sortable.item="{{ $row->getKey() }}"
3334
@endif

src/Traits/ComponentUtilities.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ trait ComponentUtilities
2626
protected array $tbodyAttributes = [];
2727
protected $thAttributesCallback;
2828
protected $trAttributesCallback;
29+
protected $trUrlCallback;
30+
protected $trUrlTargetCallback;
2931
protected $tdAttributesCallback;
3032
protected $collapsingColumnsStatus = true;
3133
protected string $emptyMessage = 'No items found. Try to broaden your search.';

src/Traits/Configuration/ComponentConfiguration.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,23 @@ public function setCollapsingColumnsDisabled(): self
261261

262262
return $this;
263263
}
264+
265+
266+
267+
268+
269+
270+
public function setTableRowUrl(callable $callback): self
271+
{
272+
$this->trUrlCallback = $callback;
273+
274+
return $this;
275+
}
276+
277+
public function setTableRowUrlTarget(callable $callback): self
278+
{
279+
$this->trUrlTargetCallback = $callback;
280+
281+
return $this;
282+
}
264283
}

src/Traits/Helpers/ComponentHelpers.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,4 +275,22 @@ public function collapsingColumnsAreDisabled(): bool
275275
{
276276
return $this->getCollapsingColumnsStatus() === false;
277277
}
278+
279+
280+
281+
282+
public function hasTableRowUrl(): bool
283+
{
284+
return $this->trUrlCallback !== null;
285+
}
286+
287+
public function getTableRowUrl($row): ?string
288+
{
289+
return $this->trUrlCallback ? call_user_func($this->trUrlCallback, $row) : null;
290+
}
291+
292+
public function getTableRowUrlTarget($row): ?string
293+
{
294+
return $this->trUrlTargetCallback ? call_user_func($this->trUrlTargetCallback, $row) : null;
295+
}
278296
}

src/Views/Column.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Illuminate\Support\Str;
66
use Rappasoft\LaravelLivewireTables\DataTableComponent;
7+
use Rappasoft\LaravelLivewireTables\Views\Columns\LinkColumn;
78
use Rappasoft\LaravelLivewireTables\Views\Traits\Configuration\ColumnConfiguration;
89
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\ColumnHelpers;
910
use Rappasoft\LaravelLivewireTables\Views\Traits\Helpers\RelationshipHelpers;
@@ -53,6 +54,7 @@ class Column
5354
protected $secondaryHeaderCallback;
5455
protected bool $footer = false;
5556
protected $footerCallback;
57+
protected bool $clickable = true;
5658

5759
/**
5860
* @param string $title

src/Views/Traits/Configuration/ColumnConfiguration.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,13 @@ public function footer($callback = null): self
176176

177177
return $this;
178178
}
179+
180+
181+
182+
public function unclickable(): self
183+
{
184+
$this->clickable = false;
185+
186+
return $this;
187+
}
179188
}

src/Views/Traits/Helpers/ColumnHelpers.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,4 +509,14 @@ public function arrayToAttributes(array $attributes)
509509
return $key . '="' . $attributes[$key] . '"';
510510
}, array_keys($attributes)));
511511
}
512+
513+
514+
515+
516+
public function isClickable(): bool
517+
{
518+
return $this->clickable &&
519+
$this->component->hasTableRowUrl() &&
520+
! $this instanceof LinkColumn;
521+
}
512522
}

0 commit comments

Comments
 (0)