Python Flask SQLAlchemy Pagination - Datatable bootstap
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #app.py from flask import Flask, render_template, flash, redirect, url_for, request from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) app.config[ 'SECRET_KEY' ] = 'cairocoders-ednalan' db = SQLAlchemy(app) class Employees(db.Model): id = db.Column(db.Integer, primary_key = True ) fullname = db.Column(db.String( 150 )) position = db.Column(db.String( 150 )) office = db.Column(db.String( 150 )) age = db.Column(db.Integer) startdate = db.Column(db.String( 150 )) salary = db.Column(db.String( 150 )) @app .route( '/employees/<int:page_num>' ) def employees(page_num): employees = Employees.query.paginate(per_page = 5 , page = page_num, error_out = True ) return render_template( 'index.html' , employees = employees) if __name__ = = '__main__' : app.run(debug = True ) < / int :page_num> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | //index.hmtl <html> <head> <title>Python Flask SQLAlchemy Pagination</title> <link href= "https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel= "stylesheet" id= "bootstrap-css" > </head> <body> <div class = "container" > <div class = "row" > <p><h2>Python Flask SQLAlchemy Pagination - Datatable bootstap</h2> </p> <table id= "example" class = "table table-striped table-bordered" style= "width:100%" > <thead> <tr> <th><input type= "checkbox" onclick= "checkAll(this)" ></th> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date </th> <th>Salary</th> </tr> </thead> <tbody> {% for employee in employees.items %} <tr> <td><input type= "checkbox" name= "" ></td> <td>{{ employee.fullname}}</td> <td>{{ employee.position}}</td> <td>{{ employee.office}}</td> <td>{{ employee.age}}</td> <td>{{ employee.startdate}}</td> <td>{{ employee.salary}}</td> </tr> {% endfor %} </tbody> <tfoot> <tr> <th></th> <th>Name</th> <th>Position</th> <th>Office</th> <th>Age</th> <th>Start date </th> <th>Salary</th> </tr> </tfoot> </table> </div> <ul class = "pagination" > {% if employees.has_prev %} <li class = "page-item" ><a class = "page-link" href= "{{ url_for('employees', page_num=employees.prev_num) }}" >Previous</a></li> {% else %} <li class = "page-item disabled" ><span class = "page-link" >Previous</span> {% endif %} </li> {% for page in employees.iter_pages(left_edge=3, right_edge=3) %} {% if page %} <li class = "page-item" ><a class = "page-link" href= "{{ url_for('employees', page_num=page) }}" >{{ page }}</a></li> {% else %} <li class = "page-item disabled" id= "example_ellipsis" ><a href= "#" class = "page-link" >…</a></li> {% endif %} {% endfor %} {% if employees.has_next %} <li class = "page-item" ><a class = "page-link" href= "{{ url_for('employees', page_num=employees.next_num) }}" >Next</a></li> {% else %} <li class = "page-item disabled" ><span class = "page-link" >Next</span> {% endif %} </ul> </div> <style> table{ width:100%; } #example_filter{ float:right; } #example_paginate{ float:right; } label { display: inline-flex; margin-bottom: .5rem; margin-top: .5rem; } .page-item.disabled .page-link { color: #6c757d; pointer-events: none; cursor: auto; background-color: #fff; border-color: #dee2e6; } </style> <script> function checkAll(bx) { var cbs = document.getElementsByTagName( 'input' ); for ( var i=0; i < cbs.length; i++) { if (cbs[i].type == 'checkbox' ) { cbs[i].checked = bx.checked; } } } </script> </body> </html> |