1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

#8787: Fix toggling of PK table column

This commit is contained in:
jeremystretch
2022-03-03 14:09:32 -05:00
parent 62a1d4b3e0
commit 64acfc3187

View File

@ -41,23 +41,20 @@ class BaseTable(tables.Table):
if self.empty_text is None:
self.empty_text = f"No {self._meta.model._meta.verbose_name_plural} found"
# Hide non-default columns
default_columns = [*getattr(self.Meta, 'default_columns', self.Meta.fields), *self.exempt_columns]
for column in self.columns:
if column.name not in default_columns:
self.columns.hide(column.name)
# Apply custom column ordering for user
# Determine the table columns to display by checking the following:
# 1. User's configuration for the table
# 2. Meta.default_columns
# 3. Meta.fields
selected_columns = None
if user is not None and not isinstance(user, AnonymousUser):
selected_columns = user.config.get(f"tables.{self.__class__.__name__}.columns")
if selected_columns:
if not selected_columns:
selected_columns = getattr(self.Meta, 'default_columns', self.Meta.fields)
# Show only persistent or selected columns
for name, column in self.columns.items():
if name in [*self.exempt_columns, *selected_columns]:
self.columns.show(name)
else:
self.columns.hide(name)
# Hide non-selected columns which are not exempt
for column in self.columns:
if column.name not in [*selected_columns, *self.exempt_columns]:
self.columns.hide(column.name)
# Rearrange the sequence to list selected columns first, followed by all remaining columns
# TODO: There's probably a more clever way to accomplish this