Home > php教程 > php手册 > 改造thinkphp 的 Page类,使其支持ajax方式

改造thinkphp 的 Page类,使其支持ajax方式

WBOY
Release: 2016-06-07 11:42:11
Original
1252 people have browsed it

改造thinkphp 的 Page类,使其支持ajax方式
效果:
改造thinkphp 的 Page类,使其支持ajax方式
说明:thinkphp版本:3.2.1
步骤:
1、打开Page.class.php(路径:ThinkPHP/Library/Think)
找到第48行:
 empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);  改为:
 empty(I($this->p)) ? 1 : intval(I($this->p));
实际是改为用内置的 I 函数读取传递进来的P参数,这样并不会影响原有的功能。

2、ajax方式的调用:
即前台html页面
<div>此处用来显示分页内容</div> <br><br> javascript:    function jzry(p) {<br>         var request = $.ajax({<br>             url: "__MODULE__/Home/jzrh",  //用ajax加载分页数据<br>             type: "POST",<br>             async: false,<br>             data:{p:p},<br>             beforeSend: function(xhr) {<br>                 $('#message').html("正在加载数据,请稍候。");<br>             }<br>         });<br>         request.done(function(msg) {<br>             if (msg.bj == 'ok') {<br>                 $('#rs').html(msg.page);    //显示分页<br>                 $("#rs a").removeAttr("href");   //删除原来的连接地址方式,<br>                 <br>                 //改用ajax按键事件<br>                 $(".num,.end,.first").on("click", function(){<br>                     jzry( $(this).text() );<br>                 });<br>                 $(".next").on("click", function(){<br>                     jzry( parseInt($(".current").text())+1);<br>                 });<br>                 $(".prev").on("click", function(){<br>                     jzry( parseInt($(".current").text())-1);<br>                 });<br>         }});<br>         request.fail(function(jqXHR, textStatus) {<br>             $('#message').html("错误: " + textStatus);<br>         });<br>     } 样式: <style><br /> #rs a { color:#333;display:block; padding:0px 6px;<br /> font-weight:bold;float:left;border:1px solid #5d9cdf;<br /> height:20px; line-height:20px; margin:0px 2px;}<br /> .current { background:#a9d2ff; display:block; padding:0px 6px;<br /> font-weight:bold;;float:left}<br /> .clear{clear:both}<br /> </style>
3、后台使用和原来的一样 <br>         $user = M("user"); <br>         $User = $user->page($_POST['p'].',20')->order('id')->select();<br>         if ($User > 0) {<br>             $val['bj'] = 'ok';<br>             $count=$user->count();<br>             $Page=new \Think\Page($count,20);<br>             $val['page']=$Page->show();<br>             $this->ajaxReturn($val);                      //ajax方式返回<br>         }

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template