具体为什么用异步来做分页我就不多说了!

用异步来做分页,主要还是看分页类怎么玩!

方便管理,还是把Ajax分页作为一个工具来使用:

同样新建工具类:

多次尝试,最终修改好的分页类是这样的:(我自己使用还是比较爽的)

  1. <?php
  2. namespace Components;
  3. class AjaxPage {
  4. public $firstRow; // 起始行数
  5. public $listRows; // 列表每页显示行数
  6. public $parameter; // 分页跳转时要带的参数
  7. public $totalRows; // 总行数
  8. public $totalPages; // 分页总页面数
  9. public $rollPage = 11;// 分页栏每页显示的页数
  10. public $lastSuffix = true; // 最后一页是否显示总页数
  11.  
  12. private $p = 'p'; //分页参数名
  13. private $url = ''; //当前链接URL
  14. private $nowPage = 1;
  15.  
  16. // 分页显示定制
  17. private $config = array(
  18. 'header' => '<span class="rows">共 %TOTAL_ROW% 条记录</span>',
  19. 'prev' => '<<',
  20. 'next' => '>>',
  21. 'first' => '1...',
  22. 'last' => '...%TOTAL_PAGE%',
  23. 'theme' => '%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%',
  24. );
  25.  
  26. /**
  27. * 架构函数
  28. * @param array $totalRows 总的记录数
  29. * @param array $listRows 每页显示记录数
  30. * @param array $parameter 分页跳转的参数
  31. */
  32. public function __construct($totalRows, $listRows=20,$ajax_func, $parameter = array()) {
  33. C('VAR_PAGE') && $this->p = C('VAR_PAGE'); //设置分页参数名称
  34. /* 基础设置 */
  35. $this->totalRows = $totalRows; //设置总记录数
  36. $this->ajax_func = $ajax_func;
  37. $this->listRows = $listRows; //设置每页显示行数
  38. $this->parameter = empty($parameter) ? $_GET : $parameter;
  39. $this->nowPage = empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
  40. $this->nowPage = $this->nowPage>0 ? $this->nowPage : 1;
  41. $this->firstRow = $this->listRows * ($this->nowPage - 1);
  42. }
  43.  
  44. /**
  45. * 定制分页链接设置
  46. * @param string $name 设置名称
  47. * @param string $value 设置值
  48. */
  49. public function setConfig($name,$value) {
  50. if(isset($this->config[$name])) {
  51. $this->config[$name] = $value;
  52. }
  53. }
  54.  
  55. /**
  56. * 生成链接URL
  57. * @param integer $page 页码
  58. * @return string
  59. */
  60. private function url($page){
  61. return str_replace(urlencode('[PAGE]'), $page, $this->url);
  62. }
  63.  
  64. /**
  65. * 组装分页链接
  66. * @return string
  67. */
  68. public function show() {
  69. if(0 == $this->totalRows) return '';
  70.  
  71. /* 生成URL */
  72. $this->parameter[$this->p] = '[PAGE]';
  73. $a = u(__SELF__);
  74. $b = substr($a,15,strlen($a));
  75. $this->url = U($b, $this->parameter);
  76. /* 计算分页信息 */
  77. $this->totalPages = ceil($this->totalRows / $this->listRows); //总页数
  78. if(!empty($this->totalPages) && $this->nowPage > $this->totalPages) {
  79. $this->nowPage = $this->totalPages;
  80. }
  81.  
  82. /* 计算分页临时变量 */
  83. $now_cool_page = $this->rollPage/2;
  84. $now_cool_page_ceil = ceil($now_cool_page);
  85. $this->lastSuffix && $this->config['last'] = $this->totalPages;
  86.  
  87. //上一页
  88. $up_row = $this->nowPage - 1;
  89. //$up_page = $up_row > 0 ? '<a href="javascript:' . $this->ajax_func.'('.$up_row.')" class="prev">' . $this->config['prev'] . '</a>' : '<a href="javascript:;" class="prev">' . $this->config['prev'] . '</a>';
  90. $up_page = $up_row > 0 ? '<a href="javascript:;" class="prev">' . $this->config['prev'] . '</a>' : '<a href="javascript:;" class="prev">' . $this->config['prev'] . '</a>';
  91.  
  92. //下一页
  93. $down_row = $this->nowPage + 1;
  94. //$down_page = ($down_row <= $this->totalPages) ? '<a href="javascript:' . $this->ajax_func.'('.$down_row.')" class="next">' . $this->config['next'] . '</a>' : '<a class="next">' . $this->config['next'] . '</a>';
  95. $down_page = ($down_row <= $this->totalPages) ? '<a href="javascript:;" class="next">' . $this->config['next'] . '</a>' : '<a href="javascript:;" class="next">' . $this->config['next'] . '</a>';
  96.  
  97. //第一页
  98. $the_first = '';
  99. if($this->totalPages > $this->rollPage && ($this->nowPage - $now_cool_page) >= 1){
  100. $the_first = '<a href="javascript:' . $this->ajax_func . '(1);" class="com">' . $this->config['first'] . '</a>';
  101. }
  102.  
  103. //最后一页
  104. $the_end = '';
  105. if($this->totalPages > $this->rollPage && ($this->nowPage + $now_cool_page) < $this->totalPages){
  106. $the_end = '<a href="javascript:' . $this->ajax_func . '('.$this->totalPages.');" class="com">' . $this->config['last'] . '</a>';
  107. }
  108.  
  109. //数字连接
  110. $link_page = "";
  111. for($i = 1; $i <= $this->rollPage; $i++){
  112. if(($this->nowPage - $now_cool_page) <= 0 ){
  113. $page = $i;
  114. }elseif(($this->nowPage + $now_cool_page - 1) >= $this->totalPages){
  115. $page = $this->totalPages - $this->rollPage + $i;
  116. }else{
  117. $page = $this->nowPage - $now_cool_page_ceil + $i;
  118. }
  119. if($page > 0 && $page != $this->nowPage){
  120.  
  121. if($page <= $this->totalPages){
  122. $link_page .= '<a href="javascript:' . $this->ajax_func . '(' . $page . ');" class="com">' . $page . '</a>';
  123. }else{
  124. break;
  125. }
  126. }else{
  127. if($page > 0){
  128. $link_page .= '<a href="javascript:' . $this->ajax_func . '(' . $page . ');" class="active com">' . $page . '</a>';
  129. }
  130. }
  131. }
  132.  
  133. //替换分页内容
  134. $page_str = str_replace(
  135. array('%HEADER%', '%NOW_PAGE%', '%UP_PAGE%', '%DOWN_PAGE%', '%FIRST%', '%LINK_PAGE%', '%END%', '%TOTAL_ROW%', '%TOTAL_PAGE%'),
  136. array($this->config['header'], $this->nowPage, $up_page, $down_page, $the_first, $link_page, $the_end, $this->totalRows, $this->totalPages),
  137. $this->config['theme']);
  138. return "{$page_str}";
  139. }
  140. }

接下来写控制器:

  1. <?php
  2. namespace Home\Controller;
  3. use Think\Controller;
  4.  
  5. class LingdaoController extends Controller {
  6. public function index(){
  7. //分页处理
  8. $count = M('document')->count();
  9. //$page = new \Think\Page($count,2);
  10. $page = new \Components\AjaxPage($count,5,'show');
  11. $show = $page->show();
  12. $list = M('document')->limit($page->firstRow.','.$page->listRows)->select();
  13. $this->assign('list',$list);
  14. $this->assign('page',$show);
  15. $this->display();
  16. }
  17. public function ajaxpage(){
  18. //分页处理
  19. $count = M('document')->count();
  20. //$page = new \Think\Page($count,2);
  21. $page = new \Components\AjaxPage($count,5,'show');
  22. $show = $page->show();
  23. $list = M('document')->limit($page->firstRow.','.$page->listRows)->select();
  24. foreach($list as $k => $v){
  25. echo '<li>'.$k.'=='.$v['title'].'</li>';
  26. }
  27. }
  28. }

经过多次尝试,最终视图调整为这样:

  1. <!doctype html>
  2. <html>
  3. <head>
  4. <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'>
  5. <title>标题</title>
  6. <meta name='Keywords' content=''>
  7. <meta name='Description' content=''>
  8. <style type='text/css'>
  9. *{margin:0px; padding:0px; list-style-type:none; text-decoration:none; font-family:"微软雅黑";}
  10. ul{width: 500px; height: 120px; margin: 60px auto;}
  11. /* 分页样式 */
  12. div.listNext{text-align: center; font-size:0;}
  13. div.listNext a{font-size:14px; width: 35px; height:35px; display:inline-block; border:1px solid red; border-right:none; text-align: center; line-height: 35px;}
  14. div.listNext a.next{border-right:1px solid red;}
  15. div.listNext a.text{cursor:text;}
  16. div.listNext a.active{background:#ddd;}
  17. </style>
  18. </head>
  19. <body>
  20. <ul class='list'>
  21. <foreach name='list' item='v' key='i'>
  22. <li>{$i}=={$v.title}</li>
  23. </foreach>
  24. </ul>
  25. <div class='listNext'>{$page}</div>
  26. <script src="https://cdn.bootcss.com/jquery/1.8.3/jquery.min.js"></script>
  27. <script>
  28. function show(id){
  29. var url = "{:U('Lingdao/ajaxpage')}";
  30. $.get(url,{'p':id}, function(data){
  31. $('ul.list').html(data);
  32. });
  33. }
  34. (function(){
  35. var oIndex = 0;
  36. var oAcomSize = $('div.listNext>a.com').size();
  37. $('div.listNext>a.prev').addClass('text');
  38. $('div.listNext>a.com').click(function(){
  39. oIndex = $(this).index();
  40. if(oIndex != 1){$('div.listNext>a.prev').removeClass('text');}else{$('div.listNext>a.prev').addClass('text');};
  41. if(oIndex != oAcomSize){$('div.listNext>a.next').removeClass('text');}else{$('div.listNext>a.next').addClass('text');};
  42. $(this).addClass('active').siblings('a.com').removeClass('active');
  43. });
  44. // prev click
  45. $('div.listNext>a.prev').click(function(){
  46. $('div.listNext>a.com').each(function(i,e){if($(this).hasClass('active')){oIndex = i+1;};});
  47. oIndex = oIndex - 1;
  48. $('div.listNext>a.next').removeClass('text');
  49. if(oIndex == 1){$(this).addClass('text');};
  50. if(oIndex == 0){oIndex = 0; return false;};
  51. $('div.listNext>a').eq(oIndex).addClass('active').siblings('a.com').removeClass('active');
  52. $(this).attr('href','javascript:show('+oIndex+');');
  53. });
  54. // next click
  55. $('div.listNext>a.next').click(function(){
  56. $('div.listNext>a.com').each(function(i,e){if($(this).hasClass('active')){oIndex = i+1;};});
  57. oIndex = oIndex + 1;
  58. if(oIndex == oAcomSize){$(this).addClass('text');};
  59. if(oIndex == oAcomSize+1){oIndex = oAcomSize; return false;};
  60. $('div.listNext>a').eq(oIndex).addClass('active').siblings('a.com').removeClass('active');
  61. $(this).attr('href','javascript:show('+oIndex+');');
  62. });
  63. })();
  64. </script>
  65. </body>
  66. </html>

ThinkPHP分页用异步来做,玩转分页类!的更多相关文章

  1. thinkphp简洁、美观、靠谱的分页类

    我们要实现如下图分页效果 这个分页类是在thinkphp框架内置的分页类的基础上修改而来:原分页类的一些设计,在实际运用中感觉不是很方便: 1.只有一页内容时不显示分页: 2.原分页类在当前页是第一页 ...

  2. thinkphp结合云之讯做短信验证码

    thinkphp结合云之讯做短信验证码先去云之讯注册账号 网址http://www.ucpaas.com/ 注册云之讯平台账号,即可免费获得10元测试费用测试够用啦 解压附件到 ThinkPHP\Li ...

  3. Thinkphp+Ajax带关键词搜索列表无刷新分页实例

    Thinkphp+Ajax带关键词搜索列表无刷新分页实例,两个查询条件,分页和搜索关键字,懂的朋友还可以添加其他分页参数. 搜索#keyword和加载内容区域#ajax_lists <input ...

  4. Thinkphp源码分析系列(二)–引导类

    在上一章我们说到,ThinkPHP.php在设置完框架所需要的变量和调教好环境后,在最后调用了  Think\Think::start();  即Think命名空间中的Think类的静态方法start ...

  5. PHP分页初探 一个最简单的PHP分页代码的简单实现

    PHP分页代码在各种程序开发中都是必须要用到的,在网站开发中更是必选的一项. 要想写出分页代码,首先你要理解SQL查询语句:select * from goods limit 2,7.PHP分页代码核 ...

  6. 分页技巧_抽取出公共的分页用的Service方法

    分页技巧_抽取出公共的分页用的Service方法 TopicAction.java ForumAction.java 放在父类中DaoSupport.java DaoSupportImpl.java ...

  7. 63. Unique Paths II(中等, 能独立做出来的DP类第二个题^^)

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

  8. 重复造轮子,编写一个轻量级的异步写日志的实用工具类(LogAsyncWriter)

    一说到写日志,大家可能推荐一堆的开源日志框架,如:Log4Net.NLog,这些日志框架确实也不错,比较强大也比较灵活,但也正因为又强大又灵活,导致我们使用他们时需要引用一些DLL,同时还要学习各种用 ...

  9. 分页技巧_测试并继续改进分页用的QueryHelper辅助对象

    分页技巧_测试并继续改进分页用的QueryHelper辅助对象 QueryHelper.java /** * 用于辅助拼接HQL语句 */ public class QueryHelper { pri ...

随机推荐

  1. JavaScript入门学习书籍的阶段选择

    对于许多想学习 JavaScript 的朋友来说,无疑如何选择入门的书籍是他们最头疼的问题,或许也是他们一直畏惧,甚至放弃学习 JavaScript 的理由.在 JavaScript 方面,自己不是什 ...

  2. Spring 4 官方文档学习(十一)Web MVC 框架之multipart(文件上传)支持

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-multipart 1.简 ...

  3. e595. Drawing an Image

    See also e575 The Quintessential Drawing Program and e594 Reading an Image or Icon from a File. publ ...

  4. e558. 在Applet中多图片交互显示

    This is the simplest applet to animate an array of images. In practice, you should use double-buffer ...

  5. Zookeeper 应用程序

    Zookeeper为分布式环境提供灵活的协调基础架构.ZooKeeper框架支持许多当今最好的工业应用程序.我们将在本章中讨论ZooKeeper的一些最显着的应用. 雅虎 ZooKeeper框架最初是 ...

  6. 优矿众包对冲基金计划”优选策略---100w实盘资金管理权!!

    https://uqer.io/contest/ http://www.cnblogs.com/dunitian/p/4939369.html 优连

  7. Android Studio怎样查看资源或者函数在哪些类中被引用

    很多人在做完Keymap匹配到Eclispe快捷键后,发现查看资源或者函数在哪些地方被引用的快捷键"Ctrl+Shift+G"不灵 了.你选中某个函数后,使用这个快捷键.发现仅仅会 ...

  8. MVC后台与前台交互的问题。。。

    后台: viewbag.sb/*这是一个sb路径*/=@"\gao\shou"; 前台js: var sb='@viewbag.sb'; alert(sb); 结果就是gaocon ...

  9. window设置TortoiseGit连接git不用每次输入用户名和密码

    1. 在Windows中添加一个HOME环境变量,值为%USERPROFILE%,如下图: 2. 在“开始>运行(快捷键:win+r)”中打开%Home%,然后在目录下新建一个名为“_netrc ...

  10. Digest Authentication 摘要认证

    “摘要”式认证( Digest authentication)是一个简单的认证机制,最初是为HTTP协议开发的,因而也常叫做HTTP摘要,在RFC2671中描述.其身份验证机制很简单,它采用杂凑式(h ...