DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> AJAX入門 >> AJAX基礎知識 >> 分頁技術原理與實現之無刷新的Ajax分頁技術(三)
分頁技術原理與實現之無刷新的Ajax分頁技術(三)
編輯:AJAX基礎知識     

緊接著上篇—分頁技術原理與實現之Java+Oracle代碼實現分頁(二) ,本篇繼續分析分頁技術。上篇講的是分頁技術的代碼實現,這篇繼續分析一下分頁技術的效果控制。
上篇已經用代碼簡單的實現了一個分頁。但是我們都看到,代碼中每次通過servlet請求取得結果集後,都會轉向到一個jsp頁面顯示結果,這樣每次查詢頁面都會刷新一下,比如查詢出現結果集後要查看第三頁,頁面就會刷新一下。這樣頁面給人的效果感覺就會有點不舒服,所以我們希望能夠在通過條件查詢結果集後無論訪問哪一頁,頁面都不會刷新,而只是結果集變化。要解決這個,我想大家很容易就會想到Ajax了。
是啊,這就要請Ajax出山了。當通過查詢條件查詢到結果集後,以後每次訪問任何一頁都通過Ajax來訪問,使用異步Ajax與Servlet進行交互,將結果查詢出來返回給Ajax,這樣頁面內容因為Ajax返回結果而改變,而頁面卻不會刷新,這就實現了無刷新的分頁技術。
下面我們來看一個簡單的無刷新分頁實現,代碼如下:

<!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 href="http://lib.csdn.net/base/javascript" class='replace_word' title="JavaScript知識庫" target='_blank' style='color:#df3434; font-weight:bold;'>JavaScript</a>" src="../lib/<a href="http://lib.csdn.net/base/jquery" class='replace_word' title="jQuery知識庫" 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 href="http://lib.csdn.net/base/docker" class='replace_word' title="Docker知識庫" 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>

這就是一個非常簡單的無刷新分頁實現,使用了JQuery+ jquery.pagination框架。現在隨著框架的流行,尤其是Jquery的流行,使用框架來開發是非常有效的。上面代碼原理在代碼中已有注釋,也可參考Jquery的官方網站:。
現在就可以來開發我們的Ajax無刷新分頁實現。基於上面的原理,在響應頁碼被按下的代碼中pageselectCallback(),我們使用一個Ajax異步訪問數據庫,通過點擊的頁號將結果集取出後再用異步設置到頁面,這樣就可以完成了無刷新實現。 

頁碼被按下的響應函數pageselectCallback()修改如下: 

這樣就可以用異步方式獲取結果,用showResponse函數來處理結果了,showResponse函數如下:

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);
  }

如上代碼就是用來處理通過Ajax異步請求Servlet後返回的XML格式的結果,其中Servlet代碼在上篇中。其中itemList、pageList分別是解析返回後生成的用戶List和分頁導航,這樣用戶就可以以自己的展現方式展現數據了。

function pageselectCallback(page_index, jq){
  var pars="pageNo="+(page_index+1);
   $.ajax({
    type: "POST",
   url: " UserBasicSearchServlet",
   cache: false,
   data: pars,
   success: showResponse
  });
    return false;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved