使用bootstrap-pagination进行分页

项目地址

前端示例代码:

<!DOCTYPE html>
<html>
<head>
    <title></title>
    <link href="{{ url_for("static",filename="bootstrap/css/bootstrap.min.css") }}" rel="stylesheet" media="screen">
</head>

<body>
        <!--点击查询-->
    <input type="button" value="点击" onclick="query()">
         <!--这个地方展示数据-->
    <table id="data-table">
        <tr>
            <th>UID</th>
            <th>时间</th>
            <th>channel</th>
        </tr>

    </table>
    <!--这个地方展示分页-->
    <ul class="pagination">
    </ul>


    <script src="//cdn.bootcss.com/jquery/2.2.4/jquery.min.js"></script>
    <script type="text/javascript" src="{{ url_for("static",filename="bootstrap/js/bootstrap.min.js") }}"></script>
    <script type="text/javascript" src="{{ url_for("static",filename="jquery-bootstrap-pagination.js") }}"></script>



<script>
    function sendmsg(url,data,func){
        $.getJSON(url,data,func)
    }

//进行查询
    function query(e,page){
        var query = {};
        query['page'] = page;
        //这里另每页数量为5,可自行调整
        query['page_size'] = 5;
        sendmsg("/api/count",query,loadlist);
    }
//返回结果处理
    function loadlist(json){
        $(".loaded-data").remove();
        for(var i=0;i<json.results.length;i++){
             $("#data-table").append(
            "<tr class=\"loaded-data\"><th>" +
            json.results[i]["uid"] + "</th><th>" +
            json.results[i]["dt"]+ "</th><th>" +
            json.results[i]["channel"]+ "</th></tr>")
        }
        //这里回调
        $(".pagination").unbind();
        $(".pagination").pagination({
            total_pages:json.total_pages,
            current_page:json.current_page,
            callback:query
        });
    }

</script>
</body>
</html>

后端示例代码:
这里使用python+mongoengine


class TestApi(MethodView):
    def get(self):
        page = request.args.get("page", 1, int)
        page_size = int(request.args.get("page_size", int))

        p = (page - 1) * page_size
        # 由于模拟数据少,所以直接查询所有
        query_result = RegisterRecord.objects()
        # 多个查询条件的话,可以:
        # query = {}
        # query['id__lte'] = ObjectId.from_datetime(datetime.utcnow())
        # query['id__gte'] = ObjectId.from_datetime(datetime.now())
        # result = RegisterRecord.objects(**query)

        count = query_result.count()
        total_page = self.get_page_number(count, page_size)
        current_page = page

        result = query_result.skip(p).limit(page_size)

        results = []
        for record in result:
            dt = record.pk.generation_time
            uid = record.uid
            channel = record.channel
            results.append(dict(dt=dt, uid=uid, channel=channel))

        return jsonify(results=results, total_pages=total_page, current_page=current_page)

    @staticmethod
    def get_page_number(count, page_size):
        count = float(count)
        page_size = abs(page_size)
        if page_size != 0:
            total_page = math.ceil(count / page_size)
        else:
            total_page = math.ceil(count / 5)
        return total_page

这要就行啦,嗯,就是这样~

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值