您的位置:首页 > 编程语言 > PHP开发

thinkphp自带的Page类 + Ajax实现无刷新分页 有视频教程和源文件

2017-06-03 12:51 423 查看
前端代码:	<div class="list">
<foreach name="list" item="vv">
<div class="title">{$vv.title}</div>
<div class="content">{$vv.content}</div>
<hr size="5" color="red" />
</foreach>
<div class="page">
{$page}
</div>
</div>
</body>
<script>
$(function(){
$(".page a").live('click',function(){ //直接用click点击事件因为js没刷新还是会出现跳转,on(“click”,function)事件测试有的页码也会出现跳转
var pageObj = this;
var url = pageObj.href;
$.ajax({
type:'get',
url:url,
success:function(res){
$(".list").html(res);
}
})

return false;//取消a链接的跳转
})

})
</script>
控制器:<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
$article = M('article'); // 实例化User对象
$count      = $article->where('1=1')->count();// 查询满足要求的总记录数
$Page       = new \Think\Page($count,5);// 实例化分页类 传入总记录数和每页显示的记录数(25)
$show       = $Page->show();// 分页显示输出
// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
$list = $article->where('1=1')->limit($Page->firstRow.','.$Page->listRows)->select();
if(IS_AJAX){
$this->assign('list',$list);// 赋值数据集
$this->assign('page',$show);// 赋值分页输出
$html = $this->fetch('Index/ajaxPage');   //另建一个html文件用来获取得到的分页内容
$this->ajaxReturn($html);
}
$this->assign('list',$list);// 赋值数据集
$this->assign('page',$show);// 赋值分页输出
$this->display(); // 输出模板
}
}

ajaxPage页面: <foreach name="list" item="vv">
<div class="title">{$vv.title}</div>
<div class="content">{$vv.content}</div>
<hr size="5" color="red" />
</foreach>
<div class="page">
{$page}
</div>
转载地址:http://www.thinkphp.cn/topic/33736.html  有视频和源文件哦!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: