一例基于thinkphp,jquery和bootstrap渲染的查询数据分页器
对于某些查询记录很多的结果,web页面不得不采用分页器,现在奉上一例代码,其主要逻辑是:由页面的dom
节点发起ajax请求,返回的查询结果根据页面布局需要进行切片;并根据总记录数和页面展现的条数算出页码列表
- html部分
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<link rel="icon" href="../../../../favicon.ico">
<title>分页技术测试</title>
<!-- Bootstrap core CSS -->
<link href="/static/booststrap4/css/bootstrap.min.css" rel="stylesheet">
<!-- Custom styles for this template -->
<link href="/static/mycss/starter-template.css" rel="stylesheet">
</head>
<body>
<nav class="navbar navbar-expand-md navbar-dark bg-dark fixed-top">
<a class="navbar-brand" href="#">Navbar</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExampleDefault"
aria-controls="navbarsExampleDefault" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarsExampleDefault">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="#">Home <span class="sr-only">(current)</span></a>
</li>
<li class="nav-item">
<a class="nav-link" href="#" onclick="test()">Link</a>
</li>
<li class="nav-item">
<a class="nav-link disabled" href="#">Disabled</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" href="http://example.com" id="dropdown01" data-toggle="dropdown"
aria-haspopup="true" aria-expanded="false">Dropdown</a>
<div class="dropdown-menu" aria-labelledby="dropdown01">
<a class="dropdown-item" href="#">Action</a>
<a class="dropdown-item" href="#">Another action</a>
<a class="dropdown-item" href="#">Something else here</a>
</div>
</li>
</ul>
<form class="form-inline my-2 my-lg-0">
<input id="searchparam" class="form-control mr-sm-2" type="text" placeholder="Search" aria-label="Search">
<button onclick = "startsearch()" class="btn btn-outline-success my-2 my-sm-0"
type="button">Search</button>
</form>
</div>
</nav>
<main role="main" class="container">
<div class="starter-template">
<h1>Bootstrap starter template</h1>
<div class="alert alert-primary" role="alert">
<table class="table">
<thead>
<tr>
</tr>
</thead>
<tbody>
<tr>
</tr>
</tbody>
</table>
</div>
<hr>
<nav aria-label="Page navigation example">
<ul class="pagination">
<!-- <li id = "clickprev" class="page-item"><a onclick="moveprev()" class="page-link" href="#">
Previous</a></li>-->
<li id="pagememo">
<span style="margin-left:100px;"></span>共计:<span id="totalpage"></span>页
<span style="margin-left:50px;"></span>当前第<span id="currentpage"></span>页
</li>
</ul>
</nav>
</div>
</main><!-- /.container -->
<!-- Bootstrap core JavaScript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="/static/booststrap4/js/jquery-3.3.1.min.js"></script>
<script src="/static/myjs/forpage2.js">
</script>
</body>
</html>
- thinkphp5.11部分
1 use think\mongo\Query; 2 class Goodmaster extends Controller 3 { 4 function forpagination($tblname,$numperpage,$start,$end,$current) 5 { 6 $ret = Db::connect('db_mongo')->name($tblname)->select(); 7 $resultlist=Array(); 8 $totalnum = count($ret);//总记录数 9 $current=intval($current); 10 $numperpage =intval($numperpage); 11 //总页数=总记录数/每页面显示的条数 12 //关于页数分两种情况:1.总记录数大于每页显示的条数使用上述公式 13 //2.总记录数小于每页显示的条数,$totalpage = 1 14 if($totalnum<$numperpage) 15 { 16 $totalpage=1; 17 $startpage = 1; 18 $endpage = 1; 19 $resultlist['ret'] =$ret; 20 } 21 else{ 22 $totalpage = ceil($totalnum/$numperpage); 23 $startpage = $start; 24 $endpage = $end; 25 $leftindex =($current-1)*$numperpage;//数组切片左索引 26 $rightindex =$leftindex+$numperpage;//数组切片左索引 27 $subret = array_slice($ret,$leftindex,$numperpage); 28 $resultlist['ret'] =$subret; 29 } 30 $resultlist['totalpage'] =$totalpage; 31 $resultlist['frompage'] =$startpage; 32 $resultlist['current'] =$current; 33 $resultlist['topage'] =$endpage; 34 $resultlist['leftindex'] =$leftindex; 35 $resultlist['totalnum'] =$totalnum; 36 //return var_dump($resultlist); 37 return $resultlist; 38 } 39 }
thinkphp5.11查询功能 代码,写的繁冗,牛逼的朋友请自行去粗取精
关于mongodb的链式查询需要配置,请看帖子 https://www.cnblogs.com/saintdingspage/p/10398441.html
- Jquery部分---取数据
function test()可以忽略,函数体可以写一些测试用的代码,已验证jquery选择器是否使用正确
下列代码主要是给出取数据,渲染表头,添加数据行的办法
var pagearray =[]; //读取数据,参数param是选中的页码 function test() { alert(pagearray[pagearray.length-1]); $(".pagination .page-item").remove(); } //这一部分主要是根据点击的页码去算应该对查询数据结果集如何切片,以每页展现10条记录为例,第一页就是数据集的第0至第9条记录(数组索引总是从0开始)
第二页就是10-19,加上php的数组切片array_slice(数组变量,起始索引,步长---就是截几个数),包含左边,不包含右边(你懂的)
function loaddata(param,startpage,endpage)
{
//var myparam =(param=='')?'all':param;
pagearray=[];
var headtitle= new Array();//数组---用于渲染表头的字段
var rowcount=0;
//var pagearray =[];
headtitle=['ID','lng','Lat','Remarks','province','city','xian'];//渲染表头的字段,当然可以动态生成
/*var startpage =$("#clickprev").next().children().html();
var endpage =$("#clicknext").prev().children().html();*/
//var currentpage = $("#currentpage").html();
var currentpage = parseInt(param);
currentpage = (currentpage=="undefined")?1:currentpage;
//alert("curpage"+currentpage);
/* startpage = (startpage=="undefined")?1:startpage;
endpage = (endpage=="undefined")?1:endpage;*/
console.log("endpage:"+endpage);
$.ajax({
url:'/forpagination/area/10/'+startpage+'/'+endpage+'/'+currentpage,
type:'get',
success:function (data) {
console.log(data);
//alert("left:"+data['leftindex']);
//alert("totalnum:"+data['totalnum']);
//生成页码列表
for(var i=1;i<=data['totalpage'];i++)
{
pagearray.push(i);
}
//对页码列表进行切片,以便于渲染bootstrap的pagination(分页)组件
//var pagenumlist = $("#clickprev").nextUntil("#clicknext");
$(".pagination .page-item").remove();
$("#pagememo").before("<li id = 'clickprev' class='page-item'>"+"<a onclick='moveprev()'
class='page-link' href='#'>"+"Previous</a></li>");
$("#clickprev").after("<li id = 'clicknext' class='page-item'>"+"
<a onclick='movenext()' class='page-link' href='#'>Next</a></li>");
var newpage = pagearray.slice(startpage-1,startpage+9);
for(var i =0;i<=newpage.length-1;i++)
{
if (newpage[i]==currentpage)//如果是选中的页码,添加的bootstrap4特效
{
$("#clicknext").before("<li id='pagenum' class='page-item active'>
<span class='page-link' href='#' onclick='pointsearch("+newpage[i]+")'>"+newpage[i]+"</span></li>");
}
else
{
$("#clicknext").before("<li id='pagenum' class='page-item'>
<a class='page-link' href='#' onclick='pointsearch("+newpage[i]+")'>"+newpage[i]+"</a></li>");
}
//onclick='pointsearch("+newpage[i]+"
console.log("fffffffff"+i);
}
//动态添加表头
$("#totalpage").html(data['totalpage']);
$("table thead tr th").remove();
for(var i in headtitle)
{
$("table thead tr").append("<th scope='col'>"+headtitle[i]+"</th>");
}
//动态添加表头(结束)
//动态添加行的值
//$("table tbody").remove();
$("table").append("<tbody></tbody>");
$.each(data['ret'],function (h,i) {
console.log(h,"----------",i);
var newrow = $("<tr></tr>");
for (var k in headtitle)
{
newrow.append("<th scope='row'>"+i[headtitle[k]]+"</th>");
}
$("table tbody").append(newrow);
rowcount+=1;
});
//动态添加行的值(结束)
$("#currentpage").html(data['current']);
}
})
}
//发起一次新的查询,针对表的第一次查询,初始化查询,从第一页生成的方式(从无到有的查询方式)点击search按键发起
function startsearch()
{
var startpage =1;
var endpage = 1;
$("table tbody").remove();
loaddata(1,startpage,endpage)
}
function pointsearch(param)//初始化查询后,(在已生成的结果集)根据指定的页码进行查询
{
var startpage =$("#clickprev").next().children().html();
var endpage =$("#clicknext").prev().children().html();
startpage = (startpage=="undefined")?1:parseInt(startpage);
endpage = (endpage=="undefined")?1:parseInt(endpage);
$("table tbody").remove();
loaddata(param,startpage,endpage);
}
function movenext()//next---往后翻页
{
var nextele = $("#pagenum span").next();//当前选中的页码
var numtoken = parseInt($("#pagenum span").html());//当前选中的页码
var firstindex = parseInt($("#clickprev").next().children().html());
var lastindex = parseInt($("#clicknext").prev().children().html());
//alert("lastindex:"+lastindex);
if(numtoken==lastindex)//翻到头了
{
$(".pagination #pagenum").remove();
var newpagelist = pagearray.slice(lastindex,lastindex+9);
var rowcount = 0;
for(var i in newpagelist)
{
$("#clicknext").before("<li id='pagenum' class='page-item'><a class='page-link' href='#'
onclick='pointsearch("+newpagelist[i]+")'>"+newpagelist[i]+"</a></li>");
rowcount+=1;
}
//var startpage =parseInt($("#clickprev").next().children().html());
//var endpage =parseInt($("#clicknext").prev().children().html());
pointsearch(parseInt(numtoken)+1);
}
else
{
pointsearch(parseInt(numtoken)+1);
}
翻页的效果---采用“翻篇”的方法
}
function moveprev()//向前翻页
{
var numtoken = parseInt($("#pagenum span").html());//当前选中的页码
var firstindex = parseInt($("#clickprev").next().children().html());
if(numtoken==firstindex)
{
if(numtoken==1)
{
}
else
{
$(".pagination #pagenum").remove();
var newpagelist = pagearray.slice(firstindex-11,firstindex);
for(var i in newpagelist)
{
$("#clicknext").before("<li id='pagenum' class='page-item'><a class='page-link'
href='#' onclick='pointsearch("+newpagelist[i]+")'>"+newpagelist[i]+"</a></li>");
}
pointsearch(parseInt(numtoken)-1);
}
}
else
{
pointsearch(parseInt(numtoken)-1);
}
}