Thinkphp3.1 修改自带分页为ajax分页

原创
2016/08/22 16:00
阅读数 639

样式为站酷   论坛里找到的  顺便根据自己需求小改了一下 

类似这样:

php文件路径:项目\ThinkPHP\Extend\Library\ORG\Util\AjaxPage.class.php

<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id: Page.class.php 2712 2012-02-06 10:12:49Z liu21st $

class AjaxPage {
    // 分页栏每页显示的页数
    public $rollPage = 6;
    // 页数跳转时要带的参数
    public $parameter  ;
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow ;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页显示定制
	
	protected $config  =    array('header'=>'条记录','prev'=>'<i></i>','next'=>'<i></i>','first'=>'1','last'=>'<span id="lastspan">最后一页</span>','theme'=>'<ul>%upPage% %first% %prePage% %linkPage% %end% %downPage% %nextPage%<li><span> %totalRow% %header% 条记录</span></li></ul>'); 
	
    // 默认分页变量名
    protected $varPage;


    public function __construct($totalRows,$listRows='',$ajax_func,$parameter='') {
        $this->totalRows = $totalRows;
        $this->ajax_func = $ajax_func;
        $this->parameter = $parameter;
        $this->varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //总页数
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }
	 

	public function setConfig($name,$value) {
        if(isset($this->config[$name])) {
            $this->config[$name]    =   $value;
        }
    }
	
	public function show()
    {
    $adjacents=2;
    if(0 == $this->totalRows) return '';
    $p              =   $this->varPage;
    $nowCoolPage    =   ceil($this->nowPage/$this->rollPage);
          
    //上下翻页字符串
    $upRow          =   $this->nowPage-1;
    $downRow        =   $this->nowPage+1;
    
    // 上一页
    if ($upRow>0){
         $pages.=   "<li><a class='turn_page prev' href='javascript:".$this->ajax_func."(".$upRow.")'>".$this->config['prev']."</a></li>";
    }
     
    //第一页
    if($this->nowPage>($adjacents+1)) {
        $pages.= "<li><a href='javascript:".$this->ajax_func."(1)'>1</a></li>";
    }
     
    // 添加省略号
    if($this->nowPage>($adjacents+2)) {
        $pages.= "<li><b>···</b></li>";
    }
     
    // 12345
		$pmin = ($this->nowPage>$adjacents) ? ($this->nowPage-$adjacents) : 1;
		$pmax = ($this->nowPage<($this->totalPages-$adjacents)) ? ($this->nowPage+$adjacents) : $this->totalPages;
		if($pmax > 1){
			for($i=$pmin; $i<=$pmax; $i++) {
				if($i==$this->nowPage) {
					$pages.= "<li><a class='current'>".$i."</a></li>";
				}else{
					$pages.= "<li><a href='javascript:".$this->ajax_func."(".$i.")'>".$i."</a></li>";
				}
			}
		}
     
    // 添加省略号
    if($this->nowPage < ($this->totalPages-$adjacents-1)) {
        $pages.= "<li><b>···</b></li>";
    }
     
    // 最后一页
    if($this->nowPage<($this->totalPages-$adjacents)) {
        
        $pages.= "<li><a href='javascript:".$this->ajax_func."(".$this->totalPages.")'>".$this->totalPages."</a></li>";
    }
     
    // 下一页
    if ($downRow <= $this->totalPages){
         $pages.=   "<li><a class='turn_page next' href='javascript:".$this->ajax_func."(".$downRow.")'>".$this->config['next']."</a></li>";
    }
	//条数
	if ($this->totalRows > 10){
		$pages .= '<li class="count_page">共计<u>'.$this->totalRows.'</u>条记录</li>';
    }

    return $pages;
    }

}

父类里引入php  

import("ORG.Util.AjaxPage");

方法:

$count = $RatingYes->countNum($this->starttime,$this->endtime,$value,$key,$provinces);
$p = new AjaxPage($count,10,"ajaxPage"); 
$limit = $p->firstRow.','.$p->listRows;
$data = $RatingYes->rating($this->starttime,$this->endtime,$limit,$value,$key,$provinces);
$page = $p->show(); 
$this->assign('data',$data);
$this->assign('page',$page);

前端:

{page}  就是分页代码

 

展开阅读全文
打赏
0
0 收藏
分享
加载中

引用来自“衣渐宽”的评论

楼主公司非IT行业 再加渣渣水平 只能研究下thinkphp这种简单的框架 大牛勿喷
无需妄自菲薄:)
2016/08/22 16:11
回复
举报
如花花博主
楼主公司非IT行业 再加渣渣水平 只能研究下thinkphp这种简单的框架 大牛勿喷
2016/08/22 16:02
回复
举报
更多评论
打赏
2 评论
0 收藏
0
分享
返回顶部
顶部