Skip to content

Commit 62969bc

Browse files
committed
add DropAfter option to line buttons
1 parent f97a606 commit 62969bc

File tree

3 files changed

+31
-13
lines changed

3 files changed

+31
-13
lines changed

src/config/backpack/operations/list.php

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

52-
// What is the minimum threshold of action buttons for nesting into a dropdown
53-
'lineButtonsAsDropdownThreshold' => 1,
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+
'lineButtonsAsDropdownMinimumToDrop' => 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+
'lineButtonsAsDropdownDropAfter' => 0,
5461

5562
// Show a "Reset" button next to the List operation subheading
5663
// (Showing 1 to 25 of 9999 entries. Reset)

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -401,30 +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 minimumThreshold = $('#crudTable').data('line-buttons-as-dropdown-threshold');
410+
const minimumToDrop = $('#crudTable').data('line-buttons-as-dropdown-minimum-to-drop');
411+
const dropAfter = $('#crudTable').data('line-buttons-as-dropdown-drop-after');
411412
412413
$('#crudTable tbody tr').each(function (i, tr) {
413414
const actionCell = $(tr).find('td').eq(actionColumnIndex);
414415
const actionButtons = actionCell.find('a.btn.btn-link');
415-
if(actionCell.find('.actions-buttons-column').length) return;
416-
if(actionButtons.length < minimumThreshold) return;
416+
if (actionCell.find('.actions-buttons-column').length) return;
417+
if (actionButtons.length < minimumToDrop) return;
417418
418-
// Wrap the cell with the component needed for the dropdown
419-
actionCell.wrapInner('<div class="nav-item dropdown"></div>');
420-
actionCell.wrapInner('<div class="dropdown-menu dropdown-menu-left"></div>');
421-
422419
// Prepare buttons as dropdown items
423-
actionButtons.each((index, action) => {
420+
const dropdownItems = actionButtons.slice(dropAfter).map((index, action) => {
424421
$(action).addClass('dropdown-item').removeClass('btn btn-sm btn-link');
425422
$(action).find('i').addClass('me-2 text-primary');
423+
return action;
426424
});
427-
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, dropAfter);
436+
actionCell.prepend(remainingButtons);
437+
}
428438
});
429439
}
430440
</script>

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +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-threshold="{{ (int) $crud->getOperationSetting('lineButtonsAsDropdownThreshold') }}"
65+
data-line-buttons-as-dropdown-minimum-to-drop="{{ (int) $crud->getOperationSetting('lineButtonsAsDropdownMinimumToDrop') }}"
66+
data-line-buttons-as-dropdown-drop-after="{{ (int) $crud->getOperationSetting('lineButtonsAsDropdownDropAfter') }}"
6667
cellspacing="0">
6768
<thead>
6869
<tr>

0 commit comments

Comments
 (0)