Initial support for table column reordering

This commit is contained in:
Jeremy Stretch 2020-04-27 16:56:25 -04:00
parent 4971054c34
commit 0ee1112d9d
3 changed files with 29 additions and 3 deletions

View File

@ -76,7 +76,16 @@ class CircuitTable(BaseTable):
z_side = tables.Column( z_side = tables.Column(
verbose_name='Z Side' verbose_name='Z Side'
) )
install_date = tables.Column(
visible=False
)
commit_rate = tables.Column(
visible=False
)
class Meta(BaseTable.Meta): class Meta(BaseTable.Meta):
model = Circuit 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',
)

View File

@ -6,13 +6,29 @@ class BaseTable(tables.Table):
""" """
Default table for object lists Default table for object lists
""" """
def __init__(self, *args, **kwargs): def __init__(self, *args, columns=None, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
# Set default empty_text if none was provided # Set default empty_text if none was provided
if self.empty_text is None: if self.empty_text is None:
self.empty_text = 'No {} found'.format(self._meta.model._meta.verbose_name_plural) 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: class Meta:
attrs = { attrs = {
'class': 'table table-hover table-headings', 'class': 'table table-hover table-headings',

View File

@ -164,7 +164,8 @@ class ObjectListView(View):
permissions[action] = request.user.has_perm(perm_name) permissions[action] = request.user.has_perm(perm_name)
# Construct the table based on the user's permissions # 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']): if 'pk' in table.base_columns and (permissions['change'] or permissions['delete']):
table.columns.show('pk') table.columns.show('pk')