Skip to content

Commit

Permalink
refactor relation table
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Nov 28, 2023
1 parent 868ca93 commit 9fd41dd
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 112 deletions.
2 changes: 1 addition & 1 deletion resources/views/resources/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
@can('create', $model)
<a href="{{ $url }}/create" class="btn btn--primary btn--icon">
<x-root::icon name="plus" class="btn__icon" />
{{ __('Add :model', ['model' => $modelName]) }}
{{ __('Add :resource', ['resource' => $modelName]) }}
</a>
@endcan
@endsection
Expand Down
27 changes: 26 additions & 1 deletion resources/views/resources/relation.blade.php
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
<turbo-frame id="relation-{{ $attribute }}">
@include('root::table.table')
<div class="app-card" x-data="table()">
<div class="app-card__header">
<h2 class="app-card__title">
<a href="{{ $url }}" data-turbo-frame="_top">{{ $title }}</a>
</h2>
<div class="app-card__actions">
@can('create', $model)
<a href="{{ $url }}/create" class="btn btn--primary btn--icon" data-turbo-frame="_top">
<x-root::icon name="plus" class="btn__icon" />
{{ __('Add :resource', ['resource' => $modelName]) }}
</a>
@endcan
@include('root::table.filters')
</div>
</div>
@include('root::table.body')
</div>

{{-- Script --}}
@pushOnce('scripts')
{{
Vite::withEntryPoints('resources/js/table.js')
->useBuildDirectory('vendor/root/build')
->useHotFile(public_path('vendor/root/hot'))
}}
@endpushOnce
</turbo-frame>
110 changes: 110 additions & 0 deletions resources/views/table/body.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<div class="app-card__body">
<div class="data-table">
@includeWhen(! empty($actions), 'root::table.actions')
@if($data->isNotEmpty())
<div class="table-responsive">
<table class="table table--hover">
<thead>
<tr>
@if(! empty($actions))
<th style="inline-size: 3.25rem;" scope="col">
<span class="sr-only">{{ __('Select') }}</span>
<label class="form-check" aria-label="{{ __('Select all items') }}">
<input
class="form-check__control"
type="checkbox"
x-on:change="selection = event.target.checked ? {{ $data->pluck('id')->toJson() }} : []"
>
</label>
</th>
@endif
@foreach($data[0]['fields'] as $column)
@include('root::table.column', $column)
@endforeach
<th scope="col">
<span class="sr-only">{{ __('Actions') }}</span>
</th>
</tr>
</thead>
<tbody>
@foreach($data as $row)
<tr>
@if(! empty($actions))
<td>
<label class="form-check" aria-label="">
<input
class="form-check__control"
type="checkbox"
value="{{ $row['id'] }}"
x-model="selection"
>
</label>
</td>
@endif
@foreach($row['fields'] as $cell)
@include('root::table.cell', $cell)
@endforeach
<td>
<div class="data-table__actions">
@can('view', $row['model'])
<a href="{{ $row['url'] }}" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('View') }}" data-turbo-frame="_top">
<x-root::icon name="eye" class="btn__icon" />
</a>
@endcan
@can('update', $row['model'])
<a href="{{ $row['url'] }}/edit" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('Edit') }}" data-turbo-frame="_top">
<x-root::icon name="edit" class="btn__icon" />
</a>
@endcan
@can('delete', $row['model'])
<form action="{{ $row['url'] }}" method="POST" onsubmit="return window.confirm('{{ __('Are you sure?') }}');">
@csrf
@method('DELETE')
<button type="submit" class="btn btn--delete btn--sm btn--icon" aria-label="{{ __('Delete') }}">
<x-root::icon name="trash" class="btn__icon" />
</button>
</form>
@endcan
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="data-table__footer">
<div class="data-table__footer-column">
<div class="form-group">
<label class="sr-only" for="per_page">
{{ __('Number of results') }}
</label>
<select
form="{{ $key }}"
class="form-control form-control--sm"
id="per_page"
name="{{ $perPageKey }}"
onchange="this.form.requestSubmit()"
>
@foreach($perPageOptions as $option)
<option value="{{ $option }}" @selected($option === $data->perPage())>
{{ $option }}
</option>
@endforeach
@if(! in_array($data->perPage(), $perPageOptions))
<option value="{{ $data->perPage() }}" selected>
{{ __('Custom (:perPage)', ['perPage' => $data->perPage()]) }}
</option>
@endif
</select>
</div>
<p>{{ __('Showing :from to :to of :total results', ['from' => $data->firstItem(), 'to' => $data->lastItem(), 'total' => $data->total()]) }}</p>
</div>
{!! $data->links('root::table.pagination') !!}
</div>
@else
<x-root::alert>
{{ __('No results found.') }}
</x-root::alert>
@endif
</div>
</div>
111 changes: 1 addition & 110 deletions resources/views/table/table.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,116 +5,7 @@
@include('root::table.filters')
</div>
</div>
<div class="app-card__body">
<div class="data-table">
@includeWhen(! empty($actions), 'root::table.actions')
@if($data->isNotEmpty())
<div class="table-responsive">
<table class="table table--hover">
<thead>
<tr>
@if(! empty($actions))
<th style="inline-size: 3.25rem;" scope="col">
<span class="sr-only">{{ __('Select') }}</span>
<label class="form-check" aria-label="{{ __('Select all items') }}">
<input
class="form-check__control"
type="checkbox"
x-on:change="selection = event.target.checked ? {{ $data->pluck('id')->toJson() }} : []"
>
</label>
</th>
@endif
@foreach($data[0]['fields'] as $column)
@include('root::table.column', $column)
@endforeach
<th scope="col">
<span class="sr-only">{{ __('Actions') }}</span>
</th>
</tr>
</thead>
<tbody>
@foreach($data as $row)
<tr>
@if(! empty($actions))
<td>
<label class="form-check" aria-label="">
<input
class="form-check__control"
type="checkbox"
value="{{ $row['id'] }}"
x-model="selection"
>
</label>
</td>
@endif
@foreach($row['fields'] as $cell)
@include('root::table.cell', $cell)
@endforeach
<td>
<div class="data-table__actions">
@can('view', $row['model'])
<a href="{{ $row['url'] }}" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('View') }}" data-turbo-frame="_top">
<x-root::icon name="eye" class="btn__icon" />
</a>
@endcan
@can('update', $row['model'])
<a href="{{ $row['url'] }}/edit" class="btn btn--light btn--sm btn--icon" aria-label="{{ __('Edit') }}" data-turbo-frame="_top">
<x-root::icon name="edit" class="btn__icon" />
</a>
@endcan
@can('delete', $row['model'])
<form action="{{ $row['url'] }}" method="POST" onsubmit="return window.confirm('{{ __('Are you sure?') }}');">
@csrf
@method('DELETE')
<button type="submit" class="btn btn--delete btn--sm btn--icon" aria-label="{{ __('Delete') }}">
<x-root::icon name="trash" class="btn__icon" />
</button>
</form>
@endcan
</div>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
<div class="data-table__footer">
<div class="data-table__footer-column">
<div class="form-group">
<label class="sr-only" for="per_page">
{{ __('Number of results') }}
</label>
<select
form="{{ $key }}"
class="form-control form-control--sm"
id="per_page"
name="{{ $perPageKey }}"
onchange="this.form.requestSubmit()"
>
@foreach($perPageOptions as $option)
<option value="{{ $option }}" @selected($option === $data->perPage())>
{{ $option }}
</option>
@endforeach
@if(! in_array($data->perPage(), $perPageOptions))
<option value="{{ $data->perPage() }}" selected>
{{ __('Custom (:perPage)', ['perPage' => $data->perPage()]) }}
</option>
@endif
</select>
</div>
<p>{{ __('Showing :from to :to of :total results', ['from' => $data->firstItem(), 'to' => $data->lastItem(), 'total' => $data->total()]) }}</p>
</div>
{!! $data->links('root::table.pagination') !!}
</div>
@else
<x-root::alert>
{{ __('No results found.') }}
</x-root::alert>
@endif
</div>
</div>
@include('root::table.body')
</div>

{{-- Script --}}
Expand Down

0 comments on commit 9fd41dd

Please sign in to comment.