ThinkPHP5.1+Ajax实现的无刷新分页功能示例
2020-02-13 11:31
811 查看
本文实例讲述了ThinkPHP5.1+Ajax实现的无刷新分页功能。分享给大家供大家参考,具体如下:
无刷新分页可以减轻服务器负担,利用Ajax技术,请求部分信息,提高网站访问速度,是网站建设的必备技术。
需要在后台展示自定义属性列表(lst.html),其中的列表部分摘出来,放到(paginate1.html)中:
<div class="row"> <div class="col-sm-12"> <div class="ibox float-e-margins"> <div class="ibox-content"> <table class="table table-bordered"> <thead> <tr> <th>ID</th> <th>名称</th> <th>取值</th> <th>显示</th> <th>排序</th> <th>操作</th> </tr> </thead> <tbody> {volist name="self" id="vo"} <tr> <td>{$vo.id}</td> <td>{$vo.name}</td> <td>{$vo.value}</td> <td> {if $vo.isshow==1} <button type="button" class="btn btn-success btn-sm">是</button> {else/} <button type="button" class="btn btn-danger btn-sm">否</button> {/if} </td> <td><input type="text" value="{$vo.order}" name=""></td> <td> <div class="btn-group open"> <button data-toggle="dropdown" class="btn btn-primary dropdown-toggle" aria-expanded="true">操作 <span class="caret"></span> </button> <ul class="dropdown-menu"> <li><a href="">修改</a> </li> <li><a href="">删除</a> </li> </ul> </div> </td> </tr> {/volist} </tbody> </table> {$self|raw} <div class="row"> <div class="col-sm-2"> <button class="btn btn-success" type="button" id="changeOrder"> <i class="fa fa-plus-square"></i> <span class="bold">排序</span> </button> </div> </div> </div> </div> </div> </div>
其中self是服务器端传递过来的自定义属性,并进行了分页操作:
$selfattribute_select = db("selfattribute")->paginate(5); $this->assign("self",$selfattribute_select);
因为lst.html把列表摘了出来,所以还要在引入回去,才能使页面完整,同时,为了方便进行jquery操作,把列表用带id的div包裹起来:
<div id="paginate"> {include file="selfattribute/paginate1"} </div>
ThinkPHP5.1带的分页类使用的是BootStrap样式,它在页面显示时实际会有一个pagination的类,查看源代码如下:
<ul class="pagination"> <li class="disabled"> <span>«</span></li> <li class="active"> <span>1</span></li> <li> <a href="/xkershouche/public/admin/selfattribute/lst.html?page=2" rel="external nofollow" rel="external nofollow" >2</a></li> <li> <a href="/xkershouche/public/admin/selfattribute/lst.html?page=3" rel="external nofollow" >3</a></li> <li> <a href="/xkershouche/public/admin/selfattribute/lst.html?page=4" rel="external nofollow" >4</a></li> <li> <a href="/xkershouche/public/admin/selfattribute/lst.html?page=5" rel="external nofollow" >5</a></li> <li> <a href="/xkershouche/public/admin/selfattribute/lst.html?page=6" rel="external nofollow" >6</a></li> <li> <a href="/xkershouche/public/admin/selfattribute/lst.html?page=2" rel="external nofollow" rel="external nofollow" >»</a></li> </ul>
这就是好多人搞不懂的pagination是怎么来的。
然后开始写js代码,因为我们的分页按钮也在被请求的页面当中,属于“未来”的元素,所以这里我们要用on方法,这个方法是jquery1.7以后的方法,注意自己的jquery版本。
<script type="text/javascript"> $(document).on('click', '.pagination a', function(event) { var url = $(this).attr('href'); $.ajax({ url: url, type: 'get', }) .done(function(data) { $("#paginate").html(data); }) return false; }); </script>
其中.done()方法和success方法是一样的,return false是为了阻止默认事件,防止直接跳转。
那么服务器端就可以根据情况渲染模板了,代码如下:
public function lst() { $selfattribute_select = db("selfattribute")->paginate(5); $this->assign("self",$selfattribute_select); if (request()->isAjax()) { return view("paginate1"); } else { return view(); } }
更多关于thinkPHP相关内容感兴趣的读者可查看本站专题:《ThinkPHP入门教程》、《thinkPHP模板操作技巧总结》、《ThinkPHP常用方法总结》、《codeigniter入门教程》、《CI(CodeIgniter)框架进阶教程》、《Zend FrameWork框架入门教程》及《PHP模板技术总结》。
希望本文所述对大家基于ThinkPHP框架的PHP程序设计有所帮助。
您可能感兴趣的文章:
相关文章推荐
- thinkPHP5框架实现基于ajax的分页功能示例
- thinkPHP5.1框架使用SemanticUI实现分页功能示例
- ThinkPHP3.2框架自带分页功能实现方法示例
- Jquery+php+mysql实现上拉加载更多,后端CI,超详细,有示例(其实就是ajax无刷新分页)
- 在Thinkphp中使用AJAX实现无刷新分页
- Ajax(asp.net)技术实现异步刷新和控件功能示例流程
- thinkPHP基于ajax实现的菜单与分页示例
- thinkPHP多表查询及分页功能实现方法示例
- tp5框架基于Ajax实现列表无刷新排序功能示例
- thinkPHP5框架实现分页查询功能的方法示例
- ThinkPHP5.1+Ajax无刷新分页
- Gridview各种功能+AspNetPager+Ajax实现无刷新存储过程分页 (留着用)
- ThinkPHP5&5.1实现验证码的生成、使用及点击刷新功能示例
- AJAX学习明灯,简单示例:AJAX+存储过程实现无刷新简单登录验证功能!
- thinkphp下实现ajax无刷新分页
- thinkphp中使用ajax实现无刷新分页
- asp.net+ajax+json来实现无刷新分页功能
- thinkphp ajax 无刷新分页效果的实现
- ThinkPHP实现ajax无刷新分页
- ajax实现无刷新分页功能