登录  /  注册
首页 > php框架 > ThinkPHP > 正文

手把手教你实现thinkphp ajax无刷新分页

藏色散人
发布: 2021-09-20 16:59:46
转载
1508人浏览过

下面thinkphp/" target="_blank">thinkphp框架教程栏目将给大家介绍怎么实现thinkphp的ajax无刷新分页,希望对需要的朋友有所帮助!

前言

thinkphp框架自带的分页类是每次翻页都要刷新一下整个页面,这种翻页的用户体验显然是不太理想的,我们希望每次翻页只刷新我们想要的数据集部分的数据,这样我们很容易想到ajax异步通信,用ajax与数据库(本人在开发过程中使用的是mysql数据库)异步交互,将从数据库查询的数据返回,用jquery替换原有的数据,从而在不刷新这个页面的情况下进行局部刷新,从而达到我们预期的效果。

thinkphp ajax 分页类

这个分页类是网上找到的资源,大家可以直接在自己的thinkphp里创建这么一个类,我这里类名是 AjaxPage.class.php

<?php // +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id: Page.class.php 2712 2012-02-06 10:12:49Z liu21st $
namespace Common\Common;
class AjaxPage {
    // 分页栏每页显示的页数
    public $rollPage = 5;
    // 页数跳转时要带的参数
    public $parameter  ;
    // 默认列表每页显示行数
    public $listRows = 20;
    // 起始行数
    public $firstRow ;
    // 分页总页面数
    protected $totalPages  ;
    // 总行数
    protected $totalRows  ;
    // 当前页数
    protected $nowPage    ;
    // 分页的栏的总页数
    protected $coolPages   ;
    // 分页显示定制
    protected $config  = array('header'=&gt;'条记录','prev'=&gt;'上一页','next'=&gt;'下一页','first'=&gt;'第一页','last'=&gt;'最后一页','theme'=&gt;' %totalRow% %header% %nowPage%/%totalPage% 页 %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%');
    // 默认分页变量名
    protected $varPage;


    public function __construct($totalRows,$listRows='',$ajax_func,$parameter='') {
        $this-&gt;totalRows = $totalRows;
        $this-&gt;ajax_func = $ajax_func;
        $this-&gt;parameter = $parameter;
        $this-&gt;varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this-&gt;listRows = intval($listRows);
        }
        $this-&gt;totalPages = ceil($this-&gt;totalRows/$this-&gt;listRows);     //总页数
        $this-&gt;coolPages  = ceil($this-&gt;totalPages/$this-&gt;rollPage);
        $this-&gt;nowPage  = !empty($_GET[$this-&gt;varPage])?intval($_GET[$this-&gt;varPage]):1;
        if(!empty($this-&gt;totalPages) &amp;&amp; $this-&gt;nowPage&gt;$this-&gt;totalPages) {
            $this-&gt;nowPage = $this-&gt;totalPages;
        }
        $this-&gt;firstRow = $this-&gt;listRows*($this-&gt;nowPage-1);
    }

     public function nowpage($totalRows,$listRows='',$ajax_func,$parameter='') {
        $this-&gt;totalRows = $totalRows;
        $this-&gt;ajax_func = $ajax_func;
        $this-&gt;parameter = $parameter;
        $this-&gt;varPage = C('VAR_PAGE') ? C('VAR_PAGE') : 'p' ;
        if(!empty($listRows)) {
            $this-&gt;listRows = intval($listRows);
        }
        $this-&gt;totalPages = ceil($this-&gt;totalRows/$this-&gt;listRows);     //总页数
        $this-&gt;coolPages  = ceil($this-&gt;totalPages/$this-&gt;rollPage);
        $this-&gt;nowPage  = !empty($_GET[$this-&gt;varPage])?intval($_GET[$this-&gt;varPage]):1;
        if(!empty($this-&gt;totalPages) &amp;&amp; $this-&gt;nowPage&gt;$this-&gt;totalPages) {
            $this-&gt;nowPage = $this-&gt;totalPages;
        }
        $this-&gt;firstRow = $this-&gt;listRows*($this-&gt;nowPage-1);

        return $this-&gt;nowPage;
    }

    public function setConfig($name,$value) {
        if(isset($this-&gt;config[$name])) {
            $this-&gt;config[$name]    =   $value;
        }
    }


    public function show() {
        if(0 == $this-&gt;totalRows) return '';
        $p = $this-&gt;varPage;
        $nowCoolPage      = ceil($this-&gt;nowPage/$this-&gt;rollPage);
        $url  =  $_SERVER['REQUEST_URI'].(strpos($_SERVER['REQUEST_URI'],'?')?'':"?").$this-&gt;parameter;
        $parse = parse_url($url);
        if(isset($parse['query'])) {
            parse_str($parse['query'],$params);
            unset($params[$p]);
            $url   =  $parse['path'].'?'.http_build_query($params);
        }
        //上下翻页字符串
        $upRow   = $this-&gt;nowPage-1;
        $downRow = $this-&gt;nowPage+1;
        if ($upRow&gt;0){
            $upPage="<a>ajax_func."(".$upRow.")'&gt;".$this-&gt;config['prev']."</a>";
        }else{
            $upPage="";
        }

        if ($downRow totalPages){
            $downPage="<a>ajax_func."(".$downRow.")'&gt;".$this-&gt;config['next']."</a>";
        }else{
            $downPage="";
        }
        //  &gt;&gt;
        if($nowCoolPage == 1){
            $theFirst = "";
            $prePage = "";
        }else{
            $preRow =  $this-&gt;nowPage-$this-&gt;rollPage;
            $prePage = "<a>ajax_func."(".$preRow.")'&gt;上".$this-&gt;rollPage."页</a>";
            $theFirst = "<a>ajax_func."(1)' &gt;".$this-&gt;config['first']."</a>";
        }
        if($nowCoolPage == $this-&gt;coolPages){
            $nextPage = "";
            $theEnd="";
        }else{
            $nextRow = $this-&gt;nowPage+$this-&gt;rollPage;
            $theEndRow = $this-&gt;totalPages;
            $nextPage = "<a>ajax_func."(".$nextRow.")' &gt;下".$this-&gt;rollPage."页</a>";
            $theEnd = "<a>ajax_func."(".$theEndRow.")' &gt;".$this-&gt;config['last']."</a>";
        }
        // 1 2 3 4 5
        $linkPage = "";
        for($i=1;$irollPage;$i++){
            $page=($nowCoolPage-1)*$this-&gt;rollPage+$i;
            if($page!=$this-&gt;nowPage){
                if($pagetotalPages){
                   $linkPage .= " <a>ajax_func."(".$page.")'&gt; ".$page." </a>";
                }else{
                    break;
                }
            }else{
                if($this-&gt;totalPages != 1){
                    $linkPage .= " <span>".$page."</span>";
                }
            }
        }
        $pageStr  =  str_replace(
            array('%header%','%nowPage%','%totalRow%','%totalPage%','%upPage%','%downPage%','%first%','%prePage%','%linkPage%','%nextPage%','%end%'),
            array($this-&gt;config['header'],$this-&gt;nowPage,$this-&gt;totalRows,$this-&gt;totalPages,$upPage,$downPage,$theFirst,$prePage,$linkPage,$nextPage,$theEnd),$this-&gt;config['theme']);
        return $pageStr;
    }

}

?&gt;
登录后复制

具体步骤

接下来,我们从控制器开始一步一步地实现thinkphp无刷新分页这个效果。

1.控制器部分

这只是控制器的一部分比较核心的代码。

        //实例化数据模型
        $info=M('info');
        //统计要查询数据的数量
        $count=$info-&gt;where("ID='$id'")-&gt;count();
        //实例化分页类,传入三个参数,分别是数据总数、每页显示的数据条数、要调用的jQuery ajax方法名
        $p=new \Host\Common\AjaxPage($count,10,'server');
        //产生分页信息
        $page=$p-&gt;show();
        //要查询的数据,limit表示每页查询的数量,这里为10条
        $data = $server_info-&gt;where("ID='$id'")-&gt;limit($p-&gt;firstRow.','.$p-&gt;listRows)-&gt;select();
        //assign方法往模板赋值
        $this-&gt;assign('list',$data);
        $this-&gt;assign('page',$page);
        //ajax返回信息
        $res["content"] = $this-&gt;fetch('Index/myinfolist')
        $this-&gt;ajaxReturn($res);
登录后复制

2.模板部分

模板名:myinfolist.html与上面控制器中渲染的模板一致。

 $res["content"] = $this-&gt;fetch('Index/myinfolist')
登录后复制

因为前端用的bootstrap框架,所以这个模板里的好多class是bootstrap里的,大家也不必过分纠结这个,看整个过程的重点就好。

nbsp;html&gt;


    <p>
        </p><p>
            </p><p>
                </p><p>
                    </p><p>
                        </p><p><i></i>信息列表</p>
                    

                    <p>
                
                        </p>
登录后复制
                                                                                                                                                                                                                                                                                                                                                 //循环赋值                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   
abcd
{$info.a}{$info.b}{$info.c}{$info.d}
                        //分页信息                         

 {$page} 

                                                                                                               <script></script> 

以上就是手把手教你实现thinkphp ajax无刷新分页的详细内容,更多请关注php中文网其它相关文章!

智能AI问答
PHP中文网智能助手能迅速回答你的编程问题,提供实时的代码和解决方案,帮助你解决各种难题。不仅如此,它还能提供编程资源和学习指导,帮助你快速提升编程技能。无论你是初学者还是专业人士,AI智能助手都能成为你的可靠助手,助力你在编程领域取得更大的成就。
来源:segmentfault网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
最新问题
关于CSS思维导图的课件在哪? 课件
凡人来自于2024-04-16 10:10:18
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号