ajax无刷新分页类

前端之家收集整理的这篇文章主要介绍了ajax无刷新分页类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. <?PHP
  2. classPage{
  3.  
  4.  
  5.  
  6. //分页栏每页显示的页数
  7.  
  8. public$rollPage=10;
  9.  
  10. //页数跳转时要带的参数
  11.  
  12. public$parameter;
  13.  
  14. //默认列表每页显示行数
  15.  
  16. public$listRows=20;
  17.  
  18. //起始行数
  19.  
  20. public$firstRow;
  21.  
  22. //分页页面
  23.  
  24. protected$totalPages;
  25.  
  26. //总行数
  27.  
  28. protected$totalRows;
  29.  
  30. //当前页数
  31.  
  32. protected$nowPage;
  33.  
  34. //分页的栏的总页数
  35.  
  36. protected$coolPages;
  37.  
  38. //分页显示定制
  39.  
  40. protected$config=array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'首页','last'=>'尾页','theme'=>'%totalRow%%header%%nowPage%/%totalPage%页%upPage%%first%%prePage%%linkPage%%downPage%%nextPage%%end%%ajax%');
  41.  
  42. //默认分页变量名
  43.  
  44. protected$varPage;
  45.  
  46. //分页外层div的id
  47.  
  48. protected$pagesId;
  49.  
  50. //分页内容替换目标ID
  51.  
  52. protected$target;
  53.  
  54.  
  55.  
  56. /**
  57.  
  58. +----------------------------------------------------------
  59.  
  60. *架构函数
  61.  
  62. +----------------------------------------------------------
  63.  
  64. *@accesspublic
  65.  
  66. +----------------------------------------------------------
  67.  
  68. *@paramarray$totalRows总的记录数
  69.  
  70. *@paramarray$listRows每页显示记录数
  71.  
  72. *@paramarray$parameter分页跳转的参数
  73.  
  74. +----------------------------------------------------------
  75.  
  76. */
  77.  
  78. publicfunction__construct($totalRows,$listRows='',$parameter='',$target='',$pagesId=''){
  79.  
  80. $this->totalRows=$totalRows;
  81.  
  82. $this->parameter=$parameter;
  83.  
  84. $this->target=$target;
  85.  
  86. $this->pagesId=$pagesId;
  87.  
  88. $this->varPage=C('VAR_PAGE')?C('VAR_PAGE'):'p';
  89.  
  90. if(!empty($listRows)){
  91.  
  92. $this->listRows=intval($listRows);
  93.  
  94. }
  95.  
  96. $this->totalPages=ceil($this->totalRows/$this->listRows);//总页数
  97.  
  98. $this->coolPages=ceil($this->totalPages/$this->rollPage);
  99.  
  100. $this->nowPage=!empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
  101.  
  102. if(!empty($this->totalPages)&&$this->nowPage>$this->totalPages){
  103.  
  104. $this->nowPage=$this->totalPages;
  105.  
  106. }
  107.  
  108. $this->firstRow=$this->listRows*($this->nowPage-1);
  109.  
  110. }
  111.  
  112.  
  113.  
  114. publicfunctionsetConfig($name,$value){
  115.  
  116. if(isset($this->config[$name])){
  117.  
  118. $this->config[$name]=$value;
  119.  
  120. }
  121.  
  122. }
  123.  
  124.  
  125.  
  126. /**
  127.  
  128. +----------------------------------------------------------
  129.  
  130. *分页显示输出
  131.  
  132. +----------------------------------------------------------
  133.  
  134. *@accesspublic
  135.  
  136. +----------------------------------------------------------
  137.  
  138. */
  139.  
  140. publicfunctionshow(){
  141.  
  142. if(0==$this->totalRows)
  143.  
  144. return'';
  145.  
  146. $p=$this->varPage;
  147.  
  148. $nowCoolPage=ceil($this->nowPage/$this->rollPage);
  149.  
  150. $url=$_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter;
  151.  
  152. $parse=parse_url($url);
  153.  
  154. if(isset($parse['query'])){
  155.  
  156. parse_str($parse['query'],$params);
  157.  
  158. unset($params[$p]);
  159.  
  160. $url=$parse['path'].'?'.http_build_query($params);
  161.  
  162. }
  163.  
  164. //上下翻页字符串
  165.  
  166. $upRow=$this->nowPage-1;
  167.  
  168. $downRow=$this->nowPage+1;
  169.  
  170. if($upRow>0){
  171.  
  172. $upPage="<ahref='".$url."&".$p."=$upRow'>".$this->config['prev']."</a>";
  173.  
  174. }else{
  175.  
  176. $upPage="";
  177.  
  178. }
  179.  
  180.  
  181.  
  182. if($downRow<=$this->totalPages){
  183.  
  184. $downPage="<ahref='".$url."&".$p."=$downRow'>".$this->config['next']."</a>";
  185.  
  186. }else{
  187.  
  188. $downPage="";
  189.  
  190. }
  191.  
  192. //<<<>>>
  193.  
  194. if($nowCoolPage==1){
  195.  
  196. $theFirst="";
  197.  
  198. $prePage="";
  199.  
  200. }else{
  201.  
  202. $preRow=$this->nowPage-$this->rollPage;
  203.  
  204. $prePage="<ahref='".$url."&".$p."=$preRow'>上".$this->rollPage."页</a>";
  205.  
  206. $theFirst="<ahref='".$url."&".$p."=1'>".$this->config['first']."</a>";
  207.  
  208. }
  209.  
  210. if($nowCoolPage==$this->coolPages){
  211.  
  212. $nextPage="";
  213.  
  214. $theEnd="";
  215.  
  216. }else{
  217.  
  218. $nextRow=$this->nowPage+$this->rollPage;
  219.  
  220. $theEndRow=$this->totalPages;
  221.  
  222. $nextPage="<ahref='".$url."&".$p."=$nextRow'>下".$this->rollPage."页</a>";
  223.  
  224. $theEnd="<ahref='".$url."&".$p."=$theEndRow'>".$this->config['last']."</a>";
  225.  
  226. }
  227.  
  228. //12345
  229.  
  230. $linkPage="";
  231.  
  232. for($i=1;$i<=$this->rollPage;$i++){
  233.  
  234. $page=($nowCoolPage-1)*$this->rollPage+$i;
  235.  
  236. if($page!=$this->nowPage){
  237.  
  238. if($page<=$this->totalPages){
  239.  
  240. $linkPage.="&nbsp;<ahref='".$url."&".$p."=$page'>&nbsp;".$page."&nbsp;</a>";
  241.  
  242. }else{
  243.  
  244. break;
  245.  
  246. }
  247.  
  248. }else{
  249.  
  250. if($this->totalPages!=1){
  251.  
  252. $linkPage.="&nbsp;<spanclass='current'>".$page."</span>";
  253.  
  254. }
  255.  
  256. }
  257.  
  258. }
  259.  
  260. //<script>jquery分页</script>
  261.  
  262. $ajax='';
  263.  
  264. if($this->target){
  265.  
  266. $ajax=<<<eco
  267.  
  268. <script>
  269.  
  270. jQuery(function($){
  271.  
  272. $('#{$this->pagesId}a').click(function(){
  273.  
  274. $.ajax({
  275.  
  276. url:$(this).attr('href'),dataType:"html",type:"POST",cache:false,success:function(html){
  277.  
  278. $("#{$this->target}").html(html);
  279.  
  280. }
  281.  
  282. });
  283.  
  284. returnfalse;
  285.  
  286. });
  287.  
  288. });
  289.  
  290. </script>
  291.  
  292. eco;
  293.  
  294. }
  295.  
  296. $pageStr=str_replace(
  297.  
  298. array(
  299.  
  300. '%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%','%ajax%'
  301.  
  302. ),array(
  303.  
  304. $this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd,$ajax
  305.  
  306. ),$this->config['theme']
  307.  
  308. );
  309.  
  310. return$pageStr;
  311.  
  312. }
  313.  
  314. }

调用方法:在tp中:

  1. publicfunctionajaxPage(){
  2.  
  3. $tz=M('MoneyLog');
  4. import('ORG.Util.ajaxpage');
  5. $count=$tz->count();
  6. $page=newPage($count,10,'type=1','ajax_div','ajax_page_div');
  7.  
  8.  
  9. $data=$tz->limit($page->firstRow,$page->listRows)->select();
  10. $pagestr=$page->show();
  11. $this->assign('list',$data);
  12. $this->assign('show',$pagestr);
  13. if(IS_AJAX){
  14. exit($this->fetch('list'));
  15. }
  16. $this->display('list');
  17.  
  18. }

静态页面:

  1. <scripttype="text/javascript"src="/Public/shouye/js/jquery-1.7.1.min.js"></script>
  2. <divid="ajax_div">
  3. <table>
  4. <tr>
  5. <td>性别</td><td>年龄</td><td>单位</td>
  6. </tr>
  7. <?PHPforeach($listas$v){?>
  8. <tr>
  9. <td><?PHPecho$v['total_money'];?></td>
  10. <td><?PHPecho$v['time'];?></td>
  11. <td><?PHPecho$v['ip'];?></td>
  12. </tr>
  13. <?PHP}?>
  14.  
  15. </table>
  16. <div><divid="ajax_page_div">{$show}</div></div>
  17. </div>
  18. <script>
  19.  
  20. $("body").delegate("#ajax_page_diva","click",function(){
  21. $.get(this.href,function(html){
  22. $('#ajax_div').html(html);
  23. });
  24. returnfalse;
  25. });
  26. });
  27. </script>

猜你在找的Ajax相关文章