From 388e0433fd6f55c2e36ad77922d6f6f651ea7047 Mon Sep 17 00:00:00 2001
From: Stephan Porada <sporada@uni-bielefeld.de>
Date: Thu, 8 Aug 2019 16:06:41 +0200
Subject: [PATCH] Add searchable User table.

---
 app/main/views.py                |  5 +++--
 app/tables.py                    |  8 ++++----
 app/templates/main/admin.html.j2 | 18 +++++++++++++++++-
 3 files changed, 24 insertions(+), 7 deletions(-)

diff --git a/app/main/views.py b/app/main/views.py
index aaddc131..5f4d2c7c 100644
--- a/app/main/views.py
+++ b/app/main/views.py
@@ -60,6 +60,7 @@ def dashboard():
 def for_admins_only():
     users = User.query.order_by(User.username).all()
     items = [AdminUserItem(u.username, u.email, u.role_id, u.confirmed) for u in users]
-    table = AdminUserTable(items)
+    table = AdminUserTable(items).__html__()  # converts table object to html string
+    table = table.replace('tbody', 'tbody class="list"', 1)  # add class list to tbody element. Needed by list.js
     return render_template('main/admin.html.j2', title='Administration tools',
-                           table=table.__html__())
+                           table=table)
diff --git a/app/tables.py b/app/tables.py
index 44c6401e..ea967e3e 100644
--- a/app/tables.py
+++ b/app/tables.py
@@ -6,10 +6,10 @@ class AdminUserTable(Table):
     Declares the table describing colum by column.
     """
     classes = ['highlight', 'responsive-table']
-    username = Col('Username')
-    email = Col('Email')
-    role_id = Col('Role')
-    confirmed = Col('Confrimed Status')
+    username = Col('Username', column_html_attrs={'class': 'username'})
+    email = Col('Email', column_html_attrs={'class': 'email'})
+    role_id = Col('Role', column_html_attrs={'class': 'role'})
+    confirmed = Col('Confrimed Status', column_html_attrs={'class': 'confirmed'})
 
 
 class AdminUserItem(object):
diff --git a/app/templates/main/admin.html.j2 b/app/templates/main/admin.html.j2
index 1420ca4c..c5673203 100644
--- a/app/templates/main/admin.html.j2
+++ b/app/templates/main/admin.html.j2
@@ -5,8 +5,24 @@
   <div class="card large">
     <div class="card-content">
       <span class="card-title">User list</span>
-      {{ table }}
+      <div id="users">
+        <div class="input-field">
+          <i class="material-icons prefix">search</i>
+          <input id="search-corpus" class="search" type="text"></input>
+          <label for="search-corpus">Search users</label>
+        </div>
+        {{ table }}
+        <ul class="pagination"></ul>
+      </div>
     </div>
   </div>
 </div>
+<script type="text/javascript">
+var options = {
+  valueNames: ['username', 'email', 'role', 'confirmed'],
+  page: 3,
+  pagination: true
+};
+var userList = new List('users', options);
+</script>
 {% endblock %}
-- 
GitLab