From 0ee1112d9de7dfb8b02c7fb8b9326298a7f2c73d Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 27 Apr 2020 16:56:25 -0400 Subject: [PATCH] Initial support for table column reordering --- netbox/circuits/tables.py | 11 ++++++++++- netbox/utilities/tables.py | 18 +++++++++++++++++- netbox/utilities/views.py | 3 ++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/netbox/circuits/tables.py b/netbox/circuits/tables.py index a425b3ace..a7e1b0e84 100644 --- a/netbox/circuits/tables.py +++ b/netbox/circuits/tables.py @@ -76,7 +76,16 @@ class CircuitTable(BaseTable): z_side = tables.Column( verbose_name='Z Side' ) + install_date = tables.Column( + visible=False + ) + commit_rate = tables.Column( + visible=False + ) class Meta(BaseTable.Meta): model = Circuit - fields = ('pk', 'cid', 'status', 'type', 'provider', 'tenant', 'a_side', 'z_side', 'description') + fields = ( + 'pk', 'cid', 'status', 'type', 'provider', 'tenant', 'a_side', 'z_side', 'install_date', 'commit_rate', + 'description', + ) diff --git a/netbox/utilities/tables.py b/netbox/utilities/tables.py index 9e91aebd2..bdbaa0b9b 100644 --- a/netbox/utilities/tables.py +++ b/netbox/utilities/tables.py @@ -6,13 +6,29 @@ class BaseTable(tables.Table): """ Default table for object lists """ - def __init__(self, *args, **kwargs): + def __init__(self, *args, columns=None, **kwargs): super().__init__(*args, **kwargs) # Set default empty_text if none was provided if self.empty_text is None: self.empty_text = 'No {} found'.format(self._meta.model._meta.verbose_name_plural) + # Apply custom column ordering + if columns is not None: + pk = self.base_columns.pop('pk', None) + + for name, column in self.base_columns.items(): + if name in columns: + self.columns.show(name) + else: + self.columns.hide(name) + self.sequence = columns + + # Always include PK column, if defined on the table + if pk: + self.base_columns['pk'] = pk + self.sequence.insert(0, 'pk') + class Meta: attrs = { 'class': 'table table-hover table-headings', diff --git a/netbox/utilities/views.py b/netbox/utilities/views.py index 294acb1d1..1782f1457 100644 --- a/netbox/utilities/views.py +++ b/netbox/utilities/views.py @@ -164,7 +164,8 @@ class ObjectListView(View): permissions[action] = request.user.has_perm(perm_name) # Construct the table based on the user's permissions - table = self.table(self.queryset) + columns = request.user.config.get(f"tables.{self.table.__name__}.columns") + table = self.table(self.queryset, columns=columns) if 'pk' in table.base_columns and (permissions['change'] or permissions['delete']): table.columns.show('pk')