Skip to content

A free open source IT asset/license management system

Added dynamic dropdowns for bulk actions

snipea9a23779f866
diff --git a/resources/views/blade/table/bulk-actions.blade.php b/resources/views/blade/table/bulk-actions.blade.php
index a7685c607a..24d81c523d 100644
--- a/resources/views/blade/table/bulk-actions.blade.php
+++ b/resources/views/blade/table/bulk-actions.blade.php
@@ -1,20 +1,27 @@
@props([
'action_route',
'model_name' => 'asset',
+ 'actions' => null,
])
@aware(['name'])
-<div id="{{ Illuminate\Support\Str::camel($name) }}Form" style="min-width:400px" class="hidden-print">
+@php
+ $formId = Illuminate\Support\Str::camel($name) . 'Form';
+ $buttonId = Illuminate\Support\Str::camel($name) . 'Button';
+ $selectId = Illuminate\Support\Str::camel($name) . 'Select';
+@endphp
+
+<div id="{{ $formId }}" style="min-width:400px" class="hidden-print">
<form
method="POST"
action="{{ $action_route }}"
accept-charset="UTF-8"
class="form-inline"
- id="{{ Illuminate\Support\Str::camel($name) }}Form"
+ id="{{ $formId }}"
>
@csrf
- {{-- The sort and order will only be used if the cookie is actually empty (like on first-use)--}}
+ {{-- sort/order only used on first-use before cookie is set --}}
<input name="sort" type="hidden" value="{{ "{$model_name}.id" }}">
<input name="order" type="hidden" value="asc">
<label for="bulk_actions">
@@ -22,10 +29,24 @@
{{ trans('button.bulk_actions') }}
</span>
</label>
- <select name="bulk_actions" class="form-control select2" aria-label="bulk_actions" style="min-width: 350px;">
- {{ $slot }}
+ <select
+ id="{{ $selectId }}"
+ name="bulk_actions"
+ class="form-control select2"
+ aria-label="bulk_actions"
+ style="min-width: 350px;"
+ @if ($actions)
+ data-dynamic-actions="{{ json_encode($actions) }}"
+ data-placeholder="{{ trans('general.bulk_actions_selection_prompt') }}"
+ @endif
+ >
+ @if ($actions)
+ <option value=""></option>
+ @else
+ {{ $slot }}
+ @endif
</select>
- <button class="btn btn-theme" id="{{ Illuminate\Support\Str::camel($name) }}Button" disabled>{{ trans('button.go') }}</button>
+ <button class="btn btn-theme" id="{{ $buttonId }}" disabled>{{ trans('button.go') }}</button>
</form>
</div>
diff --git a/resources/views/licenses/index.blade.php b/resources/views/licenses/index.blade.php
index 01406a0a7f..310f5fa3ce 100755
--- a/resources/views/licenses/index.blade.php
+++ b/resources/views/licenses/index.blade.php
@@ -13,15 +13,14 @@
<x-box>
<x-slot:bulkactions>
- <x-table.bulk-actions
- name='licenses'
- action_route="{{ route('licenses.bulk.delete') }}"
- model_name="license"
- >
- @can('delete', App\Models\License::class)
- <option value="delete">{{ trans('general.delete') }}</option>
- @endcan
- </x-table.bulk-actions>
+ @can('delete', App\Models\License::class)
+ <x-table.bulk-actions
+ name='licenses'
+ action_route="{{ route('licenses.bulk.delete') }}"
+ model_name="license"
+ :actions="$bulkActions"
+ />
+ @endcan
</x-slot:bulkactions>
<x-table.licenses
diff --git a/resources/views/partials/bootstrap-table.blade.php b/resources/views/partials/bootstrap-table.blade.php
index 9d33c97476..924150488c 100644
--- a/resources/views/partials/bootstrap-table.blade.php
+++ b/resources/views/partials/bootstrap-table.blade.php
@@ -1894,6 +1894,80 @@
updateSelectedCount(this);
});
+ // Dynamic bulk actions: when a table's bulk-actions dropdown was rendered with
+ // data-dynamic-actions, its options are populated here from the intersection of
+ // each selected row's available_actions.bulk_selectable. An action shows only
+ // when every currently-selected row supports it. Tables that rendered a static
+ // option list have no data-dynamic-actions attribute and are unaffected.
+ function refreshDynamicBulkActions(table) {
+ var $table = $(table);
+ var formId = $table.data('bulk-form-id');
+ if (!formId) return;
+
+ var $select = $(formId).find('select[data-dynamic-actions]');
+ if ($select.length === 0) return;
+
+ var actions;
+ try {
+ actions = JSON.parse($select.attr('data-dynamic-actions')) || {};
+ } catch (e) {
+ return;
+ }
+
+ var selections = $table.bootstrapTable('getSelections');
+ var $button = $($table.data('bulk-button-id'));
+ var currentValue = $select.val();
+ var placeholder = $select.attr('data-placeholder') || '';
+
+ var eligible = null;
+ for (var i = 0; i < selections.length; i++) {
+ var supported = (selections[i].available_actions && selections[i].available_actions.bulk_selectable) || {};
+ var rowActions = {};
+ for (var k in supported) {
+ if (supported[k] === true) rowActions[k] = true;
+ }
+ if (eligible === null) {
+ eligible = rowActions;
+ } else {
+ var next = {};
+ for (var kk in eligible) if (rowActions[kk]) next[kk] = true;
+ eligible = next;
+ }
+ }
+
+ if ($select.hasClass('select2-hidden-accessible')) {
+ $select.select2('destroy');
+ }
+ $select.empty();
+
+ if (selections.length === 0) {
+ $select.append($('<option/>', { value: '', text: placeholder }));
+ $button.attr('disabled', 'disabled');
+ } else if (!eligible || Object.keys(eligible).length === 0) {
+ $select.append($('<option/>', { value: '', text: '{{ trans('general.bulk_actions_none_available') }}' }));
+ $button.attr('disabled', 'disabled');
+ } else {
+ var appendedAny = false;
+ for (var actionKey in actions) {
+ if (eligible[actionKey] && actions[actionKey] && actions[actionKey].label) {
+ $select.append($('<option/>', { value: actionKey, text: actions[actionKey].label }));
+ appendedAny = true;
+ }
+ }
+ if (appendedAny) {
+ if (currentValue && eligible[currentValue]) {
+ $select.val(currentValue);
+ }
+ $button.removeAttr('disabled');
+ } else {
+ $select.append($('<option/>', { value: '', text: '{{ trans('general.bulk_actions_none_available') }}' }));
+ $button.attr('disabled', 'disabled');
+ }
+ }
+
+ $select.select2();
+ }
+
// These methods dynamically add/remove hidden input values in the bulk actions form
$('.snipe-table').on('check.bs.table .btSelectItem', function (row, $element) {
var buttonName = $(this).data('bulk-button-id');
@@ -1907,6 +1981,7 @@
value: $element.id
}));
updateSelectedCount(this);
+ refreshDynamicBulkActions(this);
});
$('.snipe-table').on('check-all.bs.table', function (event, rowsAfter) {
@@ -1930,6 +2005,7 @@
$(buttonName).removeAttr('disabled');
}
updateSelectedCount(this);
+ refreshDynamicBulkActions(this);
});
@@ -1948,6 +2024,7 @@
$(buttonName).attr('disabled', 'disabled');
}
+ refreshDynamicBulkActions(this);
});
$('.snipe-table').on('uncheck-all.bs.table', function (event, rowsAfter, rowsBefore) {
@@ -1960,7 +2037,7 @@
$('#' + tableId + "_checkbox_" + rowsBefore[i].id).remove();
}
updateSelectedCount(this);
-
+ refreshDynamicBulkActions(this);
});
// Initialize sort-order for bulk actions (label-generation) for snipe-tables