Skip to content

Commit

Permalink
Using @forelse to add message when the table has no resources to disp…
Browse files Browse the repository at this point in the history
…lay (#267)

Co-authored-by: Cristian Iosif <cristian.iosif@sware.com>
Co-authored-by: Pascal Baljet <pascal@pascalbaljet.nl>
  • Loading branch information
3 people authored Feb 28, 2023
1 parent 178816f commit b543f3e
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 6 deletions.
13 changes: 13 additions & 0 deletions app/app/Http/Controllers/TableController.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,19 @@ public function caseInsensitive()
]);
}

public function empty()
{
SpladeTable::hidePaginationWhenResourceContainsOnePage();

return view('table.users', [
'users' => SpladeTable::for(User::query()->where('id', '<', 1))
->withGlobalSearch(columns: ['name'])
->column('name')
->ignoreCase(true)
->paginate(10),
]);
}

public function overflow(bool $spladeQueryBuilder = false)
{
$query = User::query()->orderBy('name');
Expand Down
2 changes: 2 additions & 0 deletions app/routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,8 @@
Route::get('/caseSensitive/{spladeQueryBuilder?}', [TableController::class, 'caseSensitive'])->name('table.caseSensitive');
Route::get('/caseInsensitive/{spladeQueryBuilder?}', [TableController::class, 'caseInsensitive'])->name('table.caseInsensitive');

Route::get('/empty', [TableController::class, 'empty'])->name('table.empty');

Route::get('/preserveScrollForm', [TableController::class, 'preserveScrollForm'])->name('table.preserveScrollForm');
Route::post('/preserveScrollForm', [TableController::class, 'preserveScrollFormSubmit'])->name('table.preserveScrollFormSubmit');

Expand Down
24 changes: 24 additions & 0 deletions app/tests/Browser/Table/EmptyTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Tests\Browser\Table;

use Laravel\Dusk\Browser;
use Tests\DuskTestCase;

/**
* @group table
*/
class EmptyTest extends DuskTestCase
{
/**
* @test
*/
public function it_shows_a_text_when_no_results_have_been_found()
{
$this->browse(function (Browser $browser) {
$browser
->visit('/table/empty/')
->assertSee('There are no items to show.');
});
}
}
2 changes: 2 additions & 0 deletions resources/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"Search": "",
"Select all on this page": "",
"Select all results": "",
"The password confirmation has expired.": "",
"There are no items to show.": "",
"of": "",
"per page": "",
"results": "",
Expand Down
18 changes: 15 additions & 3 deletions resources/views/table/body.blade.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<tbody class="divide-y divide-gray-200 bg-white">
@foreach($table->resource as $itemKey => $item)
@forelse($table->resource as $itemKey => $item)
@php $itemPrimaryKey = $table->findPrimaryKey($item) @endphp

<tr
Expand Down Expand Up @@ -39,5 +39,17 @@ class="whitespace-nowrap text-sm @if($loop->first && $hasBulkActions) pr-6 @else
</td>
@endforeach
</tr>
@endforeach
</tbody>
@empty
<tr>
<td colspan="{{ $table->columns()->count() }}" class="whitespace-nowrap">
@if(isset($emptyState) && !!$emptyState)
{{ $emptyState }}
@else
<p class="text-gray-700 px-6 py-12 font-medium text-sm text-center">
{{ __('There are no items to show.') }}
</p>
@endif
</td>
</tr>
@endforelse
</tbody>
11 changes: 8 additions & 3 deletions src/Components/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,17 @@ public function __construct(
*/
public function showPaginator(): bool
{
if (SpladeTable::hidesPaginationWhenResourceContainsOnePage()) {
$resource = $this->for->resource;

$paginator = $resource instanceof Paginator || $resource instanceof CursorPaginator;

if (!$paginator) {
return false;
}

return $this->for->resource instanceof Paginator
|| $this->for->resource instanceof CursorPaginator;
return SpladeTable::hidesPaginationWhenResourceContainsOnePage()
? $resource->hasPages()
: true;
}

/**
Expand Down

0 comments on commit b543f3e

Please sign in to comment.