mirror of
https://github.com/netbox-community/netbox.git
synced 2025-12-19 03:42:25 -06:00
Introduce InlineFields for rendering fields side-by-side
This commit is contained in:
10
netbox/utilities/forms/rendering.py
Normal file
10
netbox/utilities/forms/rendering.py
Normal file
@@ -0,0 +1,10 @@
|
||||
__all__ = (
|
||||
'InlineFields',
|
||||
)
|
||||
|
||||
|
||||
class InlineFields:
|
||||
|
||||
def __init__(self, *field_names, label=None):
|
||||
self.field_names = field_names
|
||||
self.label = label
|
||||
26
netbox/utilities/templates/form_helpers/render_fieldset.html
Normal file
26
netbox/utilities/templates/form_helpers/render_fieldset.html
Normal file
@@ -0,0 +1,26 @@
|
||||
{% load i18n %}
|
||||
{% load form_helpers %}
|
||||
<div class="field-group mb-5">
|
||||
{% if heading %}
|
||||
<div class="row">
|
||||
<h5 class="col-9 offset-3">{{ heading }}</h5>
|
||||
</div>
|
||||
{% endif %}
|
||||
{% for layout, title, items in rows %}
|
||||
{% if layout == 'field' %}
|
||||
{# Single form field #}
|
||||
{% render_field items.0 %}
|
||||
{% elif layout == 'inline' %}
|
||||
{# Multiple form fields on the same line #}
|
||||
<div class="row mb-3">
|
||||
<label class="col col-form-label text-lg-end">{{ title|default:'' }}</label>
|
||||
{% for field in items %}
|
||||
<div class="col mb-1">
|
||||
{{ field }}
|
||||
<div class="form-text">{% trans field.label %}</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</div>
|
||||
@@ -1,5 +1,7 @@
|
||||
from django import template
|
||||
|
||||
from utilities.forms.rendering import InlineFields
|
||||
|
||||
__all__ = (
|
||||
'getfield',
|
||||
'render_custom_fields',
|
||||
@@ -45,6 +47,28 @@ def widget_type(field):
|
||||
# Inclusion tags
|
||||
#
|
||||
|
||||
@register.inclusion_tag('form_helpers/render_fieldset.html')
|
||||
def render_fieldset(form, fieldset, heading=None):
|
||||
"""
|
||||
Render a group set of fields.
|
||||
"""
|
||||
rows = []
|
||||
for item in fieldset:
|
||||
if type(item) is InlineFields:
|
||||
rows.append(
|
||||
('inline', item.label, [form[name] for name in item.field_names])
|
||||
)
|
||||
else:
|
||||
rows.append(
|
||||
('field', None, [form[item]])
|
||||
)
|
||||
|
||||
return {
|
||||
'heading': heading,
|
||||
'rows': rows,
|
||||
}
|
||||
|
||||
|
||||
@register.inclusion_tag('form_helpers/render_field.html')
|
||||
def render_field(field, bulk_nullable=False, label=None):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user