-
Notifications
You must be signed in to change notification settings - Fork 263
Add template hooks to all filters for customizable UIs (dropdowns, sliders, etc.) #970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
mmzeynalli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work! Do please update the mentioned part. Also, would be nice if you provided screenshots of vanilla vs custom design filter design, even if minimal.
| filter.template | ||
| if filter.template is defined and filter.template | ||
| else ( | ||
| "sqladmin/filters/operation_filter.html" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can skip this part. If user creates new filter, they will inherit the existing one, if they create from scratch, it should be their responsibility. No need for extra has_operator check.
mmzeynalli
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM @aminalaee


Overview
This PR introduces customizable filter templates by adding a
templateattribute to filters. All built-in filters now define a default template to ensure full backward compatibility.Filters are rendered through their declared template, with predictable fallbacks, making template overrides a first-class extension point.
Goals
Example
filters/status_dropdown_filter.html{% extends "sqladmin/filters/base_filter.html" %} {% block filter_body %} {% set current_value = request.query_params.get(filter.parameter_name, '') %} <form method="get" class="d-flex flex-column" style="gap: 8px;"> {% for key, value in request.query_params.items() %} {% if key != filter.parameter_name %} <input type="hidden" name="{{ key }}" value="{{ value }}"> {% endif %} {% endfor %} <select name="{{ filter.parameter_name }}" class="form-select form-select-sm"> {% for value, label in filter.lookups(request, model_view.model, model_view._run_arbitrary_query) %} <option value="{{ value }}" {% if current_value == value %}selected{% endif %}>{{ label }}</option> {% endfor %} </select> <div class="d-flex align-items-center" style="gap: 8px;"> <button type="submit" class="btn btn-sm btn-outline-primary">Apply</button> {% if current_value %} <a href="{{ request.url.remove_query_params(filter.parameter_name) }}" class="text-decoration-none small">Clear</a> {% endif %} </div> </form> {% endblock %}By subclassing an existing filter and overriding only its template, developers can package and reuse bespoke filter widgets with minimal effort.