PHP头条
热点:

THINKPHP3ajax无刷新分页类及实例


最近在网上看见很多关于thinkphp ajax无刷新分页的实例及说明都没有办法很容易的进行,所以我现在进行讲解和分析,这里已THINKPHP3.0 开始讲解

首先需要一个ajax的分页类

找到ThinkPHP\Extend\Library\ORG\Util 目录下的Page.class.php

复制一个Page.class.php 更改名字为AjaxPage.class.php 让这2个文件处与同一目录:

  1. // +---------------------------------------------------------------------- 
  2. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ] 
  3. // +---------------------------------------------------------------------- 
  4. // | Copyright (c) 2009 http://thinkphp.cn All rights reserved. 
  5. // +---------------------------------------------------------------------- 
  6. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) 
  7. // +---------------------------------------------------------------------- 
  8. // | Author: liu21st  
  9. // +---------------------------------------------------------------------- 
  10. // $Id: Page.class.php 2712 2012-02-06 10:12:49Z liu21st $ 
  11. class AjaxPage { 
  12.     // 分页栏每页显示的页数 
  13.     public $rollPage = 5; 
  14.     // 页数跳转时要带的参数 
  15.     public $parameter  ; 
  16.     // 默认列表每页显示行数 
  17.     public $listRows = 20; 
  18.     // 起始行数 
  19.     public $firstRow ; 
  20.     // 分页总页面数 
  21.     protected $totalPages  ; 
  22.     // 总行数 
  23.     protected $totalRows  ; 
  24.     // 当前页数 
  25.     protected $nowPage    ; 
  26.     // 分页的栏的总页数 
  27.     protected $coolPages   ; 
  28.     // 分页显示定制 
  29.     protected $config  = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%'); 
  30.     // 默认分页变量名 
  31.     protected $varPage
  32.  
  33.     public function __construct($totalRows,$listRows='',$ajax_func,$parameter='') { 
  34.         $this->totalRows = $totalRows
  35.         $this->ajax_func = $ajax_func
  36.         $this->parameter = $parameter
  37.         $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ; 
  38.         if(!emptyempty($listRows)) { 
  39.             $this->listRows = intval($listRows); 
  40.         } 
  41.         $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数 
  42.         $this->coolPages  = ceil($this->totalPages/$this->rollPage); 
  43.         $this->nowPage  = !emptyempty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1; 
  44.         if(!emptyempty($this->totalPages) && $this->nowPage>$this->totalPages) { 
  45.             $this->nowPage = $this->totalPages; 
  46.         } 
  47.         $this->firstRow = $this->listRows*($this->nowPage-1); 
  48.     } 
  49. public function setConfig($name,$value) { 
  50.         if(isset($this->config[$name])) { 
  51.             $this->config[$name]    =   $value
  52.         } 
  53.     } 
  54.  
  55.     public function show() { 
  56.         if(0 == $this->totalRows) return ''
  57.         $p = $this->varPage; 
  58.         $nowCoolPage      = ceil($this->nowPage/$this->rollPage); 
  59.         $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this->parameter; 
  60.         $parse = parse_url($url); 
  61.         if(isset($parse['query'])) { 
  62.             parse_str($parse['query'],$params); 
  63.             unset($params[$p]); 
  64.             $url   =  $parse['path'].'?'.http_build_query($params); 
  65.         } 
  66.         //上下翻页字符串 
  67.         $upRow   = $this->nowPage-1; 
  68.         $downRow = $this->nowPage+1; 
  69.         if ($upRow>0){ 
  70.             $upPage=".$this->ajax_func."(".$upRow.")'>".$this->config['prev'].""
  71.         }else
  72.             $upPage=""
  73.         } 
  74. if ($downRow <= $this->totalPages){ 
  75.             $downPage=".$this->ajax_func."(".$downRow.")'>".$this->config['next'].""
  76.         }else
  77.             $downPage=""
  78.         } 
  79.         // << < > >> 
  80.         if($nowCoolPage == 1){ 
  81.             $theFirst = ""
  82.             $prePage = ""
  83.         }else
  84.             $preRow =  $this->nowPage-$this->rollPage; 
  85.             $prePage = ".$this->ajax_func."(".$preRow.")'>上".$this->rollPage."页"
  86.             $theFirst = ".$this->ajax_func."(1)' >".$this->config['first'].""
  87.         } 
  88.         if($nowCoolPage == $this->coolPages){ 
  89.             $nextPage = ""
  90.             $theEnd=""
  91.         }else
  92.             $nextRow = $this->nowPage+$this->rollPage; 
  93.             $theEndRow = $this->totalPages; 
  94.             $nextPage = ".$this->ajax_func."(".$nextRow.")' >下".$this->rollPage."页"
  95.             $theEnd = ".$this->ajax_func."(".$theEndRow.")' >".$this->config['last'].""
  96.         } 
  97.         // 1 2 3 4 5 
  98.         $linkPage = ""
  99.         for($i=1;$i<=$this->rollPage;$i++){ 
  100.             $page=($nowCoolPage-1)*$this->rollPage+$i
  101.             if($page!=$this->nowPage){ 
  102.                 if($page<=$this->totalPages){ 
  103.                    $linkPage .= .$this->ajax_func."(".$page.")'> ".$page." "
  104.                 }else
  105.                     break
  106.                 } 
  107.             }else
  108.                 if($this->totalPages != 1){ 
  109.                     $linkPage .= ".$page.""
  110.                 } 
  111.             } 
  112.         } 
  113.         $pageStr  =  str_replace
  114.             array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'), 
  115.             array($this->config['header'],$this->nowPage,$this->totalRows,$this->totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this->config['theme']); 
  116.         return $pageStr
  117.     } 
  118. ?> 

以上是我自己修改过的AjaxPage类

然后就要写控制器(Action):

  1. public function test(){ 
  2.   import("ORG.Util.AjaxPage");// 导入分页类  注意导入的是自己写的AjaxPage类 
  3.    
  4.   $credit = M('test'); 
  5.    
  6.   $count = $credit->count(); //计算记录数 
  7.         $limitRows = 15; // 设置每页记录数 
  8.         
  9.         $p = new AjaxPage($count$limitRows,"test"); //第三个参数是你需要调用换页的ajax函数名 
  10.         $limit_value = $p->firstRow . "," . $p->listRows; 
  11.         
  12.         $data = $credit->order('id desc')->limit($limit_value)->select(); // 查询数据 
  13.         $page = $p->show(); // 产生分页信息,AJAX的连接在此处生成 
  14.     $this->assign('list',$data); 
  15.         $this->assign('page',$page); 
  16.         $this->display(); 
  17.  } 

这里没有进行Ajax的返回,如果需要就要自己修改AjaxPage类了哈,我觉得这样方便,就没有修改.

tpl页面重要部分:

  1. <div id='test'>   //这里的test 和下面js中的test要一致 
  2. <volist id='list' name='list'>   //内容输出 
  3.    {$list.id}{$list.test} 
  4. volist> 
  5. {$page}  //分页输出 
  6. div> 

javascrip编写,调用了jquery 框架:

  1. function test(id){    //test函数名 一定要和action中的第三个参数一致上面有 
  2.  var id = id; 
  3.         $.get('Test/test', {'p':id}, function(data){  //用get方法发送信息到TestAction中的test方法 
  4.      $("#test").replaceWith(""+data+""); //test一定要和tpl中的一致 
  5.     }); 
  6.  } 

www.phpzy.comtrue/phpkj/9494.htmlTechArticleTHINKPHP3ajax无刷新分页类及实例 最近在网上看见很多关于thinkphp ajax无刷新分页的实例及说明都没有办法很容易的进行,所以我现在进行讲解和分析,这里已THINKPHP3.0 开始讲解 首先需要一个...

相关文章

相关频道:

PHP之友评论

今天推荐