Skip to content

Commit v0.21

kwmccabe edited this page Apr 17, 2018 · 8 revisions

v0.21 - Update get_list_opts() decorator and item_list.html page for pagination


Files changed (5)

File web/app/decorators.py MODIFIED

  • Decorator responsible for request params status, sort, order, limit, and page.
  • Caller responsible for itemcnt, pagecnt, and offset.
  • Remove model param and related call to db.session.query(model).count().
-def get_list_opts( model, session_key='list_opts' ):
+def get_list_opts( session_key='list_opts' ):

...

                 session[session_key] = { \
                     'itemcnt' : 0, \
+                    'pagecnt' : 0, \
                     'status'  : 'all', \
                     'sort'    : 'id', \
                     'order'   : 'asc', \
                     'offset'  : 0, \
-                    'limit'   : 0, \
+                    'limit'   : 10, \
+                    'page'    : 1, \
                     }
 
...

             status = request.values.get('status', S['status'])
             sort   = request.values.get('sort',   S['sort'])
             order  = request.values.get('order',  S['order'])
-            offset = int(request.values.get('offset',  S['offset']))
             limit  = int(request.values.get('limit', S['limit']))
+            page   = int(request.values.get('page', S['page']))
 
-            S['itemcnt'] = db.session.query(model).count()

 ...

-            if offset > 0 and offset != S['offset']:
-                S['offset'] = offset
+            if page > 0 and page != S['page']:
+                S['page'] = page

File web/app/item/templates/item_list.html MODIFIED

  • Add links for First, Previous, Next, and Last Pages.
  • Update message, "Page page of pagecnt".
  • Add checkbox for call to checkAll() in flaskapp.js.
 <td class="text-right">
-    <span>Page 1 of 1</span>
+    {% if session[opts_key]['page'] > 1 %}
+        <a href="{{ url_for('.item_list') }}?page=1"><span class="glyphicon glyphicon-backward" aria-hidden="true"></span></a>
+        <a href="{{ url_for('.item_list') }}?page={{ session[opts_key]['page'] - 1 }}"><span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span></a>
+    {% elif session[opts_key]['pagecnt'] > 1 %}
+        <span class="glyphicon glyphicon-backward" aria-hidden="true"></span>
+        <span class="glyphicon glyphicon-arrow-left" aria-hidden="true"></span>
+    {% endif %}
+    <span>Page {{ session[opts_key]['page'] }} of {{ session[opts_key]['pagecnt'] }}</span>
+    {% if session[opts_key]['page'] < session[opts_key]['pagecnt'] %}
+        <a href="{{ url_for('.item_list') }}?page={{ session[opts_key]['page'] + 1 }}"><span class="glyphicon glyphicon-arrow-right" aria-hidden="true"></span></a>
+        <a href="{{ url_for('.item_list') }}?page={{ session[opts_key]['pagecnt'] }}"><span class="glyphicon glyphicon-forward" aria-hidden="true"></span></a>
+    {% elif session[opts_key]['pagecnt'] > 1 %}
+        <span class="glyphicon glyphicon-arrow-right" aria-hidden="true"></span>
+        <span class="glyphicon glyphicon-forward" aria-hidden="true"></span>
+    {% endif %}
 </td>

...

 <table class="table table-condensed table-hover">
 <tr>
-<th></th>
+<th>
+<input type="checkbox" class="checkall" onclick="checkAll('item_id',this.checked);" />
+</th>

File web/app/item/views.py MODIFIED

  • The decorator get_list_opts() is responsible for updating the request params status, sort, order, limit, and page.
  • itemcnt and pagecnt depend upon the result of rows.filter(), so we'll set those here.
  • Validate page, calculate offset, and reset the session.
+import math
 
... 
 
 @item.route('/admin/item/list')
-@get_list_opts(ItemModel,'item_list_opts')
+@get_list_opts('item_list_opts')
 def item_list():

...

     if S['status'] in ['active', 'inactive']:
         rows = rows.filter(ItemModel.active == (S['status'] == 'active'))
+
+    S['itemcnt'] = rows.count()
+    S['pagecnt'] = int(math.ceil( float(S['itemcnt'])/float(S['limit']) ))
+
+    if S['page'] > S['pagecnt']:
+        S['page'] = S['pagecnt']
+    S['offset'] = 0
+    if ((S['page'] - 1) * S['limit']) < S['itemcnt']:
+        S['offset'] = (S['page'] - 1) * S['limit']
+    session[opts_key] = S

File web/app/static/css/flaskapp.css MODIFIED

 #footer_hr {
     margin-bottom: 0;
 }
+#debuginfo {
+    padding: 15px;
+}

... 

-#debuginfo { padding: 15px;}

File web/app/static/js/flaskapp.js MODIFIED

  • Function to check/uncheck all checkboxes with a name attribute matching the input_name.
  • The input_checked value should be True or False.
  • eg, <input type="checkbox" class="checkall" onclick="checkAll('item_id',this.checked);" />
+/**
+ * Check/Uncheck all checkboxes with name attribute matching 'input_name'
+ * @see item_list.html
+ */
+function checkAll( input_name, input_checked ) {
+    //alert( "checkAll( "+input_name+", "+input_checked+" )" );
+    $("input[type='checkbox']").each( function () {
+        if ($(this).prop("name") != input_name) { return; }
+        $(this).prop( "checked", input_checked );
+    });
+}

Commit-v0.20 | Commit-v0.21 | Commit-v0.22

Clone this wiki locally