Skip to content

Commit 52feab7

Browse files
committed
Merge branch 'pills-fallback-value' of https://github.com/def-studio/laravel-livewire-tables into def-studio-pills-fallback-value
2 parents 9d6bb5e + 36986f2 commit 52feab7

File tree

8 files changed

+70
-9
lines changed

8 files changed

+70
-9
lines changed

resources/views/bootstrap-4/components/table/row.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@props(['url' => null, 'target' => '_self', 'reordering' => false, 'customAttributes' => []])
22

3-
@if (!$reordering && $attributes->has('wire:sortable.item'))
3+
@if (!$reordering && (method_exists($attributes, 'has') ? $attributes->has('wire:sortable.item') : array_key_exists('wire:sortable.item', $attributes->getAttributes())))
44
@php
55
$attributes = $attributes->filter(fn ($value, $key) => $key !== 'wire:sortable.item');
66
@endphp

resources/views/bootstrap-4/includes/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
wire:key="filter-pill-{{ $key }}"
1010
class="badge badge-pill badge-info d-inline-flex align-items-center"
1111
>
12-
{{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}:
12+
{{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, isset($customFilters[$key]) && property_exists($customFilters[$key], 'name') ? $customFilters[$key]->name : ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}:
1313
@if(isset($customFilters[$key]) && method_exists($customFilters[$key], 'options'))
1414
@if(is_array($value))
1515
@foreach($value as $selectedValue)

resources/views/bootstrap-5/components/table/row.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
@props(['url' => null, 'target' => '_self', 'reordering' => false, 'customAttributes' => []])
22

3-
@if (!$reordering && $attributes->has('wire:sortable.item'))
3+
@if (!$reordering && (method_exists($attributes, 'has') ? $attributes->has('wire:sortable.item') : array_key_exists('wire:sortable.item', $attributes->getAttributes())))
44
@php
55
$attributes = $attributes->filter(fn ($value, $key) => $key !== 'wire:sortable.item');
66
@endphp

resources/views/bootstrap-5/includes/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
wire:key="filter-pill-{{ $key }}"
1010
class="badge rounded-pill bg-info d-inline-flex align-items-center"
1111
>
12-
{{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}:
12+
{{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, isset($customFilters[$key]) && property_exists($customFilters[$key], 'name') ? $customFilters[$key]->name : ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}:
1313
@if(isset($customFilters[$key]) && method_exists($customFilters[$key], 'options'))
1414
@if(is_array($value))
1515
@foreach($value as $selectedValue)

resources/views/tailwind/components/table/row.blade.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
@props(['url' => null, 'target' => '_self', 'reordering' => false, 'customAttributes' => []])
22

3-
@if (!$reordering && $attributes->has('wire:sortable.item'))
3+
@if (!$reordering && (method_exists($attributes, 'has') ? $attributes->has('wire:sortable.item') : array_key_exists('wire:sortable.item', $attributes->getAttributes())))
44
@php
55
$attributes = $attributes->filter(fn ($value, $key) => $key !== 'wire:sortable.item');
66
@endphp
77
@endif
88

99
<tr
10-
{{ $attributes->merge($customAttributes)->class(['cursor-pointer' => $url]) }}
10+
{{ $attributes->merge($customAttributes)->merge(['class' => $url ? 'cursor-pointer' : '']) }}
1111

1212
@if ($url)
1313
onclick="window.open('{{ $url }}', '{{ $target }}')"

resources/views/tailwind/includes/filter-pills.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
wire:key="filter-pill-{{ $key }}"
1010
class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium leading-4 bg-indigo-100 text-indigo-800 capitalize dark:bg-indigo-200 dark:text-indigo-900"
1111
>
12-
{{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}:
12+
{{ $filterNames[$key] ?? collect($this->columns())->pluck('text', 'column')->get($key, isset($customFilters[$key]) && property_exists($customFilters[$key], 'name') ? $customFilters[$key]->name : ucwords(strtr($key, ['_' => ' ', '-' => ' ']))) }}:
1313
@if(isset($customFilters[$key]) && method_exists($customFilters[$key], 'options'))
1414
@if(is_array($value))
1515
@foreach($value as $selectedValue)

tests/DataTableComponentTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace Rappasoft\LaravelLivewireTables\Tests;
44

55
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6+
use Illuminate\Encryption\Encrypter;
67
use Illuminate\Support\Collection;
8+
use Livewire\Livewire;
79
use Rappasoft\LaravelLivewireTables\DataTableComponent;
810
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsAltQueryTable;
911
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
@@ -115,6 +117,42 @@ public function search_filter_alt_query_relation()
115117
$this->assertEquals(2, $this->tableAltQuery->rows->total());
116118
}
117119

120+
/** @test */
121+
public function custom_filter()
122+
{
123+
$this->table->filters['species.name'] = 1;
124+
125+
$this->assertTrue($this->table->getRowsProperty()->where('name', 'Cartman')->isNotEmpty());
126+
$this->assertTrue($this->table->getRowsProperty()->where('name', 'Tux')->isNotEmpty());
127+
$this->assertFalse($this->table->getRowsProperty()->where('May', 'Tux')->isNotEmpty());
128+
$this->assertFalse($this->table->getRowsProperty()->where('Ben', 'Tux')->isNotEmpty());
129+
$this->assertFalse($this->table->getRowsProperty()->where('Chico', 'Tux')->isNotEmpty());
130+
}
131+
132+
/** @test */
133+
public function custom_filters_pills_label_use_column_name_when_possible()
134+
{
135+
config()->set('app.key', Encrypter::generateKey(config('app.cipher')));
136+
137+
Livewire::test(PetsTable::class)
138+
->set('filters', [
139+
'species.name' => 1,
140+
])
141+
->assertSeeTextInOrder(['Applied Filters:', 'Species:', 'Cat', 'Filters']);
142+
}
143+
144+
/** @test */
145+
public function custom_filters_pills_label_use_filter_name_when_is_not_bound_to_a_column()
146+
{
147+
config()->set('app.key', Encrypter::generateKey(config('app.cipher')));
148+
149+
Livewire::test(PetsTable::class)
150+
->set('filters', [
151+
'breed_id' => 1,
152+
])
153+
->assertSeeTextInOrder(['Applied Filters:', 'Filter Breed:', 'American Shorthair', 'Filters']);
154+
}
155+
118156
/** @test */
119157
public function bulk_actions_defined_through_function()
120158
{

tests/Http/Livewire/PetsTable.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,30 @@
66
use Rappasoft\LaravelLivewireTables\DataTableComponent;
77
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
88
use Rappasoft\LaravelLivewireTables\Views\Column;
9+
use Rappasoft\LaravelLivewireTables\Views\Filter;
910

1011
class PetsTable extends DataTableComponent
1112
{
13+
public function filters(): array
14+
{
15+
return [
16+
'species.name' => Filter::make('Filter Species')->select([
17+
'' => 'All',
18+
1 => 'Cat',
19+
2 => 'Dog',
20+
3 => 'Horse',
21+
4 => 'Bird',
22+
]),
23+
'breed_id' => Filter::make('Filter Breed')->select([
24+
'' => 'All',
25+
1 => 'American Shorthair',
26+
2 => 'Maine Coon',
27+
3 => 'Persian',
28+
4 => 'Norwegian Forest',
29+
]),
30+
];
31+
}
32+
1233
public function bulkActions(): array
1334
{
1435
return ['count' => 'Count selected'];
@@ -17,11 +38,13 @@ public function bulkActions(): array
1738
/**
1839
* @return Builder
1940
*/
20-
public function query() : Builder
41+
public function query(): Builder
2142
{
2243
return Pet::query()
2344
->with('species')
24-
->with('breed');
45+
->with('breed')
46+
->when($this->getFilter('species.name'), fn (Builder $query, $specimen_id) => $query->where('species_id', $specimen_id))
47+
->when($this->getFilter('breed_id'), fn (Builder $query, $breed_id) => $query->where('breed_id', $breed_id));
2548
}
2649

2750
public function columns(): array

0 commit comments

Comments
 (0)