jquery+Ajax實(shí)現(xiàn)簡單分頁條效果
本文實(shí)例為大家分享了jquery+Ajax實(shí)現(xiàn)分頁條效果的具體代碼,供大家參考,具體內(nèi)容如下
一、如果是jsp頁面的話,可以用EL表達(dá)式和JSTL標(biāo)簽制作一個(gè)分頁條,沒有什么難度。用EL表達(dá)式和JSTL標(biāo)簽實(shí)現(xiàn)的缺點(diǎn)就是無法實(shí)現(xiàn)異步效果,整個(gè)頁面是重新刷新了一遍的。
二、如果是普通的html頁面,當(dāng)然是無法使用EL表達(dá)式和JSTL標(biāo)簽的,這時(shí)只能通過異步Ajax的方式去實(shí)現(xiàn)。當(dāng)然了,JSP頁面兩種方式都是可以使用的。
三、分頁條,這里我是用Ajax和Jquery去做的。實(shí)現(xiàn)起來比較繁瑣,代碼特別長,因?yàn)槎际瞧唇右淮蠖训淖址?,然后使用html()方法或是append()方法去改變文檔的內(nèi)容。
四、事前分析
瀏覽器端需要發(fā)送給服務(wù)器端的參數(shù)有兩個(gè):
①當(dāng)前的頁碼currentPage;
②頁面的大?。ㄒ豁擄@示幾條記錄)pageSize。
服務(wù)器端給瀏覽器端發(fā)送的是Json格式的數(shù)據(jù),也就是一個(gè)頁面實(shí)體類PageBean。其中PageBean有如下字段:
①總記錄數(shù)totalCount;
②總頁碼totalPage;
③每頁的數(shù)據(jù) List list;
④當(dāng)前頁碼currentPage;
⑤每頁顯示的記錄數(shù)pageSize。
這個(gè)PageBean支持泛型,代碼如下:
public class PageBean<T>
{
private int totalCount; // 總記錄數(shù)
private int totalPage ; // 總頁碼
private List<T> list ; // 每頁的數(shù)據(jù)
private int currentPage ; //當(dāng)前頁碼
private int rows;//每頁顯示的記錄數(shù),也就是pageSize
public int getTotalCount()
{
return totalCount;
}
public void setTotalCount(int totalCount)
{
this.totalCount = totalCount;
}
public int getTotalPage()
{
return totalPage;
}
public void setTotalPage(int totalPage)
{
this.totalPage = totalPage;
}
public List<T> getList()
{
return list;
}
public void setList(List<T> list)
{
this.list = list;
}
public int getCurrentPage()
{
return currentPage;
}
public void setCurrentPage(int currentPage)
{
this.currentPage = currentPage;
}
public int getRows()
{
return rows;
}
public void setRows(int rows) {
this.rows = rows;
}
@Override
public String toString()
{
return "PageBean{" +
"totalCount=" + totalCount +
", totalPage=" + totalPage +
", list=" + list +
", currentPage=" + currentPage +
", rows=" + rows +
'}';
}
}
要想做到分頁,肯定要用到SQL語句中的“l(fā)imit”。舉個(gè)例子說明一下含義。
select * from student limit 2,5;
具體含義:從student表當(dāng)中查詢數(shù)據(jù),從索引為“2”的記錄開始查詢,往后查5條。
索引是從0開始的,所以上面的語句相當(dāng)于查詢了第3、第4、第5、第6、第7條記錄,總共5條記錄。
總而言之,第一個(gè)數(shù)字就是“從哪開始查”的意思,第二個(gè)數(shù)字就是“往后查幾條”的意思。
這里的“從哪開始查”,需要計(jì)算出來。公式如下:
(currentPage-1)×pageSize
也就是當(dāng)前頁碼減去一,括號,在乘以頁面大小。
所以查詢語句的偽代碼如下:
select * from student limit (currentPage-1)×pageSize,pageSize;
對于總頁碼totalPage,可以通過總記錄數(shù)totalCount和頁面大小pageSize計(jì)算出來。代碼如下:
totalPage=totalCount%pageSize==0?totalCount/pageSize:(totalCount/pageSize+1);
五、服務(wù)器端主要代碼
import com.fasterxml.jackson.databind.ObjectMapper;
import domain.PageBean;
import domain.RainFall;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
@WebServlet("/ViewRainByPageServlet")
public class ViewRainByPageServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
JdbcTemplate template=new JdbcTemplate(JDBCUtils.getDataSource());
String sql="select * from rain_fall limit ?,?";//查詢部分元組
String sql2="select * from rain_fall";//查詢所有元組
List<RainFall> countList = template.query(sql2, new BeanPropertyRowMapper<RainFall>(RainFall.class));
int totalCount=countList.size();//從數(shù)據(jù)庫獲取總記錄數(shù)
int totalPage;//先聲明一下總的頁碼,總的頁碼需要根據(jù)客戶端發(fā)送過來的數(shù)據(jù)進(jìn)行計(jì)算
String currentPage = request.getParameter("currentPage");
String pageSize = request.getParameter("pageSize");
int intCurrentPage = Integer.parseInt(currentPage);//用戶發(fā)來的當(dāng)前頁碼
if(intCurrentPage==0)//用戶點(diǎn)擊上一頁按鈕,currentPage就減1,會出現(xiàn)減到0的情況
{
intCurrentPage=1;//如果用戶的currentPage=0,直接把頁碼設(shè)為1,把第一頁的數(shù)據(jù)返回給客戶端
}
int intPageSize = Integer.parseInt(pageSize);//用戶發(fā)來的頁面大小
totalPage=totalCount%intPageSize==0?totalCount/intPageSize:(totalCount/intPageSize+1);//計(jì)算出總的頁數(shù)
if(intCurrentPage>totalPage)//用戶點(diǎn)擊下一頁按鈕,currentPage就加1,會出現(xiàn)大于總頁數(shù)的情況
{
intCurrentPage=totalPage;//把當(dāng)前頁碼設(shè)為總頁數(shù)
}
int startIndex=(intCurrentPage-1)*intPageSize;//從索引為幾的記錄開始查詢?
List<RainFall> list = template.query(sql, new BeanPropertyRowMapper<RainFall>(RainFall.class),startIndex,intPageSize);
ObjectMapper mapper=new ObjectMapper();
response.setContentType("application/json;charset=utf-8");//設(shè)置響應(yīng)數(shù)據(jù)類型和字符編碼
PageBean<RainFall> pageBean=new PageBean<>();//創(chuàng)建PageBean對象
//封裝PageBean對象
pageBean.setTotalCount(totalCount);
pageBean.setTotalPage(totalPage);
pageBean.setList(list);
pageBean.setCurrentPage(intCurrentPage);
pageBean.setRows(intPageSize);
//將數(shù)據(jù)寫回客戶端
System.out.println(pageBean);
mapper.writeValue(response.getOutputStream(),pageBean);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
this.doPost(request, response);
}
}
六、前端代碼(很長)
<%--
Created by Yingyong Lao.
User: laoyingyong
Date: 2019-12-10
Time: 19:28
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>查看降雨信息</title>
<!-- 1. 導(dǎo)入CSS的全局樣式 -->
<link href="css/bootstrap-3.3.7-dist/css/bootstrap.min.css" rel="stylesheet">
<!-- 2. jQuery導(dǎo)入,建議使用1.9以上的版本 -->
<script src="js/jquery-2.1.0.min.js"></script>
<!-- 3. 導(dǎo)入bootstrap的js文件 -->
<script src="js/bootstrap.min.js"></script>
<script>
$(function () //入口函數(shù)
{
$.post("ViewRainByPageServlet",{currentPage:1,pageSize:5},function (data)//頁面加載完成之后發(fā)送ajax請求,請求前5條記錄,完成界面的初始化
{
var totalCount=data.totalCount;//總記錄數(shù)
var totalPage=data.totalPage;//總頁數(shù)
var currentPage=data.currentPage;//當(dāng)前頁碼
if(currentPage==1)//如果當(dāng)前頁碼為1,用戶還點(diǎn)擊上一頁的話,設(shè)置class="disabled" ,即出現(xiàn)一個(gè)“禁用”圖標(biāo)
{
var str=' <li class="disabled" οnclick="findByPage('+(currentPage-1)+',5)">\n' +
' <a href="#" aria-label="Previous">\n' +
' <span aria-hidden="true">«</span>\n' +
' </a>\n' +
' </li>';
}
else//否則上一頁的按鈕就是正常的樣式
{
var str=' <li οnclick="findByPage('+(currentPage-1)+',5)">\n' +
' <a href="#" aria-label="Previous">\n' +
' <span aria-hidden="true">«</span>\n' +
' </a>\n' +
' </li>';
}
for(var i=1;i<=totalPage;i++)//遍歷頁碼,這是兩個(gè)圖標(biāo)(上一頁和下一頁)之間的數(shù)字
{
if(i==currentPage)//如果當(dāng)前的這個(gè)數(shù)字等于當(dāng)前頁碼currentPage,就設(shè)置class="active",即頁碼呈高亮樣式
{
var item='<li οnclick="findByPage('+i+',5);" class="active"><a href="#">'+i+'</a></li>';
}
else
{
var item='<li οnclick="findByPage('+i+',5);"><a href="#">'+i+'</a></li>';
}
str=str+item;
}
if(currentPage==totalPage)//如果當(dāng)前頁碼為最后一頁,用戶還點(diǎn)擊下一頁的話,設(shè)置class="disabled" ,即出現(xiàn)一個(gè)“禁用”圖標(biāo)
{
var strend='<li class="disabled" οnclick="findByPage('+(currentPage+1)+',5)">\n' +
' <a href="#" aria-label="Next">\n' +
' <span aria-hidden="true">»</span>\n' +
' </a>\n' +
' </li>\n' +
' <span style="font-size: 24px" id="total_sp">共'+totalCount+'條記錄,共'+totalPage+'頁</span>';
}
else //不是最后一頁,就是正常的樣式
{
var strend='<li οnclick="findByPage('+(currentPage+1)+',5)">\n' +
' <a href="#" aria-label="Next">\n' +
' <span aria-hidden="true">»</span>\n' +
' </a>\n' +
' </li>\n' +
' <span style="font-size: 24px" id="total_sp">共'+totalCount+'條記錄,共'+totalPage+'頁</span>';
}
str=str+strend;
$("#fenyelan").html(str);//分頁條初始化
var array=data.list;
for(var i=0;i<array.length;i++)
{
var obj=array[i];
var id=obj.id;
var area=obj.area;
var precipitation=obj.precipitation;
var month=obj.month;
var releaseDate=obj.releaseDate;
//表格的初始化
$("#rain_table").append('<tr class="info">\n' +
' <td style="text-align: center">'+id+'</td>\n' +
' <td style="text-align: center">'+area+'</td>\n' +
' <td style="text-align: center">'+precipitation+'</td>\n' +
' <td style="text-align: center">'+month+'</td>\n' +
' <td style="text-align: center">'+releaseDate+'</td>\n' +
' </tr>');
}
});//頁面加載完成之后發(fā)送ajax請求end
});//入口函數(shù)end
//頁面按鈕的點(diǎn)擊回調(diào)函數(shù),不需要寫在入口函數(shù)里面,因?yàn)橹挥许撁姘粹o被點(diǎn)擊時(shí),這個(gè)函數(shù)才會被調(diào)用
function findByPage(curPage,paSize) //被調(diào)用的時(shí)候,需要傳遞兩個(gè)參數(shù):當(dāng)前頁碼和頁碼的大?。ㄒ豁撚袔讞l記錄)
{
$.post("ViewRainByPageServlet",{currentPage:curPage,pageSize:paSize},function (data) //發(fā)送ajax請求
{
var totalCount=data.totalCount;//總記錄數(shù)
var totalPage=data.totalPage;//總頁數(shù)
var currentPage=data.currentPage;//當(dāng)前頁碼
if(currentPage==1)//如果當(dāng)前頁碼為1,用戶還點(diǎn)擊上一頁的話,設(shè)置class="disabled" ,即出現(xiàn)一個(gè)“禁用”圖標(biāo)
{
var str=' <li class="disabled" οnclick="findByPage('+(currentPage-1)+',5)">\n' +
' <a href="#" aria-label="Previous">\n' +
' <span aria-hidden="true">«</span>\n' +
' </a>\n' +
' </li>';
}
else//不為第一頁,上一頁按鈕就是正常的樣式
{
var str=' <li οnclick="findByPage('+(currentPage-1)+',5)">\n' +
' <a href="#" aria-label="Previous">\n' +
' <span aria-hidden="true">«</span>\n' +
' </a>\n' +
' </li>';
}
//分頁條中間數(shù)字部分
for(var i=1;i<=totalPage;i++)
{
if(i==currentPage)//如果當(dāng)前的這個(gè)數(shù)字等于當(dāng)前頁碼currentPage,就設(shè)置class="active",即頁碼呈高亮樣式
{
var item='<li οnclick="findByPage('+i+',5);" class="active"><a href="#">'+i+'</a></li>';
}
else
{
var item='<li οnclick="findByPage('+i+',5);"><a href="#">'+i+'</a></li>';
}
str=str+item;
}
if(currentPage==totalPage)//如果當(dāng)前頁碼為最后一頁,用戶還點(diǎn)擊下一頁的話,設(shè)置class="disabled" ,即出現(xiàn)一個(gè)“禁用”圖標(biāo)
{
var strend='<li class="disabled" οnclick="findByPage('+(currentPage+1)+',5)">\n' +
' <a href="#" aria-label="Next">\n' +
' <span aria-hidden="true">»</span>\n' +
' </a>\n' +
' </li>\n' +
' <span style="font-size: 24px" id="total_sp">共'+totalCount+'條記錄,共'+totalPage+'頁</span>';
}
else //不是最后一頁,就是正常的樣式
{
var strend='<li οnclick="findByPage('+(currentPage+1)+',5)">\n' +
' <a href="#" aria-label="Next">\n' +
' <span aria-hidden="true">»</span>\n' +
' </a>\n' +
' </li>\n' +
' <span style="font-size: 24px" id="total_sp">共'+totalCount+'條記錄,共'+totalPage+'頁</span>';
}
str=str+strend;
$("#fenyelan").html(str);//改變分頁條的內(nèi)容
//表格的字符串
var tableStr='<caption style="text-align: center;font-size: 24px">降雨信息一覽</caption>\n' +
' <tr class="success">\n' +
' <th style="text-align: center">id</th>\n' +
' <th style="text-align: center">地區(qū)</th>\n' +
' <th style="text-align: center">降雨量(mm)</th>\n' +
' <th style="text-align: center">月份</th>\n' +
' <th style="text-align: center">發(fā)布日期</th>\n' +
' </tr>';
var array=data.list;//具體內(nèi)容的對象數(shù)組
for(var i=0;i<array.length;i++)//遍歷數(shù)對象組
{
var obj=array[i];//數(shù)組的一個(gè)元素就是一個(gè)對象
var id=obj.id;
var area=obj.area;
var precipitation=obj.precipitation;
var month=obj.month;
var releaseDate=obj.releaseDate;
//一行記錄的字符串
var oneRecord='<tr class="info">\n' +
' <td style="text-align: center">'+id+'</td>\n' +
' <td style="text-align: center">'+area+'</td>\n' +
' <td style="text-align: center">'+precipitation+'</td>\n' +
' <td style="text-align: center">'+month+'</td>\n' +
' <td style="text-align: center">'+releaseDate+'</td>\n' +
' </tr>';
tableStr=tableStr+oneRecord;//表格字符串的追加,每遍歷一條記錄,就會追加一行
}
$("#rain_table").html(tableStr);//改變表格里面的內(nèi)容
});//ajax請求end
}//頁面按鈕的點(diǎn)擊函數(shù)end
</script>
</head>
<body>
<div class="container">
<div class="row">
<table class="table table-bordered table-hover" id="rain_table">
<caption style="text-align: center;font-size: 24px">降雨信息一覽</caption>
<tr class="success">
<th style="text-align: center">id</th>
<th style="text-align: center">地區(qū)</th>
<th style="text-align: center">降雨量(mm)</th>
<th style="text-align: center">月份</th>
<th style="text-align: center">發(fā)布日期</th>
</tr>
</table>
<nav aria-label="Page navigation">
<ul class="pagination" id="fenyelan">
<li>
<a href="#" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li><a href="#">1</a></li>
<li>
<a href="#" aria-label="Next">
<span aria-hidden="true">»</span>
</a>
</li>
<span style="font-size: 24px" id="total_sp">共2條記錄,共1頁</span>
</ul>
</nav>
</div>
</div>
</body>
</html>
七、效果展示

這只是一個(gè)簡單的分頁條,沒有百度分頁條“前五后四”的效果。當(dāng)數(shù)據(jù)量變得非常龐大時(shí),這個(gè)分頁條就會占據(jù)很大的空間。有時(shí)間的話再優(yōu)化一下吧。至于Jquery代碼,代碼的注釋里已經(jīng)寫得夠清楚了,這里就不再做過多的解釋。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery彈出層后禁用底部滾動條(移動端關(guān)閉回到原位置)
這篇文章主要介紹了jQuery彈出層后禁用底部滾動條(移動端關(guān)閉回到原位置)的關(guān)鍵代碼,非常不錯(cuò),代碼簡單易懂,需要的朋友可以參考下2016-08-08
jquery無法為動態(tài)生成的元素添加點(diǎn)擊事件的解決方法(推薦)
下面小編就為大家?guī)硪黄猨query無法為動態(tài)生成的元素添加點(diǎn)擊事件的解決方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦2016-12-12
用jquery實(shí)現(xiàn)的模擬QQ郵箱里的收件人選取及其他效果(一)
今天要說的是用jquery的語法和組件dialog及Autocomplete來制作QQ郵箱的發(fā)件人操作功能,認(rèn)為這個(gè)太過簡單的可以離開了。2011-01-01
jQuery設(shè)置指定網(wǎng)頁元素寬度和高度的方法
這篇文章主要介紹了jQuery設(shè)置指定網(wǎng)頁元素寬度和高度的方法,涉及jQuery操作width及height方法的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03
使用js+jquery實(shí)現(xiàn)無限極聯(lián)動
本篇文章是對可擴(kuò)展的無限極聯(lián)動下拉選項(xiàng)的實(shí)例進(jìn)行了分析介紹,需要的朋友參考下2013-05-05
jQuery實(shí)現(xiàn)手機(jī)自定義彈出輸入框
這篇文章主要介紹了jQuery實(shí)現(xiàn)手機(jī)自定義彈出輸入框 的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06

