yajra / laravel-datatables

jQuery DataTables API for Laravel
https://yajrabox.com/docs/laravel-datatables
MIT License
4.76k stars 857 forks source link

How to show/hide button action depending on value of table field ? #2089

Closed sergeynilov closed 1 year ago

sergeynilov commented 5 years ago

Hallo, In laravel 5.8 app I use yajra/laravel-datatables-oracle": "~8.0 and I need to show in 1 action 2 or 1 buttons, depending of status field value. 2) I define 2 buttons in my control :

            ->editColumn('action', '<a href="/admin/box-rooms/{{$id}}/edit" class="btn btn-default btn-sm btn-icon icon-left"><i class="fa fa-edit icon_dark"></i>Edit</a>' .
                                   '<a href="#" 
onclick="javascript:backendStorageSpace.storageSpaceShowFees({{$id}},\'{{$number}}\',\'{{$status}}\',\''.$current_currency_short.'\',\'{{$selling_range}}\',\'{{$insurance_vat}}\',\'{{$om_vat}}\',\'{{$electricity_vat}}\',\'{{$internet_vat}}\',\'{{$racks_vat}}\',\'{{$hot_desk_vat}}\')" class="btn btn-default btn-sm btn-icon icon-left mt-2"><i
class="fa fa-money-bill icon_dark"></i>Fees</a>')

and I need to show/hide "Fees" depending on $status value. Is there is such a way?

2) I can create 2 actions with different names, but again I did not find a way depending on $status value. Is it possible and if yes how?

Thanks!

yajra commented 5 years ago

You can use a closure that then builds your desired html or use a view:

->editColumn('action', function($user) {
  if ($user->active) {
   $action = 'active_html';
  }

  return $action;
})
yajra commented 5 years ago

Or if you prefer a polymorphic view:

->editColumn('action', function($user) {
  return view('users.actions.' . $user->status, compact('user'));
})
github-actions[bot] commented 1 year ago

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] commented 1 year ago

This issue was closed because it has been inactive for 7 days since being marked as stale.