在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+MYSQL的文章管理系统(二)
Oct 09 PHP
写一个用户在线显示的程序
Oct 09 PHP
PHP开发过程中常用函数收藏
Dec 14 PHP
PHP迅雷、快车、旋风下载专用链转换代码
Jun 15 PHP
PHP OPCode缓存 APC详细介绍
Oct 12 PHP
PHP获取数组中某元素的位置及array_keys函数应用
Jan 29 PHP
PHP转换IP地址到真实地址的方法详解
Jun 09 PHP
php表单提交与$_POST实例分析
Jan 26 PHP
Smarty最简单实现列表奇偶变色的方法
Jul 01 PHP
PHP Mysqli 常用代码集合
Nov 12 PHP
php基于dom实现的图书xml格式数据示例
Feb 03 PHP
PHP实现根据数组的值进行分组的方法
Apr 20 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
打造计数器DIY三步曲(上)
2006/10/09 PHP
php为什么选mysql作为数据库? Mysql 创建用户方法
2007/07/02 PHP
php header功能的使用
2013/10/28 PHP
基于php的CMS中展示文章类实例分析
2015/06/18 PHP
ThinkPHP实现更新数据实例详解(demo)
2016/06/29 PHP
php读取qqwry.dat ip地址定位文件的类实例代码
2016/11/15 PHP
给事件响应函数传参数的四种方式小结
2013/12/05 Javascript
ie9 提示'console' 未定义问题的解决方法
2014/03/20 Javascript
javascript实时显示当天日期的方法
2015/05/20 Javascript
jquery实现鼠标滑过小图查看大图的方法
2015/07/20 Javascript
JavaScript必知必会(三) String .的方法来自何方
2016/06/08 Javascript
全面了解js中的script标签
2016/07/04 Javascript
Angular 路由route实例代码
2016/07/12 Javascript
浅析BootStrap Treeview的简单使用
2016/10/12 Javascript
angular ng-click防止重复提交实例
2017/06/16 Javascript
JS实现百度搜索接口及链接功能实例代码
2018/02/02 Javascript
vue项目使用高德地图的定位及关键字搜索功能的实例代码(踩坑经验)
2020/03/07 Javascript
[01:06]DOTA2隆重推出2016冬季勇士令状 内含上海特级锦标赛互动指南
2016/02/17 DOTA
python爬虫入门教程之糗百图片爬虫代码分享
2014/09/02 Python
Python之list对应元素求和的方法
2018/06/28 Python
python关于调用函数外的变量实例
2019/12/26 Python
Django2.1.7 查询数据返回json格式的实现
2020/12/29 Python
html5+css3之CSS中的布局与Header的实现
2014/11/21 HTML / CSS
StubHub西班牙:购买和出售全球活动门票
2017/06/05 全球购物
英国现代家具和装饰网站:PN Home
2018/08/16 全球购物
介绍一下SQL Server里面的索引视图
2016/07/31 面试题
可靠的数据流传输TCP
2016/03/15 面试题
广告传媒专业应届生求职信
2014/03/01 职场文书
幼儿园中班开学寄语
2014/04/03 职场文书
原告代理词范文
2015/05/25 职场文书
百年孤独读书笔记
2015/06/29 职场文书
寒假生活随笔
2015/08/15 职场文书
2016教师政治学习心得体会
2016/01/23 职场文书
初中物理教学反思
2016/02/19 职场文书
创业计划书之餐饮
2019/09/02 职场文书
2022年显卡天梯图(6月更新)
2022/06/17 数码科技