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:
@ -41,40 +41,37 @@ class BaseTable(tables.Table):
|
|||||||
if self.empty_text is None:
|
if self.empty_text is None:
|
||||||
self.empty_text = f"No {self._meta.model._meta.verbose_name_plural} found"
|
self.empty_text = f"No {self._meta.model._meta.verbose_name_plural} found"
|
||||||
|
|
||||||
# Hide non-default columns
|
# Determine the table columns to display by checking the following:
|
||||||
default_columns = [*getattr(self.Meta, 'default_columns', self.Meta.fields), *self.exempt_columns]
|
# 1. User's configuration for the table
|
||||||
for column in self.columns:
|
# 2. Meta.default_columns
|
||||||
if column.name not in default_columns:
|
# 3. Meta.fields
|
||||||
self.columns.hide(column.name)
|
selected_columns = None
|
||||||
|
|
||||||
# Apply custom column ordering for user
|
|
||||||
if user is not None and not isinstance(user, AnonymousUser):
|
if user is not None and not isinstance(user, AnonymousUser):
|
||||||
selected_columns = user.config.get(f"tables.{self.__class__.__name__}.columns")
|
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
|
# Hide non-selected columns which are not exempt
|
||||||
for name, column in self.columns.items():
|
for column in self.columns:
|
||||||
if name in [*self.exempt_columns, *selected_columns]:
|
if column.name not in [*selected_columns, *self.exempt_columns]:
|
||||||
self.columns.show(name)
|
self.columns.hide(column.name)
|
||||||
else:
|
|
||||||
self.columns.hide(name)
|
|
||||||
|
|
||||||
# Rearrange the sequence to list selected columns first, followed by all remaining columns
|
# Rearrange the sequence to list selected columns first, followed by all remaining columns
|
||||||
# TODO: There's probably a more clever way to accomplish this
|
# TODO: There's probably a more clever way to accomplish this
|
||||||
self.sequence = [
|
self.sequence = [
|
||||||
*[c for c in selected_columns if c in self.columns.names()],
|
*[c for c in selected_columns if c in self.columns.names()],
|
||||||
*[c for c in self.columns.names() if c not in selected_columns]
|
*[c for c in self.columns.names() if c not in selected_columns]
|
||||||
]
|
]
|
||||||
|
|
||||||
# PK column should always come first
|
# PK column should always come first
|
||||||
if 'pk' in self.sequence:
|
if 'pk' in self.sequence:
|
||||||
self.sequence.remove('pk')
|
self.sequence.remove('pk')
|
||||||
self.sequence.insert(0, 'pk')
|
self.sequence.insert(0, 'pk')
|
||||||
|
|
||||||
# Actions column should always come last
|
# Actions column should always come last
|
||||||
if 'actions' in self.sequence:
|
if 'actions' in self.sequence:
|
||||||
self.sequence.remove('actions')
|
self.sequence.remove('actions')
|
||||||
self.sequence.append('actions')
|
self.sequence.append('actions')
|
||||||
|
|
||||||
# Dynamically update the table's QuerySet to ensure related fields are pre-fetched
|
# Dynamically update the table's QuerySet to ensure related fields are pre-fetched
|
||||||
if isinstance(self.data, TableQuerysetData):
|
if isinstance(self.data, TableQuerysetData):
|
||||||
|
Reference in New Issue
Block a user