-
Notifications
You must be signed in to change notification settings - Fork 8
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
- +7 -6 [M] web/app/decorators.py
- +19 -2 [M] web/app/item/templates/item_list.html
- +13 -2 [M] web/app/item/views.py
- +5 -1 [M] web/app/static/css/flaskapp.css
- +12 -0 [M] web/app/static/js/flaskapp.js
- Decorator responsible for request params
status
,sort
,order
,limit
, andpage
. - Caller responsible for
itemcnt
,pagecnt
, andoffset
. - 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
- Add links for First, Previous, Next, and Last Pages.
- Update message, "Page
page
ofpagecnt
". - Add checkbox for call to
checkAll()
inflaskapp.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>
- The decorator
get_list_opts()
is responsible for updating the request paramsstatus
,sort
,order
,limit
, andpage
. -
itemcnt
andpagecnt
depend upon the result ofrows.filter()
, so we'll set those here. - Validate
page
, calculateoffset
, and reset thesession
.
+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
#footer_hr {
margin-bottom: 0;
}
+#debuginfo {
+ padding: 15px;
+}
...
-#debuginfo { padding: 15px;}
- Function to check/uncheck all checkboxes with a name attribute matching the
input_name
. - The
input_checked
value should beTrue
orFalse
. - 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
- FlaskApp Tutorial
- Table of Contents
- About
- Application Setup
- Modules, Templates, and Layouts
- Database Items, Forms, and CRUD
- List Filter, Sort, and Paginate
- Users and Login
- Database Relationships
- API Module, HTTPAuth and JSON
- Refactoring User Roles and Item Status
- AJAX and Public Pages