Skip to content

Commit f76027e

Browse files
authored
Merge pull request #5667 from Laravel-Backpack/pr/5568
Allow developer to configure the behavior of actions buttons dropdown
2 parents 088bfc6 + 70e1775 commit f76027e

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

src/config/backpack/operations/list.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,16 @@
4949
// Nest action buttons within a dropdown in actions column
5050
'lineButtonsAsDropdown' => false,
5151

52+
// What is the minimum actions for the dropdown to be created
53+
// Example: when minimum to drop is «2», any row with less than «2» action buttons
54+
// will not create a dropdown, but will show the buttons inline
55+
'lineButtonsAsDropdownMinimum' => 1,
56+
57+
// Force «X» actions to be shown inline before the dropdown is created
58+
// Example: when setting this to «2», the first «2» actions will be shown inline
59+
// and the rest will be moved to the dropdown
60+
'lineButtonsAsDropdownShowBefore' => 0,
61+
5262
// Show a "Reset" button next to the List operation subheading
5363
// (Showing 1 to 25 of 9999 entries. Reset)
5464
// that allows the user to erase local storage for that datatable,

src/resources/views/crud/inc/datatables_logic.blade.php

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -401,26 +401,40 @@ function resizeCrudTableColumnWidths() {
401401
@endif
402402
403403
});
404-
404+
405405
function formatActionColumnAsDropdown() {
406406
// Get action column
407407
const actionColumnIndex = $('#crudTable').find('th[data-action-column=true]').index();
408408
if (actionColumnIndex === -1) return;
409409
410+
const minimumButtonsToBuildDropdown = $('#crudTable').data('line-buttons-as-dropdown-minimum');
411+
const buttonsToShowBeforeDropdown = $('#crudTable').data('line-buttons-as-dropdown-show-before-dropdown');
412+
410413
$('#crudTable tbody tr').each(function (i, tr) {
411414
const actionCell = $(tr).find('td').eq(actionColumnIndex);
412-
if(actionCell.find('.actions-buttons-column').length) return;
415+
const actionButtons = actionCell.find('a.btn.btn-link');
416+
if (actionCell.find('.actions-buttons-column').length) return;
417+
if (actionButtons.length < minimumButtonsToBuildDropdown) return;
413418
414-
// Wrap the cell with the component needed for the dropdown
415-
actionCell.wrapInner('<div class="nav-item dropdown"></div>');
416-
actionCell.wrapInner('<div class="dropdown-menu dropdown-menu-left"></div>');
417-
418419
// Prepare buttons as dropdown items
419-
actionCell.find('a.btn.btn-link').each((index, action) => {
420+
const dropdownItems = actionButtons.slice(buttonsToShowBeforeDropdown).map((index, action) => {
420421
$(action).addClass('dropdown-item').removeClass('btn btn-sm btn-link');
421422
$(action).find('i').addClass('me-2 text-primary');
423+
return action;
422424
});
423-
actionCell.prepend('<a class="btn btn-sm px-2 py-1 btn-outline-primary dropdown-toggle actions-buttons-column" href="#" data-toggle="dropdown" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">{{ trans('backpack::crud.actions') }}</a>');
425+
426+
// Only create dropdown if there are items to drop
427+
if (dropdownItems.length > 0) {
428+
// Wrap the cell with the component needed for the dropdown
429+
actionCell.wrapInner('<div class="nav-item dropdown"></div>');
430+
actionCell.wrapInner('<div class="dropdown-menu dropdown-menu-left"></div>');
431+
432+
actionCell.prepend('<a class="btn btn-sm px-2 py-1 btn-outline-primary dropdown-toggle actions-buttons-column" href="#" data-toggle="dropdown" data-bs-toggle="dropdown" data-bs-auto-close="outside" aria-expanded="false">{{ trans('backpack::crud.actions') }}</a>');
433+
434+
// Move the remaining buttons outside the dropdown
435+
const remainingButtons = actionButtons.slice(0, buttonsToShowBeforeDropdown);
436+
actionCell.prepend(remainingButtons);
437+
}
424438
});
425439
}
426440
</script>

src/resources/views/crud/list.blade.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class="{{ backpack_theme_config('classes.table') ?? 'table table-striped table-h
6262
data-has-details-row="{{ (int) $crud->getOperationSetting('detailsRow') }}"
6363
data-has-bulk-actions="{{ (int) $crud->getOperationSetting('bulkActions') }}"
6464
data-has-line-buttons-as-dropdown="{{ (int) $crud->getOperationSetting('lineButtonsAsDropdown') }}"
65+
data-line-buttons-as-dropdown-minimum="{{ (int) $crud->getOperationSetting('lineButtonsAsDropdownMinimum') }}"
66+
data-line-buttons-as-dropdown-show-before-dropdown="{{ (int) $crud->getOperationSetting('lineButtonsAsDropdownShowBefore') }}"
6567
cellspacing="0">
6668
<thead>
6769
<tr>

0 commit comments

Comments
 (0)