Skip to content

Commit

Permalink
Use actions callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
eliurkis committed Apr 1, 2019
1 parent 3e7d38d commit e2ade1b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
24 changes: 22 additions & 2 deletions src/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class CrudController extends Controller
protected $listDisplay = [
'action-buttons' => true,
];
protected $customActions = [];
protected $actions = [];

public function __construct($entity, $config = [])
{
Expand All @@ -66,6 +66,26 @@ public function __construct($entity, $config = [])
$this->$key = $value;
}
}

$this->actions = [
'details' => function ($row, $route) {
return '<a href="'.route($route.'.show', $row->{$row->getKeyName()}).'" class="btn-default btn btn-xs">
<i class="fas fa-eye"></i>
</a>';
},
'edit' => function ($row, $route) {
return '<a href="'.route($route.'.edit', $row->{$row->getKeyName()}).'" class="btn-primary btn btn-xs edit_element">
<i class="far fa-edit"></i>
</a>';
},
'delete' => function ($row, $route) {
return '<a href="'.route($route.'.destroy', $row->{$row->getKeyName()}).'"
class="btn-danger btn btn-xs delete_element"
onclick="return confirm(\''.trans('eliurkis::crud.confirmation_delete').'\');">
<i class="far fa-trash-alt"></i>
</a>';
},
];
}

public function index(Request $request)
Expand Down Expand Up @@ -125,7 +145,7 @@ public function index(Request $request)
->with('listDisplay', $this->listDisplay)
->with('links', $this->prepareLinks())
->with('request', $request)
->with('customActions', $this->customActions)
->with('actions', $this->actions)
->with('route', $this->route);
}

Expand Down
21 changes: 4 additions & 17 deletions src/resources/views/list.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
@foreach($columns as $name)
<th class="field--{{ $name }}">{!! isset($fields[$name]['label']) ? __($fields[$name]['label']) : __(title_case(preg_replace("/[^A-Za-z0-9 ]/", ' ', $name))) !!}</th>
@endforeach
@if ($listDisplay['action-buttons'])
@if ($actions)
<th class="no-sort action-buttons" nowrap>{{ trans('eliurkis::crud.action') }}</th>
@endif
</tr>
Expand Down Expand Up @@ -75,23 +75,10 @@
@endif
</td>
@endforeach
@if ($listDisplay['action-buttons'])
@if ($actions)
<td class="action-buttons" nowrap>
@if (Route::has($route.'.show'))
<a href="{{ route($route.'.show', $row->{$row->getKeyName()}) }}" class="btn-default btn btn-xs"><i class="fas fa-eye"></i></a>
@endif
@if (Route::has($route.'.edit'))
<a href="{{ route($route.'.edit', $row->{$row->getKeyName()}) }}" class="btn-primary btn btn-xs edit_element"><i class="far fa-edit"></i></a>
@endif
@if (Route::has($route.'.destroy'))
<a href="{{ route($route.'.destroy', $row->{$row->getKeyName()}) }}"
class="btn-danger btn btn-xs delete_element"
onclick="return confirm('{{ $t['confirmation_delete'] or trans('eliurkis::crud.confirmation_delete') }}');">
<i class="far fa-trash-alt"></i>
</a>
@endif
@foreach($customActions as $actionCallback)
{!! $actionCallback($row) !!}
@foreach($actions as $actionCallback)
{!! $actionCallback($row, $route) !!}
@endforeach
</td>
@endif
Expand Down

0 comments on commit e2ade1b

Please sign in to comment.