Home  >  Article  >  php教程  >  Xiaobai's brief introduction to the use of laypage paging

Xiaobai's brief introduction to the use of laypage paging

WBOY
WBOYOriginal
2016-08-04 08:56:241294browse

I am a novice, so I used to use the built-in paging. Later, a master recommended this to me. It felt good, so I recommended it. Paging generally uses GET to pass values, so once you want to perform a search, , you need to add the search value to the URL, otherwise you will click on the next page and the search conditions will be erased. This is your first post. (Welcome experts to give me some suggestions for improvement). It is mainly written for fellow novices who are struggling. Friends, watch it (encourage each other).
//这里是前端页面需要引入的文件,去 http://laypage.layui.com/ 官网下载放在项目下即可(我将文件放在了Public下,然后再调用出来),<script src="/Public/laypage/laypage.js"></script>//好像很实用的样子,后端的同学再也不用写分页逻辑了。这里只要复制进来就可以了。不用改<b><script></b><br> <b>laypage({<br>     cont: 'page11',<br>     pages: 18,</b> //假设我们获取到的是18(后端计算完总页数后将总页数值传过来,放在这里即可(类似{$totalpage})).<br>  <b>curr: function(){ </b>//通过url获取当前页,也可以同上(pages)方式获取<br> <b> var page = location.search.match(/page=(\d+)/);<br>         return page ? page[1] : 1;//如果没有页数显示时,默认是第一页<br>     }(), </b><br>    <b> jump: function(e, first){ </b>//触发分页后的回调<br>        <b> if(!first){</b> //一定要加此判断,否则初始时会无限刷新<br> <b>            location.href=setParam("page",e.curr);<br>         }<br>     }<br> });</b>//设置url中连接符(为什么要加这段呢?因为我们要带搜索条件时,一般的URL要带"?","&",这两个符号,这里就是为了,在追加页码时,当有了“?”符号时,会换成“&”,没有时则是“?”加page=页数,以下也是直接复制进去就可以了。)<b>function setParam(param,value){<br>     var query = location.search.substring(1);<br>     var p = new RegExp("(^|)" + param + "=([^&]*)(|$)");<br>     if(p.test(query)){<br>         //query = query.replace(p,"$1="+value);<br>         var firstParam=query.split(param)[0];<br>         var secondParam=query.split(param)[1];<br>         if(secondParam.indexOf("&")>-1){<br>             var lastPraam=secondParam.split("&")[1];<br>             return  '?'+firstParam+'&'+param+'='+value+'&'+lastPraam;<br>         }else{<br>             if(firstParam){<br>                 return '?'+firstParam+''+param+'='+value;<br>             }else{<br>                 return '?'+param+'='+value;<br>             }<br>         }<br>     }else{<br>         if(query == ''){<br>             return '?'+param+'='+value;<br>         }else{<br>             return '?'+query+'&'+param+'='+value;<br>         }<br>     }    <br> }<br> </script></b>//最后在你显示数据(例如后)最后添加下面这个<b><div style="margin-top:15px; text-align:center;" id="page11"></div></b>//The above id is set by yourself. If you change it, pay attention to the previous cont: 'page11', which also needs to be changed here. The preparations for the front end are now complete.
If the search terms also need to include a URL address, here’s what I wrote
//Click to search<b>$("#sou").bind("click",function(event){<br> Event.preventDefault();</b>//If you don’t understand here, you can check it yourself (the default behavior for canceling events is usually when there is <from>, if not, just remove it directly). <br> ​ <b>var type=$("#type").val();</b>//Get the hypothetical search condition value<br> <b>var url=$(this).attr("souid");</b>//Here is the address to jump to when clicking (for example: souid="<*:U('Custom/customorder')*&gt ;" Change the jump address yourself)<br /> <b> window.location.href=url+"?typeid="+type;<br /> });</b> (Novices can only write like this, sorry, basic JQ should still be understandable!)

two. Now comes the backend part<b>public function text(){</b><br /> //The following is to get the page number sent by GET. If there is no page number, the page number is 1.<br /> <b>$nowpage=I('page',1);</b><br /> //$totalpage is to calculate the maximum number of pages you want to obtain, ceil is rounded forward, here it is set to 10 pieces of data as 1 page (note the brackets). <br /> <b>$totalpage=ceil((M('order')->where(condition)<br> ->count())/10);//This is as abbreviated as possible. <br> //Below, please add the sentence limit(($nowpage-1)*10,10), which means the data controls the number of data displayed on each page. After obtaining the number of pages, multiply it by the set number of data to obtain 10 items on the page. (Set it yourself) data<br> <b>$res=M('order')->where(condition)<br> ->limit(($nowpage-1)*10,10)<br> ->select();</b><br> //Finally, the data and the maximum number of pages are passed to the front end and accepted. (Search conditions must also be sent if required.) <br> <b> $this->assign("totalpage",$totalpage);<br>           $this->assign("res",$res);</b><br> <b>}</b>

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn