diff --git a/CHANGES.rst b/CHANGES.rst index 59211ac2..2554c735 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -15,6 +15,8 @@ Release date: - Release date: - +- Add a ``custom_actions`` parameter for the ``render_table`` macro. When passing a list of tuples ``[(title, bootstrap icon, link)]`` to the ``custom_actions`` parameter, the ``render_table`` macro will create an icon (link) on the action header for each tuple in the list. The title text (first index of each tuple) will show when hovering over each ``custom_actions`` button. + 1.6.0 ----- diff --git a/docs/macros.rst b/docs/macros.rst index d97d3434..348a130d 100644 --- a/docs/macros.rst +++ b/docs/macros.rst @@ -433,6 +433,7 @@ API responsive_class='table-responsive',\ show_actions=False,\ actions_title='Actions',\ + custom_actions=None,\ view_url=None,\ edit_url=None,\ delete_url=None,\ @@ -451,6 +452,9 @@ API :param responsive_class: The responsive class to apply to the table. Default is ``'table-responsive'``. :param show_actions: Whether to display the actions column. Default is ``False``. :param actions_title: Title for the actions column header. Default is ``'Actions'``. + :param custom_actions: A list of tuples for creating custom action buttons, where each tuple contains + ('Title Text displayed on hover', 'bootstrap icon name', 'url_for()') + (e.g.``[('Run', 'play-fill', url_for('run_report', report_id=':primary_key'))]``). :param view_url: URL to use for the view action. :param edit_url: URL to use for the edit action. :param delete_url: URL to use for the delete action. diff --git a/flask_bootstrap/templates/bootstrap/table.html b/flask_bootstrap/templates/bootstrap/table.html index ec1f449e..717e601c 100644 --- a/flask_bootstrap/templates/bootstrap/table.html +++ b/flask_bootstrap/templates/bootstrap/table.html @@ -9,6 +9,7 @@ responsive_class='table-responsive', show_actions=False, actions_title='Actions', + custom_actions=None, view_url=None, edit_url=None, delete_url=None, @@ -37,6 +38,9 @@ + {% if custom_actions %} + {% from 'bootstrap/utils.html' import render_icon %} + {% endif %} {% for row in data %} {% for title in titles %} @@ -48,6 +52,14 @@ {% endfor %} {% if show_actions %} + {% if custom_actions %} + {% for (action_name, action_icon, action_url) in custom_actions %} + {{ render_icon(action_icon) }} + {% endfor %} + {% endif %} {% if view_url %} View {% endif %} diff --git a/tests/test_render_table.py b/tests/test_render_table.py index 707fdc34..3190bc41 100644 --- a/tests/test_render_table.py +++ b/tests/test_render_table.py @@ -148,6 +148,10 @@ class Message(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.Text) + @app.route('/table//resend') + def test_resend_message(message_id): + return 'Re-sending {}'.format(message_id) + @app.route('/table//view') def test_view_message(message_id): return 'Viewing {}'.format(message_id) @@ -169,14 +173,20 @@ def test(): messages = pagination.items titles = [('id', '#'), ('text', 'Message')] return render_template_string(''' - {% from 'bootstrap/table.html' import render_table %} - {{ render_table(messages, titles, show_actions=True, - view_url=url_for('test_view_message', message_id=':primary_key'), - new_url=url_for('test_create_message')) }} - ''', titles=titles, messages=messages) + {% from 'bootstrap/table.html' import render_table %} + {{ render_table(messages, titles, show_actions=True, + custom_actions=[ + ('Resend', 'bootstrap-reboot', url_for('test_resend_message', message_id=':primary_key')) + ], + view_url=url_for('test_view_message', message_id=':primary_key'), + new_url=url_for('test_create_message')) }} + ''', titles=titles, messages=messages) response = client.get('/table') data = response.get_data(as_text=True) + assert 'icons/bootstrap-icons.svg#bootstrap-reboot' in data + assert 'href="/table/1/resend"' in data + assert 'title="Resend">' in data assert '' in data assert '' in data assert 'New' in data