日期:2014-05-16  浏览次数:20745 次

ThinkPHP & Jquery Ajax分页演示

演示一下jquery 处理json 并根据静态html模板生成相关内容

IndexAction 文件代码

  1. class IndexAction extends Action
  2. {
  3.         public function index()
  4.         {
  5.                 $Page = D('Page');
  6.                
  7.                 import("ORG.Util.Page");       
  8.                 $count = $Page->count();
  9.                 $p     = new Page($count,10);
  10.                
  11.                 $json['list'] = $Page->findAll('','','id DESC',$p->firstRow.','.$p->listRows);
  12.                 $json['page'] = $this->ajax_page($p->show(true));
  13.                
  14.                 $json = json_encode($json);
  15.                
  16.                 if(empty($_GET['do']))
  17.                 {
  18.                     $this->assign('json',$json);
  19.                         $this->display();
  20.                 }
  21.                 else
  22.                 {
  23.                     echo $json;
  24.                 }
  25.         }
  26.        
  27.         //--> ajax分页扩展
  28.         public function ajax_page($page_array=array(),$step=10)
  29.         {
  30.             if ($page_array['totalPages'] >1)
  31.                 {
  32.                         $page['first'] = 1;
  33.                        
  34.                         $pre = intval($page_array['nowPage']/$step);       
  35.                         $pre >0 ? $first = $page_array['nowPage']-$step : $first = $pre*$step+1 ;
  36.                         $pre >0 ? $end   = $page_array['nowPage']+$step : $end   = ($pre+1)*$step;
  37.                         if ($end >$page_array['totalPages'])
  38.                         {
  39.                             $first = $page_array['totalPages'] - $step;
  40.