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

来源:互联网 发布:守望 网络初始化错误 编辑:程序博客网 时间:2024/05/17 00:16
前端代码:<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>
控制器:<?phpnamespace 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  有视频和源文件哦!

原创粉丝点击