TP5缓存分页

  1. php控制器判断是否为ajax请求
 
  1. //分页变量

  2. $page=$articles->render();

  3. $this->assign('page',$page);

  4. //文章变量

  5. $this->assign('articles', $articles);

  6. //判断ajax请求,渲染到不同模板

  7. if(request()->isAjax()){

  8. //return $articles;

  9. //如果是ajax请求,则渲染到该页面

  10. return $this->fetch('articleList');

  11. }

  12. else {

  13. //否则到该页面

  14. return $this->fetch('articleIndex');

  15. }

  16.  
  1. 负责ajax请求渲染的模板
 
  1. <!-- START TABLE -->

  2. <div class="simplebox grid740">

  3.  
  4. <div class="titleh">

  5. <h3>博文列表</h3>

  6. </div>

  7.  
  8. <table id="myTable" class="tablesorter">

  9. <thead>

  10. <tr>

  11. <th>#ID</th>

  12. <th>作者</th>

  13. <th>分类</th>

  14. <th>标题</th>

  15. <th>发布日期</th>

  16. <th>评论数量</th>

  17. <th>状态</th>

  18. <th>操作</th>

  19. </tr>

  20. </thead>

  21. <tbody>

  22.  
  23. {volist name='articles' id='article' key="k"}

  24. <tr>

  25. <td>{$k}</td>

  26. <td>作者</td>

  27. <td>{$article.c_name}</td>

  28. <td>{$article.name}</td>

  29. <td>{$article.publishtime|date="y-m-d",###}</td>

  30. <td>{$article.id|count}</td>

  31. <td>{$article.status}</td>

  32. <td>

  33. <form action="\article\{$article.id}\edit" name="edit">

  34. <button>编辑</button>

  35. </form>

  36. <form action="\article\{$article.id}" name="delete" method="post">

  37. <button onclick="return confirm('是否确定删除文章:{$article.c_name}?');">删除</button>

  38. <input type="hidden" name="_method" value="DELETE">

  39. </form>

  40. </td>

  41. </tr>

  42. {/volist}

  43. </tbody>

  44. </table>

  45. {$page}

  46. </div>

  47. <!-- END TABLE -->

  1. 正式访问页面执行ajax请求
 
  1. {literal}

  2. <script>

  3. $(function () {

  4. //给id为list的元素代理绑定下面所有的a元素"click"事件

  5. $("#list").on("click",".pagination a",function() {

  6. //取a标签的href即url发送ajax请求

  7. $.get($(this).attr('href'),function(html){

  8. //返回数据输出到id为list的元素中

  9. $('#list').html(html);

  10. });

  11. //阻止默认事件和冒泡,即禁止跳转

  12. return false;

  13. })

  14. })

  15. </script>

  16. {/literal}

  17. <div id="list">

  18. <!-- START TABLE -->

  19. <div class="simplebox grid740">

  20.  
  21. <div class="titleh">

猜你喜欢

转载自blog.csdn.net/heyuqing32/article/details/81411312
今日推荐