nodejs实现分页功能

2023-01-01 08:39:28
  1. find 方法修改
db.collection(collectionName).find(json1,{fields:attr}).skip(slipNum).limit(pageSize);

2.封装 count 方法

var result=db.collection(collection).count(json);//nodejs封装mongodb知识
result.then(data=>{
	resolve(data);
	}

3.数据结合 jqPaginator 实现分页
http://jqpaginator.keenwon.com/
a.引入js文件jqPaginator.js,并声明

<script src="{{_HOST_}}/admin/jqPaginator.js"></script>

b.引入js分页逻辑

<script>
		$('#page').jqPaginator({
			totalPages: {{totalPages}},
			visiblePages: 8,
			currentPage: {{page}},
			onPageChange: function (num, type) {
				console.log(num, type);
//
				if(type=='change'){
					location.href="{{_HOST_}}/admin/focus?page="+num;
				}
			}
		});
	</script>

c.在表格下加上分页html

<div id="page" class="pagination"></div>

d.在后台引入分页所需数据

var result=await DB.find('article',{},{},{
        page,pageSize,
        sortJson:{
            'add_time':-1
        }
    });
    await ctx.render('admin/article/list',{
        list:result,
        page:page,
        totalPages:Math.ceil(count/pageSize)
    });
  • 作者:令人着迷的夜晚
  • 原文链接:https://blog.csdn.net/weixin_42929948/article/details/106349193
    更新时间:2023-01-01 08:39:28