分頁(yè)技術(shù)原理與實(shí)現(xiàn)之無(wú)刷新的Ajax分頁(yè)技術(shù)(三)
緊接著上篇—分頁(yè)技術(shù)原理與實(shí)現(xiàn)之Java+Oracle代碼實(shí)現(xiàn)分頁(yè)(二) ,本篇繼續(xù)分析分頁(yè)技術(shù)。上篇講的是分頁(yè)技術(shù)的代碼實(shí)現(xiàn),這篇繼續(xù)分析一下分頁(yè)技術(shù)的效果控制。
上篇已經(jīng)用代碼簡(jiǎn)單的實(shí)現(xiàn)了一個(gè)分頁(yè)。但是我們都看到,代碼中每次通過(guò)servlet請(qǐng)求取得結(jié)果集后,都會(huì)轉(zhuǎn)向到一個(gè)jsp頁(yè)面顯示結(jié)果,這樣每次查詢頁(yè)面都會(huì)刷新一下,比如查詢出現(xiàn)結(jié)果集后要查看第三頁(yè),頁(yè)面就會(huì)刷新一下。這樣頁(yè)面給人的效果感覺(jué)就會(huì)有點(diǎn)不舒服,所以我們希望能夠在通過(guò)條件查詢結(jié)果集后無(wú)論訪問(wèn)哪一頁(yè),頁(yè)面都不會(huì)刷新,而只是結(jié)果集變化。要解決這個(gè),我想大家很容易就會(huì)想到Ajax了。
是啊,這就要請(qǐng)Ajax出山了。當(dāng)通過(guò)查詢條件查詢到結(jié)果集后,以后每次訪問(wèn)任何一頁(yè)都通過(guò)Ajax來(lái)訪問(wèn),使用異步Ajax與Servlet進(jìn)行交互,將結(jié)果查詢出來(lái)返回給Ajax,這樣頁(yè)面內(nèi)容因?yàn)锳jax返回結(jié)果而改變,而頁(yè)面卻不會(huì)刷新,這就實(shí)現(xiàn)了無(wú)刷新的分頁(yè)技術(shù)。
下面我們來(lái)看一個(gè)簡(jiǎn)單的無(wú)刷新分頁(yè)實(shí)現(xiàn),代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="../lib/jquery_pagination/pagination.css" mce_href="lib/jquery_pagination/pagination.css" />
<mce:script type="text/<a class='replace_word' title="JavaScript知識(shí)庫(kù)" target='_blank' style='color:#df3434; font-weight:bold;'>JavaScript</a>" src="../lib/<a class='replace_word' title="jQuery知識(shí)庫(kù)" target='_blank' style='color:#df3434; font-weight:bold;'>jQuery</a>/jquery.min.js" mce_src="lib/jquery/jquery.min.js"></mce:script>
<mce:script type="text/javascript"
src="../lib/jquery_pagination/jquery.pagination.js"></mce:script>
<mce:script type="text/javascript"><!--
/**
* Callback function that displays the content.
*
* Gets called every time the user clicks on a pagination link.
*
* @param {int}page_index New Page index
* @param {jQuery} jq the <a class='replace_word' title="Docker知識(shí)庫(kù)" target='_blank' style='color:#df3434; font-weight:bold;'>Container</a> with the pagination links as a jQuery object
*/
function pageselectCallback(page_index, jq) {
var new_content = $('#hiddenresult div.result:eq(' + page_index + ')')
.clone();
$('#Searchresult').empty().append(new_content);
return false;
}
function initPagination() {
var num_entries = $('#hiddenresult div.result').length;
// Create pagination element
$("#Pagination").pagination(num_entries, {
num_edge_entries : 2,
num_display_entries : 8,
callback : pageselectCallback,
items_per_page : 1
});
}
// When the HTML has loaded, call initPagination to paginate the elements
$(document).ready(function() {
initPagination();
});
// --></mce:script>
<mce:style type="text/css"><!--
* {
padding: 0;
margin: 0;
}
body {
background-color: #fff;
margin: 20px;
padding: 0;
height: 100%;
font-family: Arial, Helvetica, sans-serif;
}
#Searchresult {
margin-top: 15px;
margin-bottom: 15px;
border: solid 1px #eef;
padding: 5px;
background: #eef;
width: 40%;
}
#Searchresult p {
margin-bottom: 1.4em;
}
--></mce:style><style type="text/css" mce_bogus="1">* {
padding: 0;
margin: 0;
}
body {
background-color: #fff;
margin: 20px;
padding: 0;
height: 100%;
font-family: Arial, Helvetica, sans-serif;
}
#Searchresult {
margin-top: 15px;
margin-bottom: 15px;
border: solid 1px #eef;
padding: 5px;
background: #eef;
width: 40%;
}
#Searchresult p {
margin-bottom: 1.4em;
}</style>
<title>Pagination</title>
</head>
<body>
<h4>
jQuery Pagination Plugin Demo
</h4>
<div id="Pagination" class="pagination">
</div>
<br style="clear: both;" mce_style="clear: both;" />
<div id="Searchresult">
This content will be replaced when pagination inits.
</div>
<div id="hiddenresult" style="display: none;" mce_style="display: none;">
<div class="result">
<p>
Globally maximize granular "outside the box" thinking vis-a-vis
quality niches. Proactively formulate 24/7 results whereas 2.0
catalysts for change. Professionally implement 24/365 niches rather
than client-focused users.
</p>
<p>
Competently engineer high-payoff "outside the box" thinking through
cross functional benefits. Proactively transition intermandated
processes through open-source niches. Progressively engage
maintainable innovation and extensible interfaces.
</p>
</div>
<div class="result">
<p>
Credibly fabricate e-business models for end-to-end niches.
Compellingly disseminate integrated e-markets without ubiquitous
services. Credibly create equity invested channels with
multidisciplinary human capital.
</p>
<p>
Interactively integrate competitive users rather than fully tested
infomediaries. Seamlessly initiate premium functionalities rather
than impactful architectures. Rapidiously leverage existing
resource-leveling processes via user-centric portals.
</p>
</div>
<div class="result">
<p>
Monotonectally initiate unique e-services vis-a-vis client-centric
deliverables. Quickly impact parallel opportunities with B2B
bandwidth. Synergistically streamline client-focused
infrastructures rather than B2C e-commerce.
</p>
<p>
Phosfluorescently fabricate 24/365 e-business through 24/365 total
linkage. Completely facilitate high-quality systems without
stand-alone strategic theme areas.
</p>
</div>
</div>
</body>
</html>
這就是一個(gè)非常簡(jiǎn)單的無(wú)刷新分頁(yè)實(shí)現(xiàn),使用了JQuery+ jquery.pagination框架?,F(xiàn)在隨著框架的流行,尤其是Jquery的流行,使用框架來(lái)開(kāi)發(fā)是非常有效的。上面代碼原理在代碼中已有注釋,也可參考Jquery的官方網(wǎng)站:。
現(xiàn)在就可以來(lái)開(kāi)發(fā)我們的Ajax無(wú)刷新分頁(yè)實(shí)現(xiàn)?;谏厦娴脑?,在響應(yīng)頁(yè)碼被按下的代碼中pageselectCallback(),我們使用一個(gè)Ajax異步訪問(wèn)數(shù)據(jù)庫(kù),通過(guò)點(diǎn)擊的頁(yè)號(hào)將結(jié)果集取出后再用異步設(shè)置到頁(yè)面,這樣就可以完成了無(wú)刷新實(shí)現(xiàn)。
頁(yè)碼被按下的響應(yīng)函數(shù)pageselectCallback()修改如下:
這樣就可以用異步方式獲取結(jié)果,用showResponse函數(shù)來(lái)處理結(jié)果了,showResponse函數(shù)如下:
function showResponse(request){
var content = request;
var root = content.documentElement;
var responseNodes = root.getElementsByTagName("root");
var itemList = new Array();
var pageList=new Array();
alert(responseNodes.length);
if (responseNodes.length > 0) {
var responseNode = responseNodes[0];
var itemNodes = responseNode.getElementsByTagName("data");
for (var i=0; i<itemNodes.length; i++) {
var idNodes = itemNodes[i].getElementsByTagName("id");
var nameNodes = itemNodes[i].getElementsByTagName("name");
var sexNodes=itemNodes[i].getElementsByTagName("sex");
var ageNodes=itemNodes[i].getElementsByTagName("age");
if (idNodes.length > 0 && nameNodes.length > 0&&sexNodes.length > 0&& ageNodes.length > 0) {
var id=idNodes[0].firstChild.nodeValue;
var name = nameNodes[0].firstChild.nodeValue;
var sex = sexNodes[0].firstChild.nodeValue;
var age=ageNodes[0].firstChild.nodeValue;
itemList.push(new Array(id,name, sex,age));
}
}
var pageNodes = responseNode.getElementsByTagName("pagination");
if (pageNodes.length>0) {
var totalNodes = pageNodes[0].getElementsByTagName("total");
var startNodes = pageNodes[0].getElementsByTagName("start");
var endNodes=pageNodes[0].getElementsByTagName("end");
var currentNodes=pageNodes[0].getElementsByTagName("pageno");
if (totalNodes.length > 0 && startNodes.length > 0&&endNodes.length > 0) {
var total=totalNodes[0].firstChild.nodeValue;
var start = startNodes[0].firstChild.nodeValue;
var end = endNodes[0].firstChild.nodeValue;
var current=currentNodes[0].firstChild.nodeValue;
pageList.push(new Array(total,start,end,current));
}
}
}
showTable(itemList,pageList);
}
如上代碼就是用來(lái)處理通過(guò)Ajax異步請(qǐng)求Servlet后返回的XML格式的結(jié)果,其中Servlet代碼在上篇中。其中itemList、pageList分別是解析返回后生成的用戶List和分頁(yè)導(dǎo)航,這樣用戶就可以以自己的展現(xiàn)方式展現(xiàn)數(shù)據(jù)了。
function pageselectCallback(page_index, jq){
var pars="pageNo="+(page_index+1);
$.ajax({
type: "POST",
url: " UserBasicSearchServlet",
cache: false,
data: pars,
success: showResponse
});
return false;
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 使用ajax技術(shù)無(wú)刷新動(dòng)態(tài)調(diào)用新浪股票實(shí)時(shí)數(shù)據(jù)
- ThinkPHP結(jié)合AjaxFileUploader實(shí)現(xiàn)無(wú)刷新文件上傳的方法
- Jquery基于Ajax方法自定義無(wú)刷新提交表單Form實(shí)例
- 使用ajax實(shí)現(xiàn)無(wú)刷新改變頁(yè)面內(nèi)容和地址欄URL
- php+ajax實(shí)現(xiàn)無(wú)刷新動(dòng)態(tài)加載數(shù)據(jù)技術(shù)
- jQuery+AJAX實(shí)現(xiàn)無(wú)刷新下拉加載更多
- jQuery實(shí)現(xiàn)AJAX定時(shí)刷新局部頁(yè)面實(shí)例
- jQuery使用$.ajax進(jìn)行異步刷新的方法(附demo下載)
- jQuery實(shí)現(xiàn)form表單基于ajax無(wú)刷新提交方法詳解
- jQuery+Ajax實(shí)現(xiàn)無(wú)刷新操作
- Ajax回退刷新頁(yè)面問(wèn)題的解決辦法
相關(guān)文章
history保存列表頁(yè)ajax請(qǐng)求的狀態(tài)使用示例詳解
這篇文章主要為大家介紹了history保存列表頁(yè)ajax請(qǐng)求的狀態(tài)使用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
ajax簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要為大家詳細(xì)介紹了ajax基礎(chǔ)知識(shí)點(diǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
解決ajax的delete、put方法接收不到參數(shù)的問(wèn)題方法
今天小編就為大家分享一篇解決ajax的delete、put方法接收不到參數(shù)的問(wèn)題方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Ajax異步提交數(shù)據(jù)返回值的換行問(wèn)題實(shí)例分析
這篇文章主要介紹了Ajax異步提交數(shù)據(jù)返回值的換行問(wèn)題,結(jié)合實(shí)例形式較為詳細(xì)的分析了ajax異步提交過(guò)程中返回值帶有換行的處理技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-12-12
Ajax添加數(shù)據(jù)與刪除篇實(shí)現(xiàn)代碼
Hello 大家好!這個(gè)ajax系列教程講到這里已經(jīng)完成50%了.第4篇講了ajax向數(shù)據(jù)庫(kù)添加數(shù)據(jù),第5篇講了ajax修改數(shù)據(jù)庫(kù)中的數(shù)據(jù).今天我們來(lái)講ajax添加數(shù)據(jù)與刪除并存的實(shí)例效果.2010-10-10
Ajax實(shí)現(xiàn)跨域訪問(wèn)的三種方法
本文給大家介紹了ajax實(shí)現(xiàn)跨域訪問(wèn)的3種解決方案,非常的實(shí)用,個(gè)人比較推薦第三種,小伙伴們可以著重看下。2015-06-06
AjaxFileUpload+Struts2實(shí)現(xiàn)多文件上傳功能
這篇文章主要介紹了AjaxFileUpload+Struts2實(shí)現(xiàn)多文件上傳功能,需要的朋友可以參考下2017-09-09
js對(duì)ajax返回?cái)?shù)組的處理介紹
本篇文章主要是對(duì)js對(duì)ajax返回?cái)?shù)組的處理進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-02-02

