From ff2ccfd6707b895be5fbeb602dc969700eda17c3 Mon Sep 17 00:00:00 2001 From: Hunter Johnston Date: Sun, 19 Jun 2022 19:12:52 -0400 Subject: [PATCH] Closes #9525: Added split button functionality to ActionsColumn --- netbox/netbox/tables/columns.py | 43 ++++++++++++++++++++++++++------- netbox/netbox/tables/tables.py | 6 +---- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/netbox/netbox/tables/columns.py b/netbox/netbox/tables/columns.py index e82e8a1ea..9067b13f2 100644 --- a/netbox/netbox/tables/columns.py +++ b/netbox/netbox/tables/columns.py @@ -175,6 +175,7 @@ class ActionsColumn(tables.Column): :param actions: The ordered list of dropdown menu items to include :param extra_buttons: A Django template string which renders additional buttons preceding the actions dropdown + :param split_actions: When True, converts the actions dropdown menu into a split button with first action as the direct button link and icon (default: True) """ attrs = {'td': {'class': 'text-end text-nowrap noprint'}} empty_values = () @@ -184,10 +185,11 @@ class ActionsColumn(tables.Column): 'changelog': ActionsItem('Changelog', 'history'), } - def __init__(self, *args, actions=('edit', 'delete', 'changelog'), extra_buttons='', **kwargs): + def __init__(self, *args, actions=('edit', 'delete', 'changelog'), extra_buttons='', split_actions=True, **kwargs): super().__init__(*args, **kwargs) self.extra_buttons = extra_buttons + self.split_actions = split_actions # Determine which actions to enable self.actions = { @@ -210,19 +212,42 @@ class ActionsColumn(tables.Column): # Compile actions menu links = [] user = getattr(request, 'user', AnonymousUser()) - for action, attrs in self.actions.items(): + for idx, (action, attrs) in enumerate(self.actions.items()): permission = f'{model._meta.app_label}.{attrs.permission}_{model._meta.model_name}' if attrs.permission is None or user.has_perm(permission): url = reverse(get_viewname(model, action), kwargs={'pk': record.pk}) - links.append( - f'
  • ' - f' {attrs.title}
  • ' - ) + + # If only a single action exists, render a regular button + if len(self.actions.items()) == 1: + html += ( + f'' + f'' + ) + + # Creates split button for the first action with direct link and icon + elif self.split_actions and idx == 0: + html += ( + f'' + f'' + f'' + ) + + # Creates standard action dropdown menu items + else: + links.append( + f'
  • ' + f' {attrs.title}
  • ' + ) + + # Create the actions dropdown menu if links: + dropdown_icon = '' if self.split_actions else '' + dropdown_class = '' if self.split_actions else '' html += ( - f'' - f'' - f'' + f'{dropdown_class}' + f'' + f'{dropdown_icon}' + f'Toggle Dropdown' f'' ) diff --git a/netbox/netbox/tables/tables.py b/netbox/netbox/tables/tables.py index de73bf6fe..8c5fb039c 100644 --- a/netbox/netbox/tables/tables.py +++ b/netbox/netbox/tables/tables.py @@ -165,11 +165,7 @@ class NetBoxTable(BaseTable): linkify=True, verbose_name='ID' ) - actions = columns.ActionsColumn( - extra_buttons=""" - - """ - ) + actions = columns.ActionsColumn() exempt_columns = ('pk', 'actions')