如何为数据表

时间:2015-05-13 19:05:54

标签: jquery python datatable pagination flask-sqlalchemy

我有一个数据库表,它有几千行,正在开发一个crud管理员web应用程序。我使用数据表来显示所有行(查看全部)。当我使用数据表加载所有行时,加载页面需要大约30秒。我想使用ajax分页来避免它。

Python代码:

我从表中获取所有行的python函数看起来像这样

 @companies.route('/list', methods=['GET'])
 @requires_session_auth
 def company_list():
    try:
       all_companies = Company.query.all()
       return render_template('company_list.html', company=all_companies)
    except:
    logger.exception('Error rendering Company list.')


 return redirect(url_for('index'))

Html代码:

 {% extends "base.html" %}
 {% block subtitle %}Companies{% endblock %}
 {% block content %}

 <h2>Companies</h2>

 <table id="datatable" class="display" cellspacing="0" width="100%">
   <thead>
    <tr>
        <th align="center">Actions</th>
        <th align="left">ID</th>
        <th align="left">Label</th>
        <th align="left">Company Name</th>
    </tr>
   </thead>
   <tbody>
     {% for image in images %}
     <tr>
       <td nowrap align="center">
        <a href="">Edit</a>
            &nbsp;&nbsp;
         <a href="" class="del_link">Delete</a>
          </td>
          <td nowrap align="left">{{ company.id }}</td>
          <td nowrap align="left">{{ company.label }}</td>
          <td nowrap align="left">{{ company.companyname }}</td>
         </tr>
       {% endfor %}
    </tbody>
   </table>
  {% endblock %}
  {% block afterbody %}
  <script>
      $(document).ready(function () {
      $('#datatable').dataTable({
         stateSave: true
       });
   </script>
  {% endblock %}

如何创建一个python函数,它在数据表上加载前10行并单击分页按钮(第2页),它应该从db表中加载下10个数据。如何在html中创建python函数和数据表。以下是php中的示例分页https://datatables.net/examples/server_side/simple.html

1 个答案:

答案 0 :(得分:0)

你见过这个项目https://github.com/Pegase745/sqlalchemy-datatables/tree/master/examples/flask_tut吗?它可能会有所帮助。

您需要的只是运行--always