Datagrid actions enhancement, now action can have a key called condition which must return boolean in order to trigger that action's visibility

This commit is contained in:
Webkul 2019-10-10 13:53:36 +05:30
parent 3819e6e2ed
commit 9e2e73d0b3
2 changed files with 27 additions and 18 deletions

View File

@ -130,6 +130,9 @@ class ProductDataGrid extends DataGrid
public function prepareActions() {
$this->addAction([
'title' => 'Edit Product',
'condition' => function() {
return true;
},
'method' => 'GET', // use GET request only for redirect purposes
'route' => 'admin.catalog.products.edit',
'icon' => 'icon pencil-lg-icon'
@ -149,7 +152,7 @@ class ProductDataGrid extends DataGrid
public function prepareMassActions() {
$this->addMassAction([
'type' => 'delete',
'label' => 'Delete',
'label' => 'Delete',
'action' => route('admin.catalog.products.massdelete'),
'method' => 'DELETE'
]);

View File

@ -42,24 +42,30 @@
<td class="actions" style="width: 100px;" data-value="{{ __('ui::app.datagrid.actions') }}">
<div class="action">
@foreach ($actions as $action)
<a
@if ($action['method'] == 'GET')
href="{{ route($action['route'], $record->{$action['index'] ?? $index}) }}"
@php
$toDisplay = (isset($action['condition']) && gettype($action['condition']) == 'object') ? $action['condition']() : true;
@endphp
@if ($toDisplay)
<a
@if ($action['method'] == 'GET')
href="{{ route($action['route'], $record->{$action['index'] ?? $index}) }}"
@endif
@if ($action['method'] != 'GET')
v-on:click="doAction($event)"
@endif
data-method="{{ $action['method'] }}"
data-action="{{ route($action['route'], $record->{$index}) }}"
data-token="{{ csrf_token() }}"
@if (isset($action['title']))
title="{{ $action['title'] }}"
@endif>
<span class="{{ $action['icon'] }}"></span>
</a>
@endif
@if ($action['method'] != 'GET')
v-on:click="doAction($event)"
@endif
data-method="{{ $action['method'] }}"
data-action="{{ route($action['route'], $record->{$index}) }}"
data-token="{{ csrf_token() }}"
@if (isset($action['title']))
title="{{ $action['title'] }}"
@endif>
<span class="{{ $action['icon'] }}"></span>
</a>
@endforeach
</div>
</td>