Skip to content
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

Closes: #9583 - Add column specific search field to tables #15073

Open
wants to merge 28 commits into
base: feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
4c39516
Preliminary work on 9583.
DanSheps Feb 2, 2024
e762755
HTMX work on 9583.
DanSheps Feb 7, 2024
f7294f7
Preliminary work on 9583.
DanSheps Feb 7, 2024
664a0eb
Preliminary work on 9583.
DanSheps Feb 7, 2024
cc423f5
Apply some quick fixes and add comments
DanSheps Feb 7, 2024
50557c0
Final work on #9583 for basic functionality
DanSheps Feb 9, 2024
03f67f3
CSS update for dropdown
DanSheps Feb 9, 2024
3243ebd
Merge remote-tracking branch 'origin/9583-add_column_specific_search_…
DanSheps Feb 9, 2024
b54cfd6
Update CSS
DanSheps Feb 12, 2024
5f69666
Change dropdown position and fix test failure
DanSheps Feb 12, 2024
06c1aff
Fix test failure
DanSheps Feb 12, 2024
4ae6683
Merge in recent feture changes
DanSheps Feb 12, 2024
c3f1a96
Update CSS after merge
DanSheps Feb 12, 2024
f81f76f
Optimizations
DanSheps Feb 12, 2024
0309796
Fix extraneous __all__ entry
DanSheps Feb 12, 2024
84151cb
Fix tom-select errors related to field id. Break out render_field fu…
DanSheps Feb 13, 2024
77bfd62
Merge branch 'feature' into 9583-add_column_specific_search_field_to_…
jeremystretch Mar 7, 2024
25a4e94
Merge branch 'feature' into 9583-add_column_specific_search_field_to_…
jeremystretch Mar 20, 2024
f257f4a
Apply suggestions from code review
DanSheps Mar 22, 2024
8a7df0b
Update netbox/utilities/templatetags/form_helpers.py
DanSheps Mar 22, 2024
a422a3c
Modify logic for table column filtering to further isolate the column…
DanSheps Mar 22, 2024
8ad79a6
Update CSS
DanSheps Mar 22, 2024
a9aa0cb
Merge in latest feature
DanSheps Apr 15, 2024
35cff12
Apply suggestions from Arthur
DanSheps Apr 15, 2024
1c995fa
Perform OOB swap for filter badges
DanSheps Apr 15, 2024
479c69b
Fix up duplication of template chits when rendering HTMX
DanSheps Apr 23, 2024
5830ae9
Rename variable for doing OOB swaps on the table
DanSheps Apr 25, 2024
fd38255
Rename swap variable back and remove join
DanSheps Apr 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions netbox/netbox/tables/tables.py
Expand Up @@ -40,6 +40,7 @@ class BaseTable(tables.Table):
:param user: Personalize table display for the given user (optional). Has no effect if AnonymousUser is passed.
"""
exempt_columns = ()
filterset_form = None

class Meta:
attrs = {
Expand Down
20 changes: 19 additions & 1 deletion netbox/netbox/views/generic/bulk_views.py
Expand Up @@ -11,6 +11,7 @@
from django.forms import HiddenInput, ModelMultipleChoiceField, MultipleHiddenInput
from django.http import HttpResponse
from django.shortcuts import get_object_or_404, redirect, render
from django.template.loader import render_to_string
from django.urls import reverse
from django.utils.safestring import mark_safe
from django.utils.translation import gettext as _
Expand Down Expand Up @@ -161,22 +162,39 @@ def get(self, request):
# Render the objects table
table = self.get_table(self.queryset, request, has_bulk_actions)

# Check for filterset_form on this view, if a form exists
# * Apply to context for use by the filter form tab and initialize the form
# * Apply to the table for use by the table and initialize a separate instance of the form for use by the table
# column filters
# * Otherwise set to None
if self.filterset_form:
filterset_form = self.filterset_form(request.GET)
table.filterset_form = self.filterset_form(request.GET)
else:
filterset_form = None
table.filterset_form = None

# If this is an HTMX request, return only the rendered table HTML
if htmx_partial(request):
if not request.htmx.target:
table.embedded = True
# Hide selection checkboxes
if 'pk' in table.base_columns:
table.columns.hide('pk')
filter_chits = render_to_string('inc/applied_filters_pane.html', {
'model': model,
'filter_form': filterset_form,
}, request)
return render(request, 'htmx/table.html', {
'table': table,
'filter_chits': filter_chits,
})

context = {
'model': model,
'table': table,
'actions': actions,
'filter_form': self.filterset_form(request.GET, label_suffix='') if self.filterset_form else None,
'filter_form': filterset_form,
'prerequisite_model': get_prerequisite_model(self.queryset),
**self.get_extra_context(request),
}
Expand Down
2 changes: 1 addition & 1 deletion netbox/project-static/dist/netbox.css

Large diffs are not rendered by default.

17 changes: 17 additions & 0 deletions netbox/project-static/styles/custom/_misc.scss
Expand Up @@ -28,3 +28,20 @@ span.color-label {
visibility: hidden;
opacity: 0;
}

// Override bootstrap "dropdown" positioning and display for column filters
.column-filter {
position: static;
display: inline;
}

// Override mdi font-size to adjust filter icon size
.column-filter.dropdown > .dropdown-toggle > .mdi-filter-settings {
font-size: .625rem;
}

.column-filter.dropdown > .dropdown-menu {
max-width: 300px;
}

.column-filter.dropdown-toggle:after { content: none }
4 changes: 1 addition & 3 deletions netbox/templates/generic/object_list.html
Expand Up @@ -63,9 +63,7 @@
<div class="tab-pane show active" id="object-list" role="tabpanel" aria-labelledby="object-list-tab">

{# Applied filters #}
{% if filter_form %}
{% applied_filters model filter_form request.GET %}
{% endif %}
{% include 'inc/applied_filters_pane.html' %}

{# Object table controls #}
{% include 'inc/table_controls_htmx.html' with table_modal="ObjectTable_config" %}
Expand Down
3 changes: 3 additions & 0 deletions netbox/templates/htmx/table.html
Expand Up @@ -2,6 +2,9 @@
{% load helpers %}
{% load render_table from django_tables2 %}

{# OOB Swaps to update various components #}
{{ filter_chits }}

{% with preferences|get_key:"pagination.placement" as paginator_placement %}
{% if paginator_placement == 'top' or paginator_placement == 'both' %}
{% include 'inc/paginator.html' with htmx=True table=table paginator=table.paginator page=table.page %}
Expand Down
8 changes: 8 additions & 0 deletions netbox/templates/inc/applied_filters_pane.html
@@ -0,0 +1,8 @@
{% load helpers %}

<div id="applied_filters_pane" hx-swap-oob="true">
{# Applied filters #}
{% if filter_form %}
{% applied_filters model filter_form request.GET %}
{% endif %}
</div>
2 changes: 1 addition & 1 deletion netbox/templates/inc/table_controls_htmx.html
Expand Up @@ -5,7 +5,7 @@
<div class="col-auto d-print-none">
<div class="input-group input-group-flat me-2 quicksearch" hx-disinherit="hx-select hx-swap">
<input type="search" results="5" name="q" id="quicksearch" class="form-control px-2 py-1" placeholder="Quick search"
hx-get="{{ request.full_path }}" hx-target="#object_list" hx-trigger="keyup changed delay:500ms, search" />
hx-get="" hx-target="#object_list" hx-trigger="keyup changed delay:500ms, search" />
Copy link
Member Author

@DanSheps DanSheps Feb 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed full path, as this will allow taking into account filters on the form (to facilitate pushing of the filters from the column as well).

Should not impact functionality

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not impact functionality

Do we know this for sure? IIRC we specify the full path for a reason, I just can't recall why.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my testing, I haven't found any issues with it.

<span class="input-group-text py-1">
<a href="#" id="quicksearch_clear" class="d-none text-secondary"><i class="mdi mdi-close-circle"></i></a>
</span>
Expand Down
11 changes: 11 additions & 0 deletions netbox/templates/inc/table_header_filter_dropdown.html
@@ -0,0 +1,11 @@
{% load form_helpers %}
{% if form_field %}
<div class="column-filter dropdown">
<a href="#" class="dropdown-toggle" data-bs-toggle="dropdown" data-bs-auto-close="outside"><i class="mdi mdi-filter-settings"></i></a>
<div class="dropdown-menu">
<div class="px-3 py-3">
{% render_table_filter_field form_field table=table request=request %}
</div>
</div>
</div>
{% endif %}
8 changes: 7 additions & 1 deletion netbox/templates/inc/table_htmx.html
@@ -1,4 +1,5 @@
{% load django_tables2 %}
{% load form_helpers %}
<table{% if table.attrs %} {{ table.attrs.as_html }}{% endif %} hx-disinherit="hx-target hx-select hx-swap">
{% if table.show_header %}
<thead
Expand All @@ -14,11 +15,16 @@
<a href="#"
hx-get="{{ table.htmx_url }}{% querystring table.prefixed_order_by_field='' %}"
class="text-danger"
{% if not table.embedded %}hx-push-url="true"{% endif %}
><i class="mdi mdi-close"></i></a>
</div>
{% endif %}
{% if table.filterset_form %}
{% include 'inc/table_header_filter_dropdown.html' with form_field=table.filterset_form|get_filter_field:column.name %}
{% endif %}
<a href="#"
hx-get="{{ table.htmx_url }}{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}"
hx-get="{{ table.htmx_url }}{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}"
{% if not table.embedded %}hx-push-url="true"{% endif %}
>{{ column.header }}</a>
</th>
{% else %}
Expand Down
41 changes: 41 additions & 0 deletions netbox/utilities/templatetags/form_helpers.py
Expand Up @@ -6,13 +6,15 @@

__all__ = (
'getfield',
'get_filter_field',
'render_custom_fields',
'render_errors',
'render_field',
'render_form',
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
'widget_type',
)

from utilities.templatetags.helpers import querystring

register = template.Library()

Expand All @@ -32,6 +34,11 @@ def getfield(form, fieldname):
return None


@register.filter()
def get_filter_field(form, fieldname):
return getfield(form, f'{fieldname}') or getfield(form, f'{fieldname}_id')


@register.filter(name='widget_type')
def widget_type(field):
"""
Expand Down Expand Up @@ -120,13 +127,47 @@ def render_field(field, bulk_nullable=False, label=None):
"""
Render a single form field from template
"""

return {
'field': field,
'label': label or field.label,
'bulk_nullable': bulk_nullable or getattr(field, '_nullable', False),
}


@register.inclusion_tag('form_helpers/render_field.html')
def render_table_filter_field(field, table, request):
"""
Render a single form field for table column filters from template
"""
url = ""

# Handle filter forms
if table:
# Build kwargs for querystring function
kwargs = {field.name: None}
# Build request url
if request and table.htmx_url:
url = table.htmx_url + querystring(request, **kwargs)
elif request:
url = querystring(request, **kwargs)

# Set HTMX args
if hasattr(field.field, 'widget'):
field.field.widget.attrs.update({
'id': f'table_filter_id_{field.name}',
'hx-get': url if url else '#',
'hx-push-url': "true",
'hx-trigger': 'hidden.bs.dropdown from:closest .dropdown'
})

return {
'field': field,
'label': None,
'bulk_nullable': False,
}


@register.inclusion_tag('form_helpers/render_custom_fields.html')
def render_custom_fields(form):
"""
Expand Down