flask+datatables服务器分页

html代码

        <table class="text-c table table-border table-bordered table-bg display table-sort radius table-striped table-hover">
        <script type="text/javascript" src="{{ url_for('static',filename='lib/jquery/1.9.1/jquery.min.js') }}"></script>
            <thead>
                <tr class="text-c">
                    <th width="25"><input type="checkbox" name="all" value="all"></th>
                    <th>queue ID</th>
                    <th>发送人</th>
                    <th>接收人</th>
                    <th>发送时间</th>
                    <th>主题</th>
                    <th>邮件大小</th>
                    <th>发送状态</th>
                    <th>失败原因</th>
                    <th>邮件编码</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>   
            </tbody>
        </table>

js代码

$(document).ready(function() {
    $('.table-sort').DataTable( {
        //"aaSorting": [[ 4, "desc" ]],//默认第几个排序
        "processing": true,
        "serverSide": true,
        "bAutoWidth": true,   //自动宽度
        "iDisplayLength": 4,  //显示条数
        "bFilter": true,   //是否启用过滤
        "bPaginate": true,    //翻页功能
        "bLengthChange": true,   //改变每页显示数量
        "bSort": false,    //排序功能
        "bStateSave": false,//状态保存
        "sPaginationType": "full_numbers",
        "ajax" : "/testdata",
        /*
        "ajax": {
            "url" : "/testdata",
            "type" : "POST",
        },
        */
        "columnDefs": [{
            "orderable": false,//禁用排序
            "targets": [0,1,2,3,5,6,7,8,9,10]   //指定的列
            }]
    } );
} );

server端flask代码

@app.route('/testdata',methods = ['GET', 'POST'])
def testdata():
    pagesize = int(request.args.get('length'))
    start = int(request.args.get('start'))
    search = request.args.get('search[value]')
    message =[]
    if search:
        message_list = Maillog.query.filter(or_(Maillog.mailsubject.like("%%%s%%" % search),Maillog.reason.like("%%%s%%" % search))).order_by(Maillog.mailtime.desc()).offset(start).limit(pagesize).all() 
    else:
        message_list = Maillog.query.order_by(Maillog.mailtime.desc()).offset(start).limit(size).all()
    message_num = len(Maillog.query.order_by(Maillog.mailtime.desc()).all())
    for x,y in enumerate(message_list):
        checkl = '<input name="checkid" type="checkbox" value="%s">' % y.queueid
        options = '<a title="del" href="javascript:;" onclick=message_del("this","%s") class="ml-5" style="text-decoration:none"><i class="Hui-iconfont">&#xe6e2;</i></a>' %y.queueid
        messagey=[checkl,y.queueid,y.sender,y.receive,y.mailtime,y.mailsubject,y.mailsize,y.mailstatus,y.reason,y.codechar,options] 
        message.append(messagey)
    messagedata= {

    "recordsTotal": message_num,
    "recordsFiltered": message_num,
    "data": message
}
    return json.dumps(messagedata)

转载于:https://my.oschina.net/hxily/blog/506200

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值