欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

分页技术原理与实现之无刷新的Ajax分页技术(三)

程序员文章站 2023-02-21 21:19:15
紧接着上篇—分页技术原理与实现之java+oracle代码实现分页(二) ,本篇继续分析分页技术。上篇讲的是分页技术的代码实现,这篇继续分析一下分页技术的效果控制。 上篇...

紧接着上篇—分页技术原理与实现之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;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。