|
28 | 28 | <a href="{{ $crud->hasAccess('list') ? url($crud->route) : url()->previous() }}" class="btn btn-secondary text-decoration-none"><span class="la la-ban"></span> {{ trans('backpack::crud.cancel') }}</a> |
29 | 29 | @endif |
30 | 30 |
|
| 31 | + @if ($crud->get('update.showDeleteButton') && $crud->get('delete.configuration') && $crud->hasAccess('delete')) |
| 32 | + <button onclick="confirmAndDeleteEntry()" type="button" class="btn btn-danger float-right"><i class="la la-trash-alt"></i> {{ trans('backpack::crud.delete') }}</button> |
| 33 | + @endif |
31 | 34 | </div> |
32 | 35 | @endif |
33 | 36 |
|
@@ -108,4 +111,76 @@ function changeTabIfNeededAndDisplayErrors(form) { |
108 | 111 | }); |
109 | 112 | }); |
110 | 113 | </script> |
| 114 | + |
| 115 | +@if ($crud->get('update.showDeleteButton') && $crud->get('delete.configuration') && $crud->hasAccess('delete')) |
| 116 | +<script> |
| 117 | + function confirmAndDeleteEntry() { |
| 118 | + // Ask for confirmation before deleting an item |
| 119 | + swal({ |
| 120 | + title: "{!! trans('backpack::base.warning') !!}", |
| 121 | + text: "{!! trans('backpack::crud.delete_confirm') !!}", |
| 122 | + icon: "warning", |
| 123 | + buttons: ["{!! trans('backpack::crud.cancel') !!}", "{!! trans('backpack::crud.delete') !!}"], |
| 124 | + dangerMode: true, |
| 125 | + }).then((value) => { |
| 126 | + if (value) { |
| 127 | + $.ajax({ |
| 128 | + url: '{{ url($crud->route.'/'.$entry->getKey()) }}', |
| 129 | + type: 'DELETE', |
| 130 | + success: function(result) { |
| 131 | + if (result !== '1') { |
| 132 | + // if the result is an array, it means |
| 133 | + // we have notification bubbles to show |
| 134 | + if (result instanceof Object) { |
| 135 | + // trigger one or more bubble notifications |
| 136 | + Object.entries(result).forEach(function(entry) { |
| 137 | + var type = entry[0]; |
| 138 | + entry[1].forEach(function(message, i) { |
| 139 | + new Noty({ |
| 140 | + type: type, |
| 141 | + text: message |
| 142 | + }).show(); |
| 143 | + }); |
| 144 | + }); |
| 145 | + } else { // Show an error alert |
| 146 | + swal({ |
| 147 | + title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}", |
| 148 | + text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}", |
| 149 | + icon: "error", |
| 150 | + timer: 4000, |
| 151 | + buttons: false, |
| 152 | + }); |
| 153 | + } |
| 154 | + } |
| 155 | + // All is good, show a success message! |
| 156 | + swal({ |
| 157 | + title: "{!! trans('backpack::crud.delete_confirmation_title') !!}", |
| 158 | + text: "{!! trans('backpack::crud.delete_confirmation_message') !!}", |
| 159 | + icon: "success", |
| 160 | + buttons: false, |
| 161 | + closeOnClickOutside: false, |
| 162 | + closeOnEsc: false, |
| 163 | + }); |
| 164 | +
|
| 165 | + // Redirect in 1 sec so that admins get to see the success message |
| 166 | + setTimeout(function () { |
| 167 | + window.location.href = '{{ is_bool($crud->get('update.showDeleteButton')) ? url($crud->route) : (string) $crud->get('update.showDeleteButton') }}'; |
| 168 | + }, 1000); |
| 169 | + }, |
| 170 | + error: function() { |
| 171 | + // Show an alert with the result |
| 172 | + swal({ |
| 173 | + title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}", |
| 174 | + text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}", |
| 175 | + icon: "error", |
| 176 | + timer: 4000, |
| 177 | + buttons: false, |
| 178 | + }); |
| 179 | + } |
| 180 | + }); |
| 181 | + } |
| 182 | + }); |
| 183 | + } |
| 184 | +</script> |
| 185 | +@endif |
111 | 186 | @endpush |
0 commit comments