Add filtering for dropdowns.

This commit is contained in:
Yaro Kasear 2025-10-22 13:31:13 -05:00
parent 5234cbdd61
commit 51520da5af
4 changed files with 87 additions and 8 deletions

View file

@ -105,7 +105,8 @@ def _fields_for_model(model: str):
"row": "name", "wrap": {"class": "col-3"}},
{"name": "supervisor", "label": "Supervisor", "label_attrs": {"class": "form-label"},
"label_spec": "{label}", "row": "details", "wrap": {"class": "col-3"},
"attrs": {"class": "form-control"}, "link": {"endpoint": "entry.entry", "params": {"id": "{supervisor.id}", "model": "user"}}},
"attrs": {"class": "form-control"}, "link": {"endpoint": "entry.entry", "params": {"id": "{supervisor.id}", "model": "user"}},
"options_params": {"active__eq": True, "staff__eq": True}},
{"name": "location", "label": "Room", "label_attrs": {"class": "form-label"},
"label_spec": "{name} - {room_function.description}",
"row": "details", "wrap": {"class": "col-3"}, "attrs": {"class": "form-control"}},

View file

@ -30,7 +30,21 @@
#}
{% if options %}
{% if value %}
{% set sel_label = (options | selectattr('value', 'equalto', value) | first)['label'] %}
{% set opts = options or [] %}
{% set selected = opts | selectattr('value', 'equalto', value) | list %}
{% if not selected %}
{# try again with string coercion to handle int vs str mismatch #}
{% set selected = opts | selectattr('value', 'equalto', value|string) | list %}
{% endif %}
{% set sel = selected[0] if selected else none %}
{% if sel %}
{% set sel_label = sel['label'] %}
{% elif value_label %}
{% set sel_label = value_label %}
{% else %}
{% set sel_label = "-- Select --" %}
{% endif %}
{% else %}
{% set sel_label = "-- Select --" %}
{% endif %}
@ -41,11 +55,13 @@
id="{{ field_name }}-filter" placeholder="Filter..." oninput="DropDown.utilities.filterList('{{ field_name }}')">
{% for opt in options %}
<li><a class="dropdown-item{% if opt.value|string == value|string %} active{% endif %}"
data-value="{{ opt['value'] }}" onclick="DropDown.utilities.selectItem('{{ field_name }}', '{{ opt['value'] }}')" id="{{ field_name }}-{{ opt['value'] }}">{{ opt['label'] }}</a></li>
data-value="{{ opt['value'] }}" onclick="DropDown.utilities.selectItem('{{ field_name }}', '{{ opt['value'] }}')"
id="{{ field_name }}-{{ opt['value'] }}">{{ opt['label'] }}</a></li>
{% endfor %}
</div>
{% else %}
<button class="btn btn-outline-dark d-block w-100 text-start dropdown-toggle disabled inventory-dropdown" disabled>-- No selection available --</button>
<button class="btn btn-outline-dark d-block w-100 text-start dropdown-toggle disabled inventory-dropdown" disabled>-- No
selection available --</button>
{% endif %}
<input type="hidden" name="{{ field_name }}" id="{{ field_name }}" value="{{ value }}">