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


Posted in PHP onOctober 25, 2016

在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实现无刷新分页的方法,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对三水点靠木网站的支持!

PHP 相关文章推荐
php中获得视频时间总长度的另一种方法
Sep 15 PHP
php中并发读写文件冲突的解决方案
Oct 25 PHP
支持中文和其他编码的php截取字符串函数分享(截取中文字符串)
Mar 13 PHP
PHP中cookie和session的区别实例分析
Aug 28 PHP
PHP面试题之文件目录操作
Oct 15 PHP
php简单实现多维数组排序的方法
Sep 30 PHP
php foreach如何跳出两层循环(详解)
Nov 05 PHP
PHP截取发动短信内容的方法
Jul 04 PHP
一个实用的php验证码类
Jul 06 PHP
PHP实现 APP端微信支付功能
Jun 22 PHP
PHP fclose函数用法总结
Feb 15 PHP
laravel框架实现去掉URL中index.php的方法
Oct 12 PHP
PHP上传Excel文件导入数据到MySQL数据库示例
Oct 25 #PHP
详解PHP中foreach的用法和实例
Oct 25 #PHP
php array_keys 返回数组的键名
Oct 25 #PHP
php array_key_exists() 与 isset() 的区别
Oct 24 #PHP
PHP实现简易blog的制作
Oct 24 #PHP
php基于websocket搭建简易聊天室实践
Oct 24 #PHP
详解php中 === 的使用
Oct 24 #PHP
You might like
php实现统计目录文件大小的函数
2015/12/25 PHP
php创建图像具体步骤
2017/03/13 PHP
Thinkphp 框架扩展之应用模式实现方法分析
2020/04/27 PHP
jQuery不兼容input的change事件问题解决过程
2014/12/05 Javascript
使用AngularJS创建单页应用的编程指引
2015/06/19 Javascript
AngularJS入门教程之AngularJS 模板
2016/08/18 Javascript
D3.js实现柱状图的方法详解
2016/09/21 Javascript
微信小程序 request接口的封装实例代码
2017/04/26 Javascript
深入理解Vue-cli搭建项目后的目录结构探秘
2017/07/13 Javascript
深入探究angular2 UI组件之primeNG用法
2017/07/26 Javascript
JS温故而知新之变量提升和时间死区
2019/01/27 Javascript
微信小程序:数据存储、传值、取值详解
2019/05/07 Javascript
python tornado微信开发入门代码
2018/08/24 Python
python 将json数据提取转化为txt的方法
2018/10/26 Python
对pycharm 修改程序运行所需内存详解
2018/12/03 Python
使用Python-OpenCV向图片添加噪声的实现(高斯噪声、椒盐噪声)
2019/05/28 Python
Numpy之将矩阵拉成向量的实例
2019/11/30 Python
Numpy 理解ndarray对象的示例代码
2020/04/03 Python
python是怎么被发明的
2020/06/15 Python
什么是python的自省
2020/06/21 Python
利用scikitlearn画ROC曲线实例
2020/07/02 Python
python如何获得list或numpy数组中最大元素对应的索引
2020/11/16 Python
25个CSS3动画按钮和菜单教程分享
2012/10/03 HTML / CSS
H5仿微信界面教程(一)
2017/07/05 HTML / CSS
世界上最大的皮肤科医生拥有和经营的美容网站:LovelySkin
2021/01/03 全球购物
某公司部分笔试题
2013/11/05 面试题
药学专业大学生个人的自我评价
2013/11/04 职场文书
电气技术员岗位职责
2013/11/19 职场文书
司机职责范本
2014/03/08 职场文书
机电一体化毕业生自荐信
2014/06/19 职场文书
公司合并协议书范本
2014/09/30 职场文书
2014年环境卫生工作总结
2014/11/24 职场文书
教师节联欢会主持词
2015/07/04 职场文书
2015年“我们的节日·重阳节”活动总结
2015/07/29 职场文书
生产设备维护保养制度
2015/08/06 职场文书
详解CSS开发过程中的20个快速提升技巧
2021/05/21 HTML / CSS