這篇文章主要為大家詳細介紹了thinkPHP5使用laypage分頁插件實現清單分頁功能,具有一定的參考價值,對thinkPHP5感興趣的小伙伴們可以參考一下,一起來看看關於thinkPHP5的laypage分頁吧
一、背景
在使用thinkPHP框架做專案的時候,經常會遇到對清單的內容進行分頁。 thinkPHP框架中自帶了分頁功能,但是有缺陷。這個缺陷是每次回傳每頁資料給頁面時需要重新載入頁面所需的JS、CSS等資源。如果頁麵包含的JS過多、過大,會增加流量壓力。因此,我們使用laypage插件。每次使用ajax請求取得每頁的資料就可以了,不需要重複載入頁面。
二、分頁流程
如下:
2) list.html 呼叫以取得總頁數的函數,並且得到總頁數。
3) 取得第一頁資料。
4)初始化laypage分頁外掛程式。
5)點選頁碼,觸發分頁查詢。
三、實作分頁功能相關的程式碼
//fetch渲染页面 public function index() { return $this->fetch('list'); } //根据页面传入的页码查询数据 public function getPage() { <span style="color:#FF0000;">// 获取页面传入的页码 </span> $nowpage = input("page"); //每页显示10条数据 $limits = 10; $app = new appInfo(); $page_info = $app->limit(($nowpage * $limits), $limits)->select(); // ajax 分页输出 $info = ['pageinfo'=>$page_info,'nowpage'=>$nowpage,'nowpage'=>$nowpage]; return json($info); } //获取所有页数 public function getAllPage(){ try{ $count = appInfo::count(); $limits = 10; // 计算总页面 $allpage = ceil($count / $limits); $info =['allpage'=>$allpage]; }catch (\Exception $e){ abort(500,$e->getMessage(),['result'=>TopsecGWErrer::TOPSEC_GW_ERR_NO_UNKNOWN]); } return json($info); }
<p class="box-body"> <table id="table1" class="table table-bordered table-striped"> <thead> <tr> <th>应用名称</th> <th>应用类型</th> <th>应用图像</th> <th>创建日期</th> <th>修改日期</th> <th>操作</th> </tr> </thead> <tbody id="table_body"></tbody> <tfoot> </tfoot> </table> <p id="result"></p> <p class=" "> <button id="add_app" type="button" class="btn btn-primary col-xs-offset-5" > <span class='fa fa-tasks white'></span>丨添加应用 </button> </p> <p id="content"> </p> <p class="box-body"> <button id="add_img" type="button" class="btn btn-primary col-xs-offset-5" > <span class='fa fa-tasks white'></span>丨添加图片 </button> </p> <!-- /.box-body --> </p>
##3、list.html中的jQuery代碼
#
//pageCount:总页数。用于初始化laypage分页控件。 //pageIndex:初始化当前页。显示第一页。 //currentPage:当前页数。 //getPageData:获取每页数据的函数。 //url:获取每页数据的方法的路径。由控制器和函数名组成。 function jsonPage(pageCount, pageIndex,currentPage, getPageData,url) { var laypageindex = laypage({ cont: 'result', //容器。值支持id名、原生dom对象,jquery对象。 skin: '#3c8dbc', pages: pageCount, //通过后台拿到的总页数 curr: pageIndex, //初始化当前页 prev: '上一页', //若不显示,设置false即可 next: '下一页', //若不显示,设置false即可 skip: true, //是否开启跳页 jump: function (obj, first) { //触发分页后的回调 //getPageData(1); if (!first) { //点击跳页触发函数自身,并传递当前页:obj.curr getPageData(url,obj.curr); currentPage = obj.curr; } } }); } /url:获取总页数据的方法的路径。由控制器和函数名组成。 function getAllpage(url){ var tmp ; $.ajax({ type: "GET", dataType: "json", async: false, url: PUBLIC_BASE+url, success: function(json) { var data = eval(json); tmp=data.allpage; }, error: function(json) { } }); return tmp; } //data:每页的数据。由JSON对象组成。 function instantiation(data) { //begin $.each $.each(data.pageinfo, function (index, item) { $("#table_body").append($('<tr class="table_tr"/>') .append($("<td/>").html(item.name)) .append($("<td/>").html(item.desc)) .append($("<td/>").html("<img src='"+item.appImg+"' / alt="thinkPHP5使用laypage分頁外掛實現清單分頁功能_php實例" >")) .append($("<td/>").html(item.create_time)) .append($("<td/>").html(item.update_time)) .append( $("<td />") .append($("<a class='yellow' id='view"+item.id+"'><i class='fa fa-eye fa-fw'></i>查看丨 </a>")) .append($("<a class='green' id='edit"+item.id+"'><i class='fa fa-edit fa-fw'></i>编辑丨 </a>")) .append($("<a class='red' id='delete"+item.id+"'><i class='fa fa-remove fa-fw'></i>删除丨 </a>")) .append($("<a class='blue' id='forbid" + item.id + "'><i class='fa fa-ban fa-fw'></i>禁用</a>")) ) ); $("#view"+item.id).click(function() { fillMainContent("/application/application?model=view&id="+item.id); }) $("#edit"+item.id).click(function() { fillMainContent("/application/application?model=edit&id="+item.id); }) $("#delete"+item.id).click(function() { deleteApp(item.id); alert("删除成功"); }) $("#forbid"+item.id).click(function() { deleteApp(item.id); alert("删除成功"); getPageData(currentPage); }) }); //end $.each } //获取每页的数据。curr:页码 ,curl:获取数据的路径。 function getPageData(url,curr) { curr = curr-1; $.ajax({ type: "GET", dataType: "json", data: {page:curr}, url: PUBLIC_BASE+url, success: function(json) { $('#table_body').empty(); var data = eval(json); console.log(data); instantiation(data); }, error: function(json) { var data = eval(json); console.log(data); } }); } <span style="color:#FF0000;">//调用函数实现分页</span> //获取总页数,用于初始化分页控件总页数 pageCount= getAllpage("/application/getAllPage"); //获取第一页数据 getPageData("/application/getpage",1); //初始化分页控件并分页 jsonPage(pageCount,pageIndex,currentPage,getPageData,"/application/getpage");
http://laypage.layui.com/
以上就是本文的全部內容,希望對大家的學習有幫助。
相關推薦:
laypage+laytpl範例thinkphp5分頁樣式,tp5分頁樣式以上是thinkPHP5使用laypage分頁外掛實現清單分頁功能_php實例的詳細內容。更多資訊請關注PHP中文網其他相關文章!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

宝塔部署thinkphp5报错的解决办法:1、打开宝塔服务器,安装php pathinfo扩展并启用;2、配置“.access”文件,内容为“RewriteRule ^(.*)$ index.php?s=/$1 [QSA,PT,L]”;3、在网站管理里面,启用thinkphp的伪静态即可。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

Dreamweaver Mac版
視覺化網頁開發工具

記事本++7.3.1
好用且免費的程式碼編輯器

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器