|
25 | 25 | $wrapper['class'] = $wrapper['class'] ?? $defaultClass; |
26 | 26 | //if ajax enabled |
27 | 27 | $buttonAjaxConfiguration = $button->meta['ajax'] ?? false; |
| 28 | + $bulkConfiguration = $button->meta['bulk'] ?? false; |
28 | 29 | if($buttonAjaxConfiguration) { |
29 | 30 | $wrapper['data-route'] = $wrapper['href']; |
30 | 31 | $wrapper['data-method'] = $button->meta['ajax']['method'] ?? 'GET'; |
31 | 32 | $wrapper['data-refresh-table'] = $button->meta['ajax']['refreshCrudTable'] ?? false; |
32 | 33 |
|
33 | 34 | $wrapper['href'] = 'javascript:void(0)'; |
34 | | - $wrapper['onclick'] = 'sendQuickButtonAjaxRequest(this)'; |
35 | | - $wrapper['data-button-type'] = 'quick-ajax'; |
| 35 | +
|
| 36 | + // Check if this is a bulk button |
| 37 | + if($bulkConfiguration) { |
| 38 | + $wrapper['onclick'] = 'sendQuickBulkButtonAjaxRequest(this)'; |
| 39 | + $wrapper['data-button-type'] = 'quick-bulk-ajax'; |
| 40 | + $wrapper['class'] = $wrapper['class'] . ' bulk-button'; |
| 41 | +
|
| 42 | + // Bulk-specific messages |
| 43 | + $wrapper['data-bulk-no-entries-title'] = $button->meta['bulk']['no_entries_title'] ?? trans('backpack::crud.bulk_no_entries_selected_title'); |
| 44 | + $wrapper['data-bulk-no-entries-message'] = $button->meta['bulk']['no_entries_message'] ?? trans('backpack::crud.bulk_no_entries_selected_message'); |
| 45 | + $wrapper['data-bulk-confirm-title'] = $button->meta['bulk']['confirm_title'] ?? trans('backpack::base.warning'); |
| 46 | + $wrapper['data-bulk-confirm-message'] = $button->meta['bulk']['confirm_message'] ?? trans('backpack::crud.bulk_operation_are_you_sure'); |
| 47 | + } else { |
| 48 | + $wrapper['onclick'] = 'sendQuickButtonAjaxRequest(this)'; |
| 49 | + $wrapper['data-button-type'] = 'quick-ajax'; |
| 50 | + } |
36 | 51 |
|
37 | 52 | //success message |
38 | 53 | $wrapper['data-success-title'] = $button->meta['ajax']['success_title'] ?? trans('backpack::crud.quick_button_ajax_success_title'); |
@@ -117,6 +132,106 @@ function sendQuickButtonAjaxRequest(button) { |
117 | 132 | }); |
118 | 133 | } |
119 | 134 | } |
| 135 | +
|
| 136 | + if (typeof sendQuickBulkButtonAjaxRequest != 'function') { |
| 137 | + $("[data-button-type=quick-bulk-ajax]").unbind('click'); |
| 138 | +
|
| 139 | + function sendQuickBulkButtonAjaxRequest(button) { |
| 140 | + // Check if items are selected |
| 141 | + if (typeof crud.checkedItems === 'undefined' || crud.checkedItems.length == 0) { |
| 142 | + let noEntriesTitle = $(button).attr('data-bulk-no-entries-title'); |
| 143 | + let noEntriesMessage = $(button).attr('data-bulk-no-entries-message'); |
| 144 | +
|
| 145 | + new Noty({ |
| 146 | + type: "warning", |
| 147 | + text: `<strong>${noEntriesTitle}</strong><br/>${noEntriesMessage}` |
| 148 | + }).show(); |
| 149 | +
|
| 150 | + return; |
| 151 | + } |
| 152 | +
|
| 153 | + let route = $(button).attr('data-route'); |
| 154 | + let confirmTitle = $(button).attr('data-bulk-confirm-title'); |
| 155 | + let confirmMessage = $(button).attr('data-bulk-confirm-message').replace(':number', crud.checkedItems.length); |
| 156 | +
|
| 157 | + const defaultButtonMessage = function(button, type) { |
| 158 | + let buttonTitle = button.getAttribute(`data-${type}-title`); |
| 159 | + let buttonMessage = button.getAttribute(`data-${type}-message`); |
| 160 | + return `<strong>${buttonTitle}</strong><br/>${buttonMessage}`; |
| 161 | + } |
| 162 | +
|
| 163 | + // Show confirmation dialog |
| 164 | + swal({ |
| 165 | + title: confirmTitle, |
| 166 | + text: confirmMessage, |
| 167 | + icon: "warning", |
| 168 | + buttons: { |
| 169 | + cancel: { |
| 170 | + text: "{{ trans('backpack::crud.no') }}", |
| 171 | + value: null, |
| 172 | + visible: true, |
| 173 | + className: "bg-secondary", |
| 174 | + closeModal: true, |
| 175 | + }, |
| 176 | + confirm: { |
| 177 | + text: "{{ trans('backpack::crud.yes') }}", |
| 178 | + value: true, |
| 179 | + visible: true, |
| 180 | + className: "bg-primary", |
| 181 | + } |
| 182 | + }, |
| 183 | + }).then((value) => { |
| 184 | + if (value) { |
| 185 | + // Submit AJAX request with selected items |
| 186 | + $.ajax({ |
| 187 | + url: route, |
| 188 | + type: $(button).attr('data-method'), |
| 189 | + data: { entries: crud.checkedItems }, |
| 190 | + success: function(result) { |
| 191 | + if($(button).attr('data-refresh-table') && typeof crud != 'undefined' && typeof crud.table != 'undefined'){ |
| 192 | + // Move to previous page if all items on current page were processed |
| 193 | + if(crud.table.rows().count() === crud.checkedItems.length) { |
| 194 | + crud.table.page("previous"); |
| 195 | + } |
| 196 | +
|
| 197 | + // Clear selections and refresh table |
| 198 | + crud.checkedItems = []; |
| 199 | + crud.table.draw(false); |
| 200 | + } |
| 201 | +
|
| 202 | + let message; |
| 203 | + // If message is returned from the API use that message |
| 204 | + if(result.message){ |
| 205 | + message = result.message; |
| 206 | + } else { |
| 207 | + message = defaultButtonMessage(button, 'success'); |
| 208 | + } |
| 209 | +
|
| 210 | + new Noty({ |
| 211 | + type: "success", |
| 212 | + text: message, |
| 213 | + }).show(); |
| 214 | + }, |
| 215 | + error: function(result) { |
| 216 | + let message; |
| 217 | +
|
| 218 | + // If message is returned from the API use that message |
| 219 | + if(result.responseJSON && result.responseJSON.message){ |
| 220 | + message = result.responseJSON.message; |
| 221 | + } else { |
| 222 | + message = defaultButtonMessage(button, 'error'); |
| 223 | + } |
| 224 | +
|
| 225 | + new Noty({ |
| 226 | + type: "error", |
| 227 | + text: message, |
| 228 | + }).show(); |
| 229 | + } |
| 230 | + }); |
| 231 | + } |
| 232 | + }); |
| 233 | + } |
| 234 | + } |
120 | 235 | </script> |
121 | 236 | @endBassetBlock |
122 | 237 | @if (!request()->ajax()) @endpush @endif |
|
0 commit comments