|
| 1 | + |
| 2 | +@php |
| 3 | + $redirectUrl = $crud->getOperationSetting('deleteButtonRedirect'); |
| 4 | + if($redirectUrl && $redirectUrl instanceof \Closure){ |
| 5 | + $redirectUrl = $redirectUrl(); |
| 6 | + } |
| 7 | + $redirectUrl = filter_var($redirectUrl, FILTER_VALIDATE_URL) ? $redirectUrl : null; |
| 8 | +@endphp |
| 9 | + |
1 | 10 | @if ($crud->hasAccess('delete', $entry)) |
2 | | - <a href="javascript:void(0)" onclick="deleteEntry(this)" bp-button="delete" data-route="{{ url($crud->route.'/'.$entry->getKey()) }}" class="btn btn-sm btn-link" data-button-type="delete"> |
| 11 | + <a href="javascript:void(0)" |
| 12 | + onclick="deleteEntry(this)" |
| 13 | + bp-button="delete" |
| 14 | + data-redirect-route="{{ $redirectUrl }}" |
| 15 | + data-route="{{ url($crud->route.'/'.$entry->getKey()) }}" |
| 16 | + data-table-id="{{ isset($crudTableId) ? $crudTableId : 'crudTable' }}" |
| 17 | + class="btn btn-sm btn-link" |
| 18 | + data-button-type="delete" |
| 19 | + > |
3 | 20 | <i class="la la-trash"></i> <span>{{ trans('backpack::crud.delete') }}</span> |
4 | 21 | </a> |
5 | 22 | @endif |
|
11 | 28 | @bassetBlock('backpack/crud/buttons/delete-button-'.app()->getLocale().'.js') |
12 | 29 | <script> |
13 | 30 |
|
14 | | - if (typeof deleteEntry != 'function') { |
15 | | - $("[data-button-type=delete]").unbind('click'); |
| 31 | + if (typeof deleteEntry != 'function') { |
| 32 | + $("[data-button-type=delete]").unbind('click'); |
16 | 33 |
|
17 | | - function deleteEntry(button) { |
18 | | - // ask for confirmation before deleting an item |
19 | | - // e.preventDefault(); |
20 | | - var route = $(button).attr('data-route'); |
| 34 | + function deleteEntry(button) { |
| 35 | + // ask for confirmation before deleting an item |
| 36 | + // e.preventDefault(); |
| 37 | + var route = $(button).attr('data-route'); |
21 | 38 |
|
22 | | - swal({ |
23 | | - title: "{!! trans('backpack::base.warning') !!}", |
24 | | - text: "{!! trans('backpack::crud.delete_confirm') !!}", |
25 | | - icon: "warning", |
26 | | - buttons: { |
27 | | - cancel: { |
28 | | - text: "{!! trans('backpack::crud.cancel') !!}", |
29 | | - value: null, |
30 | | - visible: true, |
31 | | - className: "bg-secondary", |
32 | | - closeModal: true, |
33 | | - }, |
34 | | - delete: { |
35 | | - text: "{!! trans('backpack::crud.delete') !!}", |
36 | | - value: true, |
37 | | - visible: true, |
38 | | - className: "bg-danger", |
39 | | - }, |
40 | | - }, |
41 | | - dangerMode: true, |
42 | | - }).then((value) => { |
43 | | - if (value) { |
44 | | - $.ajax({ |
45 | | - url: route, |
46 | | - type: 'DELETE', |
47 | | - success: function(result) { |
48 | | - if (result == 1) { |
49 | | - // Redraw the table |
50 | | - if (typeof crud != 'undefined' && typeof crud.table != 'undefined') { |
51 | | - // Move to previous page in case of deleting the only item in table |
52 | | - if(crud.table.rows().count() === 1) { |
53 | | - crud.table.page("previous"); |
54 | | - } |
| 39 | + swal({ |
| 40 | + title: "{!! trans('backpack::base.warning') !!}", |
| 41 | + text: "{!! trans('backpack::crud.delete_confirm') !!}", |
| 42 | + icon: "warning", |
| 43 | + buttons: { |
| 44 | + cancel: { |
| 45 | + text: "{!! trans('backpack::crud.cancel') !!}", |
| 46 | + value: null, |
| 47 | + visible: true, |
| 48 | + className: "bg-secondary", |
| 49 | + closeModal: true, |
| 50 | + }, |
| 51 | + delete: { |
| 52 | + text: "{!! trans('backpack::crud.delete') !!}", |
| 53 | + value: true, |
| 54 | + visible: true, |
| 55 | + className: "bg-danger", |
| 56 | + }, |
| 57 | + }, |
| 58 | + dangerMode: true, |
| 59 | + }).then((value) => { |
| 60 | + function showDeleteNotyAlert() { |
| 61 | + // Show a success notification bubble |
| 62 | + new Noty({ |
| 63 | + type: "success", |
| 64 | + text: "{!! '<strong>'.trans('backpack::crud.delete_confirmation_title').'</strong><br>'.trans('backpack::crud.delete_confirmation_message') !!}" |
| 65 | + }).show(); |
| 66 | + } |
| 67 | + if (value) { |
| 68 | + $.ajax({ |
| 69 | + url: route, |
| 70 | + type: 'DELETE', |
| 71 | + success: function(result) { |
| 72 | + if (result == 1) { |
| 73 | + // Get the table ID from the button's data attribute |
| 74 | + let tableId = $(button).data('table-id') || 'crudTable'; |
| 75 | + |
| 76 | + // Check if we have a specific DataTable instance |
| 77 | + if (typeof window.crud !== 'undefined' && |
| 78 | + typeof window.crud.tables !== 'undefined' && |
| 79 | + window.crud.tables[tableId]) { |
| 80 | + |
| 81 | + let table = window.crud.tables[tableId]; |
| 82 | + |
| 83 | + // Move to previous page in case of deleting the only item in table |
| 84 | + if(table.rows().count() === 1) { |
| 85 | + table.page("previous"); |
| 86 | + } |
| 87 | + // Hide the modal, if any is displayed |
| 88 | + $('.dtr-modal-close').click(); |
55 | 89 |
|
56 | | - crud.table.draw(false); |
57 | | - } |
| 90 | + showDeleteNotyAlert(); |
| 91 | + table.draw(false); |
| 92 | + } else { |
| 93 | + // there is no crud table in the current page, so we will redirect the user to the defined button redirect route in data-redirect-url |
| 94 | + let redirectRoute = $(button).data('redirect-route'); |
| 95 | + if(redirectRoute){ |
| 96 | + // queue the alert in localstorage to show it after the redirect |
| 97 | + localStorage.setItem('backpack_alerts', JSON.stringify({ |
| 98 | + 'success': [ |
| 99 | + "{!! '<strong>'.trans('backpack::crud.delete_confirmation_title').'</strong><br>'.trans('backpack::crud.delete_confirmation_message') !!}" |
| 100 | + ] |
| 101 | + })); |
| 102 | + window.location.href = redirectRoute; |
| 103 | + } else { |
| 104 | + // Show a success notification bubble, keep the previous behaviour of not redirecting |
| 105 | + // and keeping the entry open after deletion |
| 106 | + showDeleteNotyAlert(); |
| 107 | + } |
| 108 | + } |
| 109 | + } else { |
| 110 | + // if the result is an array, it means |
| 111 | + // we have notification bubbles to show |
| 112 | + if (result instanceof Object) { |
| 113 | + // trigger one or more bubble notifications |
| 114 | + Object.entries(result).forEach(function(entry, index) { |
| 115 | + var type = entry[0]; |
| 116 | + entry[1].forEach(function(message, i) { |
| 117 | + new Noty({ |
| 118 | + type: type, |
| 119 | + text: message |
| 120 | + }).show(); |
| 121 | + }); |
| 122 | + }); |
| 123 | + } else { |
| 124 | + // Show an error alert |
| 125 | + swal({ |
| 126 | + title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}", |
| 127 | + text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}", |
| 128 | + icon: "error", |
| 129 | + timer: 4000, |
| 130 | + buttons: false, |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
| 134 | + }, |
| 135 | + error: function(result) { |
| 136 | + // Show an alert with the result |
| 137 | + swal({ |
| 138 | + title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}", |
| 139 | + text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}", |
| 140 | + icon: "error", |
| 141 | + timer: 4000, |
| 142 | + buttons: false, |
| 143 | + }); |
| 144 | + } |
| 145 | + }); |
| 146 | + } |
| 147 | + }); |
58 | 148 |
|
59 | | - // Show a success notification bubble |
60 | | - new Noty({ |
61 | | - type: "success", |
62 | | - text: "{!! '<strong>'.trans('backpack::crud.delete_confirmation_title').'</strong><br>'.trans('backpack::crud.delete_confirmation_message') !!}" |
63 | | - }).show(); |
64 | | -
|
65 | | - // Hide the modal, if any |
66 | | - $('.modal').modal('hide'); |
67 | | - } else { |
68 | | - // if the result is an array, it means |
69 | | - // we have notification bubbles to show |
70 | | - if (result instanceof Object) { |
71 | | - // trigger one or more bubble notifications |
72 | | - Object.entries(result).forEach(function(entry, index) { |
73 | | - var type = entry[0]; |
74 | | - entry[1].forEach(function(message, i) { |
75 | | - new Noty({ |
76 | | - type: type, |
77 | | - text: message |
78 | | - }).show(); |
79 | | - }); |
80 | | - }); |
81 | | - } else {// Show an error alert |
82 | | - swal({ |
83 | | - title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}", |
84 | | - text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}", |
85 | | - icon: "error", |
86 | | - timer: 4000, |
87 | | - buttons: false, |
88 | | - }); |
89 | | - } |
90 | | - } |
91 | | - }, |
92 | | - error: function(result) { |
93 | | - // Show an alert with the result |
94 | | - swal({ |
95 | | - title: "{!! trans('backpack::crud.delete_confirmation_not_title') !!}", |
96 | | - text: "{!! trans('backpack::crud.delete_confirmation_not_message') !!}", |
97 | | - icon: "error", |
98 | | - timer: 4000, |
99 | | - buttons: false, |
100 | | - }); |
101 | | - } |
102 | | - }); |
103 | | - } |
104 | | - }); |
105 | | -
|
106 | | - } |
107 | | - } |
| 149 | + } |
| 150 | + } |
108 | 151 |
|
109 | | - // make it so that the function above is run after each DataTable draw event |
110 | | - // crud.addFunctionToDataTablesDrawEventQueue('deleteEntry'); |
| 152 | + // make it so that the function above is run after each DataTable draw event |
| 153 | + // crud.addFunctionToDataTablesDrawEventQueue('deleteEntry'); |
111 | 154 | </script> |
112 | 155 | @endBassetBlock |
113 | 156 | @if (!request()->ajax()) @endpush @endif |
0 commit comments