Showing nicer tables in plugin #15525
-
In my quest to display arbitrary external data in a NetBox view, I've got this far: Meaning that I can display data that is from a static dict for now. There is even pagination ( table = MyDataTable(my_precious_data_as_dict)
paginate = {
'paginator_class': EnhancedPaginator,
'per_page': get_paginate_count(request),
}
tables.RequestConfig(request, paginate).configure(table)
return render(request, "my_plugin/my_data_list.html", { "table": table}) Naturally I'd like to get the list a bit more like NetBox-looking. How is that actually achieved? My table is simply ( import django_tables2 as tables
class MyDataTable(tables.Table):
name = tables.Column()
value = tables.Column() I figured out that I cannot use The template is ( {% extends 'base/layout.html' %}
{% load render_table from django_tables2 %}
{% block title %}My Data{% endblock %}
{% block content %}
<div class="row">
<div class="col-md-9">
{% render_table table %}
</div>
</div>
{% endblock content %}
Any hints how to improve the layout and get the NetBox look and feel? Should I just to skip |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 7 replies
-
Because the data provided isn't linked to any NetBox model, we aren't able the extend the NetboxTable class, which would normally set the required classes. However, we can set those classes manually. class MyDataTable(tables.Table):
name = tables.Column()
value = tables.Column()
class Meta:
attrs = {
'class': 'table table-hover',
} This table is still quite basic compared to NetBox tables, where sorting, filtering, etc is allowed. Those functions are all implemented in NetBoxTable. |
Beta Was this translation helpful? Give feedback.
-
So the final solution for this exercise:
import django_tables2 as tables
class MyDataTable(tables.Table):
name = tables.Column()
value = tables.Column()
class Meta:
attrs = {
"class": "table table-hover",
}
import django_tables2 as tables
from utilities.htmx import is_embedded, is_htmx
from utilities.paginator import EnhancedPaginator, get_paginate_count
...
table = MyDataTable(my_precious_data_as_dict)
paginate = {
'paginator_class': EnhancedPaginator,
'per_page': get_paginate_count(request),
}
tables.RequestConfig(request, paginate).configure(table)
if is_htmx(request):
if is_embedded(request):
table.embedded = True
return render(request, 'htmx/table.html', {'table': table})
return render(request, "my_plugin/my_data_list.html", { "table": table})
{% extends 'base/layout.html' %}
{% block title %}My Data{% endblock %}
{% block content %}
<div class="htmx-container">
{% include 'htmx/table.html' %}
</div>
{% endblock content %} To be clear and honest: I have no idea how these will survive NetBox major/minor version upgrades 😁 |
Beta Was this translation helpful? Give feedback.
Because the data provided isn't linked to any NetBox model, we aren't able the extend the NetboxTable class, which would normally set the required classes. However, we can set those classes manually.
This table is still quite basic compared to NetBox tables, where sorting, filtering, etc is allowed. Those functions are all implemented in NetBoxTable.