tornado+datatables分页

原创
2015/09/15 14:44
阅读数 48

js代码

$(document).ready(function() {
    $('.table-sort').DataTable( {
        //"aaSorting": [[ 4, "desc" ]],//默认第几个排序
        "processing": true,
        "serverSide": true,
        "bAutoWidth": true,   //自动宽度
        "iDisplayLength": 10,  //显示条数
        "bFilter": true,   //是否启用过滤
        "bPaginate": true,    //翻页功能
        "bLengthChange": true,   //改变每页显示数量
        "bSort": false,    //排序功能
        "bStateSave": false,//状态保存
        "sPaginationType": "full_numbers",
        "ajax" : "/returnmessage",
        /*
        "ajax": {
            "url" : "/returnmessage",
            "type" : "POST",
        },
        */
        /* 一直提示DataTables warning : Requested unknown parameter '10' from the data source for row 0 错误,按照网上的添加下面aocolumndefs 就正常了,原因不明 */
        "aoColumnDefs": [{
            "sDefaultContent": '',
            "aTargets": [ '_all' ]
                        }],
        "columnDefs": [{
            "orderable": false,//禁用排序
            "targets": [0,1,2,3,5,6,7,8,9,10]   //指定的列
            }],
    } );
} );

tornado代码,用的torndb

db = torndb.Connection("127.0.0.1:3306","dbname","user","password")
class ReturnmessageHandler(tornado.web.RequestHandler):
    def get(self):
        pagesize =  str(self.get_argument("length")).strip()
        start =  str(self.get_argument("start")).strip()
        search =  str(self.get_argument("search[value]")).strip()
        message =[]
        try:
            if search:
                sql = "SELECT * FROM maillog where mailsubject like '{0}' limit {1} offset {2}".format("%%"+search+"%%",pagesize,start)
                msgnumsql = "select count(*) from maillog where mailsubject like '%s'" %("%%"+search+"%%")
            else:
                sql = 'SELECT * FROM maillog limit %s offset %s' % (pagesize,start)
                msgnumsql = 'select count(*) from maillog'
            print msgnumsql
            messagedata = db.query(sql)
            message_num= db.query(msgnumsql)[0]['count(*)']
            print message_num
            for y in messagedata:
                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
                arr = [checkl,y.queueid,y.sender,y.receive,y.mailtime,y.mailsubject,y.mailsize,y.mailstatus,y.reason,y.codechar,options]
                message.append(arr)
            messagedata= {
                "recordsTotal": message_num,
                "recordsFiltered": message_num,
                "data": message
                }
            self.write(json.dumps(messagedata))
        except Exception as e:
            print ("error %s" %format(e))
        finally:
            pass

tornado代码,用的mysql.connector

class ReturnmessageHandler(tornado.web.RequestHandler):
    def get(self):
        pagesize =  str(self.get_argument("length")).strip()
        start =  str(self.get_argument("start")).strip()
        search =  str(self.get_argument("search[value]")).strip()
        message =[]
        conn = mysql.connector.connect(**dbconfig)
        cursor = conn.cursor()
        try:
            msgnumsql = 'select count(*) from maillog'
            cursor.execute(msgnumsql)
            message_num = cursor.fetchall()[0][0]
            if search:
                sql = 'SELECT * FROM maillog where mailsubject lise "%%%s%%" limit %s offset %s' % (search,pagesize,start)
            else:
                sql = 'SELECT * FROM maillog limit %s offset %s' % (pagesize,start)
            cursor.execute(sql)
            messagedata = cursor.fetchall()
            for x,y in enumerate(messagedata):
                message.append(list(y))
            messagedata= {
                "recordsTotal": message_num,
                "recordsFiltered": message_num,
                "data": message
                }
            self.write(json.dumps(messagedata)) 
        except Exception as e:
            print ("%s mysql error! " +format(e))
        finally:
            cursor.close()
            conn.close()
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部