layui table laypage 实现搜索+排序+服务端分页

本文总结了layui table的搜索、排序和服务端分页实现。通过监听事件,使用ajax传递页码、显示数量、排序字段和排序类型,结合数据库分页查询优化SQL语句,提高查询效率。在laypage的jump事件中更新表格,实现动态加载。
摘要由CSDN通过智能技术生成

现在想对layui table做一个总结,比较难的是分页,数据量大的时候要实现服务端分页,前端分页的话数据量太大估计不行,在sql语句里优化和只查询当前页需要显示的数据,这样速度就会快很多。
我将table的操作分了几个部分

页面如下所示,初始状态只显示搜索栏,点击“Inquire”后显示相应的数据。
在这里插入图片描述
基本步骤是:点击查询之后,使用ajax 往后台请求数据,
ajax的参数是页码,显示数量,排序的字段,和排序的type(asc或者desc),其他参数自己设定
请求成功后先渲染laypage,在jump里渲染table,因为每次换页码都要触发jump ,所以只能在jump 里渲染table 了。
点击分页和改变每页显示条数都会再次触发ajax 请求。
我一般都会把table 的渲染单独写在一个function 里方便调用

渲染表格

//curr 是当前页
//obj 是排序对象
//limit 是显示数据的条数
 function initTable(limit,curr,obj)
 {
   
     $.ajax({
   
         type:'post',
         url: '' + rootpath + 'LabelQuery/getIMLabel',
         dataType: 'json',
         data: {
   
             'limit':limit,
             'curr': curr,
             'sortField':obj.field,
             'sortType': obj.type,
             'material': $.trim($('#material').val()),
             'demand':$.trim($('#demand').val()),
             'demandItem':$.trim($('#demandItem').val()),
             'demandType': $.trim($('#demandType').val()),
             'deliveryDate': $.trim($('#deliveryDate').val()),
             'CreatedOn': $.trim($('#CreatedOn').val()),
             'Status': $.trim($('#Status').val()),
             'enoughSupply': $.trim($('#enoughSupply').val()),
         },
         beforeSend:function(){
   
             this.index=layer.load(2);
         },
         complete:function(){
   
             layer.close(this.index);
         },
         success:function(data, status, xhr){
   
             if(xhr.status==200){
   
                 if(data.code==-1){
   
                     window.location.replace(''+rootpath+'Home/Login')
                 }else{
   
                     //使用laypage
                     laypage.render({
   
                         elem: 'page'
                         , curr: curr
                         , limit: limit
                         , count: data
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个使用layui+Python+Flask+MySQL实现分页代码示例: HTML代码: ```html <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>分页示例</title> <link rel="stylesheet" href="https://cdn.staticfile.org/layui/2.5.6/css/layui.min.css"> </head> <body> <div class="layui-container"> <table class="layui-table"> <thead> <tr> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>地址</th> </tr> </thead> <tbody id="table_data"></tbody> </table> <div id="page"></div> </div> <script src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/layui/2.5.6/layui.min.js"></script> <script> layui.use(['laypage', 'layer'], function(){ var laypage = layui.laypage, layer = layui.layer; laypage.render({ elem: 'page', limit: 10, curr: 1, count: 100, jump: function(obj, first){ if(!first){ $.ajax({ url: '/get_data', type: 'POST', dataType: 'json', data: { page: obj.curr, limit: obj.limit }, success: function(data){ if(data.code == 0){ var html = ''; for(var i=0;i<data.data.length;i++){ html += '<tr>'; html += '<td>'+data.data[i].name+'</td>'; html += '<td>'+data.data[i].age+'</td>'; html += '<td>'+data.data[i].gender+'</td>'; html += '<td>'+data.data[i].address+'</td>'; html += '</tr>'; } $('#table_data').html(html); } else{ layer.msg('获取数据失败!'); } }, error: function(){ layer.msg('获取数据失败!'); } }); } } }); }); </script> </body> </html> ``` Python代码: ```python from flask import Flask, render_template, request, jsonify import pymysql app = Flask(__name__) @app.route('/') def index(): return render_template('index.html') @app.route('/get_data', methods=['POST']) def get_data(): page = request.form.get('page', 1, type=int) limit = request.form.get('limit', 10, type=int) offset = (page - 1) * limit conn = pymysql.connect(host='localhost', user='root', password='123456', database='test', charset='utf8') cursor = conn.cursor(pymysql.cursors.DictCursor) cursor.execute('SELECT * FROM users LIMIT %s, %s', (offset, limit)) data = cursor.fetchall() cursor.execute('SELECT COUNT(*) FROM users') count = cursor.fetchone()['COUNT(*)'] cursor.close() conn.close() return jsonify({'code': 0, 'msg': '', 'data': data, 'count': count}) if __name__ == '__main__': app.run(debug=True) ``` 这个示例使用了jQuery和layui库,通过ajax异步获取分页数据。在Python部分,使用了pymysql库连接MySQL数据库,通过分页查询获取数据并返回给前端。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值