Skip to content

A free open source IT asset/license management system

New blade components for checkbox-style selectlists

snipe0d0831748a8b
diff --git a/resources/views/blade/input/select2-checkboxes-scrollbox.blade.php b/resources/views/blade/input/select2-checkboxes-scrollbox.blade.php
new file mode 100644
index 0000000000..8a39b35a31
--- /dev/null
+++ b/resources/views/blade/input/select2-checkboxes-scrollbox.blade.php
@@ -0,0 +1,119 @@
+{{-- Always-open scrolling checkbox list backed by an AJAX selectlist
+ endpoint. Behavior lives in the .js-select2-checkboxes-scrollbox init in
+ snipeit.js. Styling in overrides.less under the same name.
+
+ Submits as a single hidden input containing a comma-separated
+ list of selected ids, sidestepping max_input_vars for very large
+ selections. The server reads with
+ explode(',', $request->input($name)).
+
+ Props:
+ name : hidden input name and form field.
+ label : control label shown above the scrolling box.
+ endpoint : selectlist endpoint slug (users, companies, ...).
+ selected : array of pre-selected ids (optional).
+ height : max-height of the scrolling box in px (default 400).
+ maxSelectAll : optional per-widget ceiling for ?all=1 (clamped by MAX_UNPAGINATED).
+ help_text : optional help text under the control.
+--}}
+@props([
+ 'name',
+ 'label',
+ 'endpoint',
+ 'selected' => [],
+ 'height' => 400,
+ 'maxSelectAll' => null,
+ 'help_text' => null,
+ 'compactSubmit' => false,
+])
+
+@php
+ $listId = $name.'_checkbox_list';
+ $selectedIds = collect($selected ?? [])->filter()->values();
+ $selectedCsv = $selectedIds->implode(',');
+ $selectedCount = $selectedIds->count();
+@endphp
+
+<div @class(['form-group', 'has-error' => $errors->has($name)])>
+ <label for="{{ $listId }}" class="col-md-3 control-label">{{ $label }}</label>
+ <div class="col-md-7">
+ <div
+ class="js-select2-checkboxes-scrollbox"
+ data-endpoint="{{ $endpoint }}"
+ data-name="{{ $name }}"
+ data-compact-submit="{{ $compactSubmit ? '1' : '0' }}"
+ @if ($compactSubmit) data-hidden="{{ $listId }}_hidden" @endif
+ @if ($maxSelectAll) data-max-select-all="{{ (int) $maxSelectAll }}" @endif
+ >
+ <div class="js-select2-checkboxes-scrollbox-controls">
+ <div class="js-select2-checkboxes-scrollbox-search-wrap">
+ <input
+ type="text"
+ class="form-control js-select2-checkboxes-scrollbox-search"
+ placeholder="{{ trans('general.search') }}"
+ aria-label="{{ trans('general.search') }}"
+ >
+ <button
+ type="button"
+ class="js-select2-checkboxes-scrollbox-search-clear"
+ aria-label="{{ trans('admin/hardware/general.clear') }}"
+ >&times;</button>
+ </div>
+ <button type="button" class="btn btn-sm btn-theme js-select2-checkboxes-scrollbox-selectall">
+ {{ trans('general.select_all') }}
+ </button>
+ <button type="button" class="btn btn-sm btn-theme js-select2-checkboxes-scrollbox-clear">
+ {{ trans('admin/hardware/general.clear') }}
+ </button>
+ {{-- {n} is the JS-side placeholder convention. Laravel's
+ trans() substitutes `:key` not `{n}`, so render the
+ initial count by hand here and stash the template
+ on data-template for the JS to re-use on every
+ selection change. --}}
+ @php $countTemplate = trans('general.checkbox_multiselect_count'); @endphp
+ <span
+ class="js-select2-checkboxes-scrollbox-count"
+ aria-live="polite"
+ data-template="{{ $countTemplate }}"
+ >{{ str_replace('{n}', $selectedCount, $countTemplate) }}</span>
+ </div>
+ <div
+ id="{{ $listId }}"
+ class="js-select2-checkboxes-scrollbox-scroll"
+ style="height: {{ (int) $height }}px;"
+ role="listbox"
+ aria-multiselectable="true"
+ aria-label="{{ $label }}"
+ ></div>
+
+ @if ($compactSubmit)
+ {{-- Single hidden comma-separated input. Server reads with
+ explode(','). JS keeps it in sync as the user
+ ticks rows. Only used for lists with a LOT of items that
+ might hit the php_max_input_vars limit. --}}
+ <input
+ type="hidden"
+ id="{{ $listId }}_hidden"
+ name="{{ $name }}"
+ value="{{ $selectedCsv }}"
+ >
+ @else
+ {{-- Container for per-id hidden inputs. JS keeps one
+ name="X[]" input per selected id so the form
+ submits a plain array shape the rest of the app
+ already understands. --}}
+ <div class="js-select2-checkboxes-scrollbox-array-inputs" aria-hidden="true">
+ @foreach ($selectedIds as $preselectedId)
+ <input type="hidden" name="{{ $name }}[]" value="{{ $preselectedId }}">
+ @endforeach
+ </div>
+ @endif
+ </div>
+
+ @if ($help_text)
+ <x-form.help :name="$listId">{!! $help_text !!}</x-form.help>
+ @endif
+
+ <x-form.error :name="$name" />
+ </div>
+</div>
diff --git a/resources/views/blade/input/select2-checkboxes.blade.php b/resources/views/blade/input/select2-checkboxes.blade.php
new file mode 100644
index 0000000000..6bdddc1d54
--- /dev/null
+++ b/resources/views/blade/input/select2-checkboxes.blade.php
@@ -0,0 +1,98 @@
+{{-- AJAX-paginated select2 skinned as a select2-checkboxes.
+
+ Props:
+ name : form field name, submitted as name[].
+ label : control label.
+ endpoint : selectlist endpoint slug (companies, locations, users...).
+ selected : array of pre-selected ids (optional).
+ modelClass : Eloquent model class for pre-rendering option text and
+ tag_color on initial page load. Optional. When omitted
+ pre-selected ids render as id-only text until the user
+ interacts.
+ required : HTML required on the underlying select (optional).
+ companyId : forwarded as data-company-id so the endpoint can scope.
+ excludeId : forwarded as data-exclude-id.
+ onlyTopLevel : forwarded as data-only-top-level (companies hierarchy).
+ help_text : optional help text under the control.
+ countLabel : "{n} selected" by default, overrideable per locale.
+ placeholder : placeholder for the control + dropdown search field.
+ selectAll : enable the "Select all / Clear" row in the dropdown (default true).
+ maxSelectAll : optional per-widget ceiling for ?all=1 (clamped by MAX_UNPAGINATED).
+ labelClass : grid class for the label (default col-md-3).
+ inputClass : grid class for the input column (default col-md-7).
+--}}
+@props([
+ 'name',
+ 'label',
+ 'endpoint',
+ 'selected' => [],
+ 'modelClass' => null,
+ 'required' => false,
+ 'companyId' => null,
+ 'excludeId' => null,
+ 'onlyTopLevel' => false,
+ 'help_text' => null,
+ 'countLabel' => trans('general.checkbox_multiselect_count'),
+ 'placeholder' => '',
+ 'selectAll' => true,
+ 'maxSelectAll' => null,
+ 'labelClass' => 'col-md-3',
+ 'inputClass' => 'col-md-7',
+])
+
+@php
+ $selectId = $name.'_select';
+ $selectedIds = collect($selected ?? [])->filter()->values();
+ // Pre-fetch the selected models (name + tag_color when available) so
+ // the chip strip renders colored squares and real text on initial
+ // page load, not just the raw id. The tag_color column is optional
+ // per model and read via ?-> so models without one fall to null.
+ $selectedModels = collect();
+ if ($modelClass && $selectedIds->isNotEmpty()) {
+ $selectedModels = $modelClass::whereIn('id', $selectedIds->all())->get()->keyBy('id');
+ }
+@endphp
+
+<div @class(['form-group', 'has-error' => $errors->has($name)])>
+ <label for="{{ $selectId }}" class="{{ $labelClass }} control-label">{{ $label }}</label>
+ <div class="{{ $inputClass }}">
+ <select
+ id="{{ $selectId }}"
+ name="{{ $name }}[]"
+ class="js-select2-checkboxes"
+ data-endpoint="{{ $endpoint }}"
+ data-placeholder="{{ $placeholder }}"
+ data-count-label="{{ $countLabel }}"
+ @if ($selectAll)
+ data-select-all="1"
+ data-select-all-label="{{ trans('general.select_all') }}"
+ data-clear-all-label="{{ trans('admin/hardware/general.clear') }}"
+ @if ($maxSelectAll) data-max-select-all="{{ (int) $maxSelectAll }}" @endif
+ @endif
+ @if ($companyId) data-company-id="{{ $companyId }}" @endif
+ @if ($excludeId) data-exclude-id="{{ $excludeId }}" @endif
+ @if ($onlyTopLevel) data-only-top-level="1" @endif
+ style="width: 100%"
+ aria-label="{{ $label }}"
+ multiple
+ @required($required)
+ >
+ @foreach ($selectedIds as $id)
+ @php $model = $selectedModels->get($id); @endphp
+ <option
+ value="{{ $id }}"
+ selected="selected"
+ @if ($model?->tag_color) data-tag-color="{{ $model->tag_color }}" @endif
+ >{{ $model?->name ?? $id }}</option>
+ @endforeach
+ </select>
+ </div>
+
+ @if ($help_text)
+ <div class="{{ $inputClass }} col-md-offset-3">
+ <x-form.help :name="$selectId">{!! $help_text !!}</x-form.help>
+ </div>
+ @endif
+
+ <div class="col-md-8 col-md-offset-3"><x-form.error :name="$name" /></div>
+</div>