From dee611a5e0ebe282f157b2f24d3f6300d7b31759 Mon Sep 17 00:00:00 2001 From: Gughanathan M Date: Mon, 25 Nov 2024 20:41:12 +0530 Subject: [PATCH] ability to customize action button color --- docs/actions.rst | 17 +++++++++++++++++ flask_appbuilder/actions.py | 19 ++++++++++++++++--- .../templates/appbuilder/general/lib.html | 2 +- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/docs/actions.rst b/docs/actions.rst index 43f4ff1d7a..3253cbde15 100644 --- a/docs/actions.rst +++ b/docs/actions.rst @@ -58,3 +58,20 @@ a single record:: else: self.datamodel.delete(items) return redirect(self.get_redirect()) + +To customize the button color when the ``single`` parameter is ``True``, you can assign +the ``btn_class`` value based on the intended action. +:: + + @action("delete", "Delete", "Delete all Really?", "fa-rocket", btn_class="btn-danger") + def muldelete(self, items): + if isinstance(items, list): + self.datamodel.delete_all(items) + self.update_redirect() + else: + self.datamodel.delete(items) + return redirect(self.get_redirect()) + +In this example, the ``btn_class`` parameter +is explicitly set to **btn-danger** to reflect the critical nature of the **Delete** action, ensuring +the button visually communicates its purpose effectively. \ No newline at end of file diff --git a/flask_appbuilder/actions.py b/flask_appbuilder/actions.py index 6ad0dec394..3287413206 100644 --- a/flask_appbuilder/actions.py +++ b/flask_appbuilder/actions.py @@ -1,11 +1,14 @@ class ActionItem(object): - def __init__(self, name, text, confirmation, icon, multiple, single, func): + def __init__( + self, name, text, confirmation, icon, multiple, single, btn_class, func + ): self.name = name self.text = text or name self.confirmation = confirmation self.icon = icon self.multiple = multiple self.single = single + self.btn_class = btn_class self.func = func def __repr__(self): @@ -17,7 +20,15 @@ def __repr__(self): ) -def action(name, text, confirmation=None, icon=None, multiple=True, single=True): +def action( + name, + text, + confirmation=None, + icon=None, + multiple=True, + single=True, + btn_class="btn-primary", +): """ Use this decorator to expose actions @@ -34,10 +45,12 @@ def action(name, text, confirmation=None, icon=None, multiple=True, single=True) If true will display action on list view :param single: If true will display action on show view + :param btn_class: + Bootstrap button class name. Defaults to btn-primary """ def wrap(f): - f._action = (name, text, confirmation, icon, multiple, single) + f._action = (name, text, confirmation, icon, multiple, single, btn_class) return f return wrap diff --git a/flask_appbuilder/templates/appbuilder/general/lib.html b/flask_appbuilder/templates/appbuilder/general/lib.html index 04b8a51ebd..e9443ca938 100644 --- a/flask_appbuilder/templates/appbuilder/general/lib.html +++ b/flask_appbuilder/templates/appbuilder/general/lib.html @@ -25,7 +25,7 @@ {{_(action.text)}}