commit 9a642663cde612e797a13540d9b73063249500de
parent a6a036ba56805f8ad24fa295eb3f9114941b4550
Author: William Lindholm <william_lindholm@outlook.com>
Date: Sun, 5 Nov 2023 20:09:21 +0100
Implemented callback function support for tablebuilder
Diffstat:
4 files changed, 29 insertions(+), 26 deletions(-)
diff --git a/WebInterface/routes.py b/WebInterface/routes.py
@@ -36,20 +36,26 @@ def home():
@web_interface.route('/integrations')
def integrations():
# Define headers and rows for the messages table
- headers = ["ID", "Message", "Date"]
+ headers = ["ID", "Message", "Date", "Delete"]
rows = [
[1, "Hello World!", "2023-11-01"],
[2, "Another Message", "2023-11-02"],
- # ... add more rows as needed
]
+ def delete_link(row_data):
+ # Assuming row_data is a dictionary that contains an 'id' key
+ id = row_data[0]
+ # Return a string of HTML. The 'material-icons' class is used for Material Icons
+ return f'<a href="/message/delete/{id}" class="delete-icon"><i class="material-icons">delete</i></a>'
+
# Initialize TableBuilder and configure the table
table_builder = TableBuilder()
table_builder.set_headers(headers)
table_builder.add_rows(rows)
table_builder.set_pagination(enabled=True, page_size=5)
table_builder.set_sortable(["ID", "Date"])
- table_builder.add_callback_column("Delete", "deleteMessage")
+ table_builder.add_callback_column(delete_link)
+ table_builder.set_on_click("/message/{0}")
# Build the TableInstance
table_instance = table_builder.build()
diff --git a/WebInterface/tableBuilder.py b/WebInterface/tableBuilder.py
@@ -48,8 +48,8 @@ class TableBuilder:
def set_sortable(self, sortable_columns):
self.table_instance.set_sortable_columns(sortable_columns)
- def add_callback_column(self, name, callback):
- self.table_instance.set_callback_column((name, callback))
+ def add_callback_column(self, callback):
+ self.table_instance.set_callback_column(callback)
def set_on_click(self, url_template):
self.table_instance.set_row_click_url_template(url_template)
diff --git a/static/css/main.css b/static/css/main.css
@@ -79,4 +79,8 @@ h1 {
h1.mobile {
text-align: center;
flex-grow: 1;
+}
+
+.delete-icon {
+ color: red;
}
\ No newline at end of file
diff --git a/templates/table.html b/templates/table.html
@@ -1,15 +1,3 @@
-{% if table.pagination_enabled %}
- <ul class="pagination">
- <!-- Add Pagination Logic Here -->
- {% set total_pages = ((table.rows|length / table.page_size) | round(0, 'ceil')) | int %}
- {% for i in range(1, total_pages + 1) %}
- <li class="{% if table.current_page == i %}active{% else %}waves-effect{% endif %}">
- <a href="?page={{ i }}">{{ i }}</a>
- </li>
- {% endfor %}
- </ul>
-{% endif %}
-
<table id="table-instance" class="highlight">
<thead>
<tr>
@@ -24,10 +12,6 @@
{% endif %}
</th>
{% endfor %}
- <!-- Additional header for callback actions if any -->
- {% if table.callback_column %}
- <th>{{ table.callback_column }}</th>
- {% endif %}
</tr>
</thead>
<tbody>
@@ -39,11 +23,20 @@
{% endfor %}
<!-- Callback action buttons if any -->
{% if table.callback_column %}
- <td>
- <button onclick="{{ table.callback_column.function(row[0]) }}">{{ table.callback_column.name }}</button>
- </td>
+ <td>{{ table.callback_column(row) | safe }}</td>
{% endif %}
</tr>
{% endfor %}
</tbody>
-</table>
-\ No newline at end of file
+</table>
+
+{% if table.pagination_enabled %}
+ <ul class="pagination">
+ {% set total_pages = ((table.rows|length / table.page_size) | round(0, 'ceil')) | int %}
+ {% for i in range(1, total_pages + 1) %}
+ <li class="{% if table.current_page == i %}active{% else %}waves-effect{% endif %}">
+ <a href="?page={{ i }}">{{ i }}</a>
+ </li>
+ {% endfor %}
+ </ul>
+{% endif %}
+\ No newline at end of file