欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

在Thinkphp中使用ajax实现无刷新分页的方法

程序员文章站 2024-04-02 11:13:58
在thinkphp目录的lib\org\util\目录里新建ajaxpage.class.php,写入一下内容:

在thinkphp目录的lib\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 = 5;
// 页数跳转时要带的参数
public $parameter ;
// 默认列表每页显示行数
public $listrows = 20;
// 起始行数
public $firstrow ;
// 分页总页面数
protected $totalpages ;
// 总行数
protected $totalrows ;
// 当前页数
protected $nowpage ;
// 分页的栏的总页数
protected $coolpages ;
// 分页显示定制
protected $config = array('header'=>'条记录','prev'=>'上一页','next'=>'下一页','first'=>'第一页','last'=>'最后一页','theme'=>' %totalrow% %header% %nowpage%/%totalpage% 页 %uppage% %downpage% %first% %prepage% %linkpage% %nextpage% %end%');
// 默认分页变量名
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() {
if(0 == $this->totalrows) return '';
$p = $this->varpage;
$nowcoolpage = ceil($this->nowpage/$this->rollpage);
$url = $_server['request_uri'].(strpos($_server['request_uri'],'?')?'':"?").$this->parameter;
$parse = parse_url($url);
if(isset($parse['query'])) {
parse_str($parse['query'],$params);
unset($params[$p]);
$url = $parse['path'].'?'.http_build_query($params);
}
//上下翻页字符串
$uprow = $this->nowpage-1;
$downrow = $this->nowpage+1;
if ($uprow>0){
$uppage="<a id='big' href='javascript:".$this->ajax_func."(".$uprow.")'>".$this->config['prev']."</a>";
}else{
$uppage="";
}
if ($downrow <= $this->totalpages){
$downpage="<a id='big' href='javascript:".$this->ajax_func."(".$downrow.")'>".$this->config['next']."</a>";
}else{
$downpage="";
}
// << < > >>
if($nowcoolpage == 1){
$thefirst = "";
$prepage = "";
}else{
$prerow = $this->nowpage-$this->rollpage;
$prepage = "<a id='big' href='javascript:".$this->ajax_func."(".$prerow.")'>上".$this->rollpage."页</a>";
$thefirst = "<a id='big' href='javascript:".$this->ajax_func."(1)' >".$this->config['first']."</a>";
}
if($nowcoolpage == $this->coolpages){
$nextpage = "";
$theend="";
}else{
$nextrow = $this->nowpage+$this->rollpage;
$theendrow = $this->totalpages;
$nextpage = "<a id='big' href='javascript:".$this->ajax_func."(".$nextrow.")' >下".$this->rollpage."页</a>";
$theend = "<a id='big' href='javascript:".$this->ajax_func."(".$theendrow.")' >".$this->config['last']."</a>";
}
// 1 2 3 4 5
$linkpage = "";
for($i=1;$i<=$this->rollpage;$i++){
$page=($nowcoolpage-1)*$this->rollpage+$i;
if($page!=$this->nowpage){
if($page<=$this->totalpages){
$linkpage .= " <a id='big' href='javascript:".$this->ajax_func."(".$page.")'> ".$page." </a>";
}else{
break;
}
}else{
if($this->totalpages != 1){
$linkpage .= " <span class='current'>".$page."</span>";
}
}
}
$pagestr = str_replace(
array('%header%','%nowpage%','%totalrow%','%totalpage%','%uppage%','%downpage%','%first%','%prepage%','%linkpage%','%nextpage%','%end%'),
array($this->config['header'],$this->nowpage,$this->totalrows,$this->totalpages,$uppage,$downpage,$thefirst,$prepage,$linkpage,$nextpage,$theend),$this->config['theme']);
return $pagestr;
}
}
?>

控制器里写入以下内容:

<?php
class useraction extends action{
public function user(){
import("org.util.ajaxpage");// 导入分页类 注意导入的是自己写的ajaxpage类
$credit = m('user');
$count = $credit->count(); //计算记录数
$limitrows = 5; // 设置每页记录数
$p = new ajaxpage($count, $limitrows,"user"); //第三个参数是你需要调用换页的ajax函数名
$limit_value = $p->firstrow . "," . $p->listrows;
$data = $credit->order('id desc')->limit($limit_value)->select(); // 查询数据
$page = $p->show(); // 产生分页信息,ajax的连接在此处生成
$this->assign('list',$data);
$this->assign('page',$page);
$this->display();
}
}
?>

模板文件如下:

<html>
<head>
<title>ajax无刷新分页</title>
<script type="text/javascript" src="../public/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
function user(id){ //user函数名 一定要和action中的第三个参数一致上面有
var id = id;
$.get('user/user', {'p':id}, function(data){ //用get方法发送信息到useraction中的user方法
$("#user").replacewith("<div id='user'>"+data+"</div>"); //user一定要和tpl中的一致
});
}
</script>
</head>
<body>
<div id='user'> <!--这里的user和下面js中的test要一致-->
<volist id='list' name='list'> <!--内容输出-->
<{$list.id}>  <{$list.username}><br/>
</volist>
<{$page}> <!--分页输出-->
</div>
</body>
</html>

以上所述是小编给大家介绍的在thinkphp中使用ajax实现无刷新分页的方法,希望对大家有所帮助