jquery分頁(yè)優(yōu)化操作實(shí)例分析
本文實(shí)例講述了jquery分頁(yè)優(yōu)化操作。分享給大家供大家參考,具體如下:
前言
上次寫了博客 jquery分頁(yè)顯示,文章的jquery分頁(yè)方式雖然可以通過(guò)js實(shí)現(xiàn)分頁(yè),但是也存在明顯的弊端。
該篇文章的思路是,每次通過(guò)ajax請(qǐng)求獲取到記錄,然后通過(guò)判斷當(dāng)前頁(yè)面,顯示或者隱藏記錄,以實(shí)現(xiàn)分頁(yè)效果。但是這種方式,每次切換分頁(yè)都需要從新請(qǐng)求一次記錄,頻繁的查詢數(shù)據(jù)庫(kù)。
下面提供一種,只需要加載一次(查詢一次數(shù)據(jù)庫(kù))就能實(shí)現(xiàn)分頁(yè)的思路。我們加載記錄之后,生成所有的分頁(yè)并隱藏,然后默認(rèn)顯示第一頁(yè)。切換分頁(yè)通過(guò),分頁(yè)div的顯示和隱藏來(lái)實(shí)現(xiàn)。
創(chuàng)建數(shù)據(jù)庫(kù)語(yǔ)句
CREATE TABLE `news` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=201 DEFAULT CHARSET=utf8
記錄查詢頁(yè)面
test.php
<?php header("Content-Type:text/html;Charset=utf-8"); $mysqli = new mysqli("localhost","root","","test"); $mysqli->set_charset("utf8"); $query = $mysqli->query("SELECT * FROM news"); $res = $query->fetch_all(MYSQLI_ASSOC); echo json_encode($res);
分頁(yè)顯示頁(yè)面
index.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> .page{ width: 1000px; margin-left: auto; margin-right: auto; text-align:center; display:none; } </style> </head> <body> <div id="listTag"> </div> </body> <script type="text/javascript" src="http://localhost/jquery/jquery.js"></script> <script type="text/javascript"> $(function(){ var avgNum = 18; //每頁(yè)顯示18條記錄 $.ajax({ type:"GET", url:"http://localhost/test.php", dataType:"json", success:function(data){ var totalPage = data.length; //獲取總的記錄數(shù) var pageNum = Math.ceil(totalPage/avgNum); //計(jì)算得到頁(yè)數(shù) var content = ''; for(var i=1;i<=pageNum;i++){ content += '<div id="page'+i+'" class="page"><ul>'; for(var j=((i-1)*avgNum);j<(i*avgNum)&&j<totalPage;j++){ content += '<li>'+data[j].title+'</li>'; } content += '</ul>'; content += '<hr/><div>'; //前一頁(yè) if(i == 1){ content += '<<  '; }else{ content += '<a href="javascript:showPage('+(i-1)+')" rel="external nofollow" rel="external nofollow" ><<</a>  '; } //后一頁(yè) if(i == pageNum){ content += '>>  '; }else{ content += '<a href="javascript:showPage('+(i+1)+')" rel="external nofollow" rel="external nofollow" >>></a>  '; } content += '</div></div>'; } //生成所有的div,并隱藏 $("#listTag").append(content); showPage(1); } }); }); function showPage(num){ $("#page"+num).show(); $("#page"+num).siblings().hide(); } </script> </html>
分頁(yè)效果:
分頁(yè)樣式二
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style type="text/css"> .page{ width: 1000px; margin-left: auto; margin-right: auto; text-align:center; display:none; } .active{ color:red; } </style> </head> <body> <div id="listTag"> </div> </body> <script type="text/javascript" src="http://localhost/jquery/jquery.js"></script> <script type="text/javascript"> $(function(){ var avgNum = 18; //每頁(yè)顯示18條記錄 $.ajax({ type:"GET", url:"http://localhost/test.php", dataType:"json", success:function(data){ var totalPage = data.length; //獲取總的記錄數(shù) var pageNum = Math.ceil(totalPage/avgNum); //計(jì)算得到頁(yè)數(shù) var content = ''; for(var i=1;i<=pageNum;i++){ content += '<div id="page'+i+'" class="page"><ul>'; for(var j=((i-1)*avgNum);j<(i*avgNum)&&j<totalPage;j++){ content += '<li>'+data[j].title+'</li>'; } content += '</ul>'; content += '<hr/><div>'; //前一頁(yè) if(i == 1){ content += '<<  '; }else{ content += '<a href="javascript:showPage('+(i-1)+')" rel="external nofollow" rel="external nofollow" ><<</a>  '; } //顯示所有的頁(yè)碼 for(var p=1;p<=pageNum;p++){ content += '<a href="javascript:showPage('+p+')" rel="external nofollow" class="p'+p+'">'+p+'</a>  '; } //后一頁(yè) if(i == pageNum){ content += '>>  '; }else{ content += '<a href="javascript:showPage('+(i+1)+')" rel="external nofollow" rel="external nofollow" >>></a>  '; } content += '</div></div>'; } //生成所有的div,并隱藏 $("#listTag").append(content); showPage(1); } }); }); function showPage(num){ $("#page"+num).show(); $("#page"+num).siblings().hide(); $(".p"+num).addClass("active"); $(".p"+num).siblings().removeClass("active"); } </script> </html>
顯示效果
更多關(guān)于jQuery相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《jQuery擴(kuò)展技巧總結(jié)》、《jQuery表格(table)操作技巧匯總》、《jQuery常見(jiàn)經(jīng)典特效匯總》、《jquery選擇器用法總結(jié)》及《jQuery常用插件及用法總結(jié)》
希望本文所述對(duì)大家jQuery程序設(shè)計(jì)有所幫助。
- jquery實(shí)現(xiàn)的分頁(yè)顯示功能示例
- JQuery+Ajax無(wú)刷新分頁(yè)的實(shí)例代碼
- 基于JQuery的Pager分頁(yè)器實(shí)現(xiàn)代碼
- Jquery簡(jiǎn)單分頁(yè)實(shí)現(xiàn)方法
- jquery pagination插件實(shí)現(xiàn)無(wú)刷新分頁(yè)代碼
- jquery+json實(shí)現(xiàn)數(shù)據(jù)列表分頁(yè)示例代碼
- jquery dataTable 后臺(tái)加載數(shù)據(jù)并分頁(yè)實(shí)例代碼
- jquery分頁(yè)插件jquery.pagination.js使用方法解析
- JS+Ajax+Jquery實(shí)現(xiàn)頁(yè)面無(wú)刷新分頁(yè)以及分組 超強(qiáng)的實(shí)現(xiàn)
- jquery+ashx無(wú)刷新GridView數(shù)據(jù)顯示插件(實(shí)現(xiàn)分頁(yè)、排序、過(guò)濾功能)
- 基于Jquery實(shí)現(xiàn)表格動(dòng)態(tài)分頁(yè)實(shí)現(xiàn)代碼
相關(guān)文章
jQuery實(shí)現(xiàn)鼠標(biāo)跟隨效果
本文主要分享了jQuery實(shí)現(xiàn)鼠標(biāo)跟隨效果的示例代碼。具有很好的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-02-02jquery 表格排序、實(shí)時(shí)搜索表格內(nèi)容(附圖)
這篇文章主要介紹了jquery如何實(shí)現(xiàn)表格排序、實(shí)時(shí)搜索表格內(nèi)容,需要的朋友可以參考下2014-05-05jquery ajax實(shí)現(xiàn)下拉框三級(jí)無(wú)刷新聯(lián)動(dòng),且保存保持選中值狀態(tài)
jquery ajax實(shí)現(xiàn)下拉框三級(jí)無(wú)刷新聯(lián)動(dòng),且保存保持選中值狀態(tài)。需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2013-10-10jQuery圖片切換插件jquery.cycle.js使用示例
Cycle供了非常好的功能來(lái)幫助大家更簡(jiǎn)單的使用插件的幻燈功能,下面是它的一個(gè)非常不錯(cuò)的示例,大家可以學(xué)習(xí)下2014-06-06JQERY limittext 插件0.2版(長(zhǎng)內(nèi)容限制顯示)
JQERY limittext 插件為長(zhǎng)內(nèi)容增加一個(gè)顯示更多的功能2010-08-08