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 15 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 @@ -38,6 +38,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
8 changes: 7 additions & 1 deletion netbox/netbox/views/generic/bulk_views.py
Expand Up @@ -160,6 +160,12 @@ 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 and table, otherwise set to None
filterset_form = None
if hasattr(self, 'filterset_form') and self.filterset_form:
filterset_form = self.filterset_form(request.GET, label_suffix='')
table.filterset_form = filterset_form
DanSheps marked this conversation as resolved.
Show resolved Hide resolved

# If this is an HTMX request, return only the rendered table HTML
if request.htmx:
if request.htmx.target != 'object_list':
Expand All @@ -175,7 +181,7 @@ def get(self, request):
'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.

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

// Override column filter form dropdown
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
.column-filter {
position: static;
display: inline;
}
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">
<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="#" classs="btn dropdown-toggle" data-bs-toggle="dropdown" data-bs-auto-close="outside">
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
<i class="mdi mdi-filter-settings" style="font-size: 1.25rem;"> </i>
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
</a>
<div class="dropdown-menu">
{% render_field form_field table=table request=request %}
</div>
</div>
{% endif %}
11 changes: 10 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 %}>
{% if table.show_header %}
<thead>
Expand All @@ -16,14 +17,22 @@
><i class="mdi mdi-close"></i></a>
</div>
{% endif %}
{% if table.filterset_form %}
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
{% include 'inc/table_header_filter_dropdown.html' with form_field=table.filterset_form|getfilterfield:column.name %}
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
{% endif %}
<a href="#"
hx-get="{{ table.htmx_url }}{% querystring table.prefixed_order_by_field=column.order_by_alias.next %}"
hx-target="closest .htmx-container"
{% if not table.embedded %}hx-push-url="true"{% endif %}
>{{ column.header }}</a>
</th>
{% else %}
<th {{ column.attrs.th.as_html }}>{{ column.header }}</th>
<th {{ column.attrs.th.as_html }}>
{% if table.filterset_form %}
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
{% include 'inc/table_header_filter_dropdown.html' with form_field=table.filterset_form|getfilterfield:column.name %}
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
{% endif %}
{{ column.header }}
</th>
{% endif %}
{% endfor %}
</tr>
Expand Down
36 changes: 34 additions & 2 deletions netbox/utilities/templatetags/form_helpers.py
@@ -1,14 +1,17 @@
from django import template


__all__ = (
'getfield',
'getfilterfield',
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
'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 @@ -28,6 +31,15 @@ def getfield(form, fieldname):
return None


@register.filter()
def getfilterfield(form, fieldname):
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
field = getfield(form, f'{fieldname}')
if field is not None:
return field
else:
return getfield(form, f'{fieldname}_id')
DanSheps marked this conversation as resolved.
Show resolved Hide resolved


@register.filter(name='widget_type')
def widget_type(field):
"""
Expand All @@ -46,13 +58,33 @@ def widget_type(field):
#

@register.inclusion_tag('form_helpers/render_field.html')
def render_field(field, bulk_nullable=False, label=None):
def render_field(field, bulk_nullable=False, label=None, table=None, request=None):
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
"""
Render a single form field 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
DanSheps marked this conversation as resolved.
Show resolved Hide resolved
if hasattr(field.field, 'widget'):
field.field.widget.attrs.update({
'hx-get': url if url else '#',
'hx-push-url': "true",
'hx-target': '#object_list',
'hx-trigger': 'hidden.bs.dropdown from:closest .dropdown'
})

return {
'field': field,
'label': label or field.label,
'label': label or field.label if not table else None,
'bulk_nullable': bulk_nullable,
}

Expand Down