麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁(yè) > 開(kāi)發(fā) > ThinkPHP > 正文

THINKPHP3 ajax無(wú)刷新分頁(yè)類(lèi)及實(shí)例

2024-09-09 15:20:01
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最近在網(wǎng)上看見(jiàn)很多關(guān)于thinkphp ajax無(wú)刷新分頁(yè)的實(shí)例及說(shuō)明都沒(méi)有辦法很容易的進(jìn)行,所以我現(xiàn)在進(jìn)行講解和分析,這里已THINKPHP3.0 開(kāi)始講解

首先需要一個(gè)ajax的分頁(yè)類(lèi)

找到ThinkPHP/Extend/Library/ORG/Util 目錄下的Page.class.php

復(fù)制一個(gè)Page.class.php 更改名字為AjaxPage.class.php 讓這2個(gè)文件處與同一目錄:

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

以上是我自己修改過(guò)的AjaxPage類(lèi)

然后就要寫(xiě)控制器(Action):

  1. public function test(){ 
  2.   import("ORG.Util.AjaxPage");// 導(dǎo)入分頁(yè)類(lèi)  注意導(dǎo)入的是自己寫(xiě)的AjaxPage類(lèi) 
  3.    
  4.   $credit = M('test'); 
  5.    
  6.   $count = $credit->count(); //計(jì)算記錄數(shù) 
  7.         $limitRows = 15; // 設(shè)置每頁(yè)記錄數(shù) 
  8.         
  9.         $p = new AjaxPage($count$limitRows,"test"); //第三個(gè)參數(shù)是你需要調(diào)用換頁(yè)的ajax函數(shù)名 
  10.         $limit_value = $p->firstRow . "," . $p->listRows; 
  11.         
  12.         $data = $credit->order('id desc')->limit($limit_value)->select(); // 查詢(xún)數(shù)據(jù) 
  13.         $page = $p->show(); // 產(chǎn)生分頁(yè)信息,AJAX的連接在此處生成 
  14.     $this->assign('list',$data); 
  15.         $this->assign('page',$page); 
  16.         $this->display(); 
  17.  } 

這里沒(méi)有進(jìn)行Ajax的返回,如果需要就要自己修改AjaxPage類(lèi)了哈,我覺(jué)得這樣方便,就沒(méi)有修改.

tpl頁(yè)面重要部分:

  1. <div id='test'>   //這里的test 和下面js中的test要一致 
  2. <volist id='list' name='list'>   //內(nèi)容輸出 
  3.    {$list.id}{$list.test} 
  4. </volist> 
  5. {$page}  //分頁(yè)輸出 
  6. </div> 

javascrip編寫(xiě),調(diào)用了jquery 框架:

  1. function test(id){    //test函數(shù)名 一定要和action中的第三個(gè)參數(shù)一致上面有 
  2.  var id = id; 
  3.         $.get('Test/test', {'p':id}, function(data){  //用get方法發(fā)送信息到TestAction中的test方法 
  4.      $("#test").replaceWith("<div  id='test'>"+data+"</div>"); //test一定要和tpl中的一致 
  5.     }); 
  6.  } 

發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 一本色道久久综合亚洲精品图片 | 伊人亚洲精品 | 香蕉久草视频 | 日韩视频高清 | 欧美成人精品h版在线观看 国产一级淫片在线观看 | 精品视频 久久久 | 国产亚洲精品美女久久久 | 久久久久久久一区二区 | 主播粉嫩国产在线精品 | 精品一区二区免费视频视频 | 香蕉成人在线视频 | www嫩草 | 久久99综合 | 特一级黄色毛片 | 国产污污视频 | 美国黄色毛片女人性生活片 | 激情综合婷婷久久 | 久久精品一二三区 | 免费视频www在线观看 | 精品国产一区二区三区久久久蜜 | 加勒比色综合 | 黄色片网站在线看 | 毛片免费观看视频 | 中文字幕在线观看日韩 | 成人三级电影网站 | 国产精品视频中文字幕 | 中文字幕涩涩久久乱小说 | 美国av免费看 | 羞羞的视频免费 | 男人天堂免费 | 爱操影视| 毛片大全免费看 | 伊人成人免费视频 | 91精品国产综合久久久动漫日韩 | xx53xx| 热99在线视频 | 黄色影院网站 | 黄色大片在线免费看 | 久章草影院| 亚洲电影免费观看国语版 | lutube成人福利在线观看 |