Measuring open the road to one hundred fifty: implement paging function

 

 

Display achieve specified number of pages and the number of pages which display specified:

view:

 

EmployeelListView class (MethodView): 
"" "Display employee information (paging)" ""

DEF GET (Self, Page = 1):
# for data with .paginate division
employees = db.session.query (Employee) .paginate ( page , per_page = 10) # page pAGE, per page per_page bar
return render_template ( 'admin / emp- list.html', employees = employees)

View registration

 

Import the Blueprint flask from 

ADMIN = the Blueprint ( 'ADMIN', the __name__) # statement blueprint

# view to guiding the incoming registration blueprint to blueprint
from admin.views Import *

# prevent view flask register conflict, where the same view as to which variables stored registration, the view will not be reported to the problem of conflict
emp_list_view = EmployeelListView.as_view ( 'EMP_LIST')

admin.add_url_rule ( '/ EMP-List /', view_func = emp_list_view, Defaults = { 'Page':. 1}) = {# Defaults 'page': 1}: given a default value. 1
admin.add_url_rule ( '/ EMP-List / <int: Page> /', view_func = emp_list_view)
admin.add_url_rule ( '/ EMP-del / <int: ID> / ', view_func = EmployeelDeleteView.as_view (' emp_del '))
# admin.add_url_rule (' / EMP-Create / ', view_func = EmployeeCreateView.as_view (' creat_emp '))
admin.add_url_rule (' / EMP-Create / ', view_func = EmployeeCreateOrEdit.as_view('creat_emp'))
admin.add_url_rule('/emp-edit/<int:id>/', view_func=EmployeeCreateOrEdit.as_view('emp_edit'))

Page: paginate not check out the list, can not be directly traversal, traverse available .items

 

{% extends 'admin/base.html' %}


{% block stylesheet%}

{% endblock %}


{% block page_head %}
<div class="page-head">
<h3 class="m-b-less">人事列表</h3>
<div class="state-information">
<ol class="breadcrumb m-b-less bg-less">
<li><a href="#">首页</a></li>
<li><a href="#">人事管理</a></li>
<li class="active">人事列表</li>
</ol>
</div>
</div>
{% endblock %}


{% block main_content %}
<div class="wrapper">
<div class="row">
<div class="col-lg-12">
<section class="panel">
<header class="panel-heading head-border">
员工列表
</header>
<table class="table table-striped custom-table table-hover">
<thead>
<tr>
<th>编号</th>
<th class="hidden-xs">姓名</th>
<th>性别</th>
<th>生日</th>
<th>地址</th>
<th class="hidden-xs">操作</th>
</tr>
</thead>
<tbody>
{% for emp in employees.items %}
<tr>
<td><a href="#">{{ emp.id }}</a></td>
<td>{{ emp.name }}</td>
<td>{{ emp.gender }}</td>
<td>{{ emp.birthdate }}</td>
<td>{{ emp.address }}</td>
<td class="hidden-xs">
<button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
<a href="{{ url_for('.emp_edit', id=emp.id) }}" class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></a>
<a href="{{ url_for('.emp_del', id=emp.id) }}" class="btn btn-danger btn-xs"><i class="fa fa-trash-o "></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</section>
</div>
</div>
</div>
{% endblock %}


{% block script%}

{% endblock %}

effect

 

 

Plus paging buttons

Since other features may also use paging function, so come out of a separate written macros

 

{% Macro render_admin_pageination (pageination, Endpoint)%} 
<div class = "text-Center">
<UL class = "the pagination">

{# Home #}
<Li> <A the href = "{{the url_for (Endpoint, Page = 1)}} "> Home </a> </ Li>

{# Previous decided whether or not, if so, add hyperlink Previous} #
{%} IF pageination.has_prev%
<Li> < the href = A "the url_for {{(Endpoint, Page = pageination.prev_num)}}"> << </a> </ Li>
{% endif%}

{#} Page section #
{% for in pageination Page.iter_pages ()%}
{# value are rendered with a determination} #
{%}% IF Page
{# determines if this page, then mark the page to the active state} #
{% IF Page!} = pageination.page%
<Li> <a href="{{ url_for(endpoint, page=page) }}"> Page {{}} </a> </ Li>
{%}% the else
{# determines if this page, then the page labeled active state, and to remove the hyperlink} #
<Li class = "Active"> <a> Page {{}} </a> </ Li>
{% endif%}
{% endif%}
{%} endfor%

{# decided whether or not the next page, if any, to add a hyperlink Next} #
{%} IF pageination.has_next%
<Li> <A the href = "{{the url_for (Endpoint, Page = pageination.next_num )}} ">» </a> </ Li>
{% endif%}

{#} last page #
<Li> <A the href = "{{the url_for (Endpoint, Page = pageination.pages) }}">末页</a></li>

</ul>
</div>
{% endmacro %}

Import Macro

 

References and parameter passing

 

{% extends 'admin/base.html' %}
{% import 'admin/helper.html' as helper %}

{% block stylesheet%}

{% endblock %}


{% block page_head %}
<div class="page-head">
<h3 class="m-b-less">人事列表</h3>
<div class="state-information">
<ol class="breadcrumb m-b-less bg-less">
<li><a href="#">首页</a></li>
<li><a href="#">人事管理</a></li>
<li class="active">人事列表</li>
</ol>
</div>
</div>
{% endblock %}


{% block main_content %}
<div class="wrapper">
<div class="row">
<div class="col-lg-12">
<section class="panel">
<header class="panel-heading head-border">
员工列表
</header>
<table class="table table-striped custom-table table-hover">
<thead>
<tr>
<th>编号</th>
<th class="hidden-xs">姓名</th>
<th>性别</th>
<th>生日</th>
<th>地址</th>
<th class="hidden-xs">操作</th>
</tr>
</thead>
<tbody>
{% for emp in employees.items %}
<tr>
<td><a href="#">{{ emp.id }}</a></td>
<td>{{ emp.name }}</td>
<td>{{ emp.gender }}</td>
<td>{{ emp.birthdate }}</td>
<td>{{ emp.address }}</td>
<td class="hidden-xs">
<button class="btn btn-success btn-xs"><i class="fa fa-check"></i></button>
<a href="{{ url_for('.emp_edit', id=emp.id) }}" class="btn btn-primary btn-xs"><i class="fa fa-pencil"></i></a>
<a href="{{ url_for('.emp_del', id=emp.id) }}" class="btn btn-danger btn-xs"><i class="fa fa-trash-o "></i></a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
{{ helper.render_admin_pageination(employees, 'admin.emp_list') }}
</section>
</div>
</div>
</div>
{% endblock %}


{% block script%}

{% endblock %}

effect

 

Guess you like

Origin www.cnblogs.com/zhongyehai/p/11545356.html