成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

一步步實(shí)現(xiàn)thinkphp上的ajax無(wú)刷新分頁(yè)

張遷 / 2498人閱讀

摘要:信息列表循環(huán)賦值分頁(yè)信息部分這一步是實(shí)現(xiàn)無(wú)刷新分頁(yè)的重點(diǎn),用到了的通信,通過(guò)與數(shù)據(jù)庫(kù)的交互,將獲取到的數(shù)據(jù)寫(xiě)到模板中,替換掉之前的數(shù)據(jù)集,達(dá)到分頁(yè)的目的。

前言

thinkphp框架自帶的分頁(yè)類是每次翻頁(yè)都要刷新一下整個(gè)頁(yè)面,這種翻頁(yè)的用戶體驗(yàn)顯然是不太理想的,我們希望每次翻頁(yè)只刷新我們想要的數(shù)據(jù)集部分的數(shù)據(jù),這樣我們很容易想到ajax異步通信,用ajax與數(shù)據(jù)庫(kù)(本人在開(kāi)發(fā)過(guò)程中使用的是mysql數(shù)據(jù)庫(kù))異步交互,將從數(shù)據(jù)庫(kù)查詢的數(shù)據(jù)返回,用jquery替換原有的數(shù)據(jù),從而在不刷新這個(gè)頁(yè)面的情況下進(jìn)行局部刷新,從而達(dá)到我們預(yù)期的效果。

thinkphp ajax 分頁(yè)類

這個(gè)分頁(yè)類是網(wǎng)上找到的資源,大家可以直接在自己的thinkphp里創(chuàng)建這么一個(gè)類,我這里類名是 AjaxPage.class.php


// +----------------------------------------------------------------------
// $Id: Page.class.php 2712 2012-02-06 10:12:49Z liu21st $
namespace CommonCommon;
class AjaxPage {
    // 分頁(yè)欄每頁(yè)顯示的頁(yè)數(shù)
    public $rollPage = 5;
    // 頁(yè)數(shù)跳轉(zhuǎn)時(shí)要帶的參數(shù)
    public $parameter  ;
    // 默認(rèn)列表每頁(yè)顯示行數(shù)
    public $listRows = 20;
    // 起始行數(shù)
    public $firstRow ;
    // 分頁(yè)總頁(yè)面數(shù)
    protected $totalPages  ;
    // 總行數(shù)
    protected $totalRows  ;
    // 當(dāng)前頁(yè)數(shù)
    protected $nowPage    ;
    // 分頁(yè)的欄的總頁(yè)數(shù)
    protected $coolPages   ;
    // 分頁(yè)顯示定制
    protected $config  = array("header"=>"條記錄","prev"=>"上一頁(yè)","next"=>"下一頁(yè)","first"=>"第一頁(yè)","last"=>"最后一頁(yè)","theme"=>" %totalRow% %header% %nowPage%/%totalPage% 頁(yè) %upPage% %downPage% %first%  %prePage%  %linkPage%  %nextPage% %end%");
    // 默認(rèn)分頁(yè)變量名
    protected $varPage;


    public function __construct($totalRows,$listRows="",$ajax_func,$parameter="") {
        $this->totalRows = $totalRows;
        $this->ajax_func = $ajax_func;
        $this->parameter = $parameter;
        $this->varPage = C("VAR_PAGE") ? C("VAR_PAGE") : "p" ;
        if(!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //總頁(yè)數(shù)
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);
    }

     public function nowpage($totalRows,$listRows="",$ajax_func,$parameter="") {
        $this->totalRows = $totalRows;
        $this->ajax_func = $ajax_func;
        $this->parameter = $parameter;
        $this->varPage = C("VAR_PAGE") ? C("VAR_PAGE") : "p" ;
        if(!empty($listRows)) {
            $this->listRows = intval($listRows);
        }
        $this->totalPages = ceil($this->totalRows/$this->listRows);     //總頁(yè)數(shù)
        $this->coolPages  = ceil($this->totalPages/$this->rollPage);
        $this->nowPage  = !empty($_GET[$this->varPage])?intval($_GET[$this->varPage]):1;
        if(!empty($this->totalPages) && $this->nowPage>$this->totalPages) {
            $this->nowPage = $this->totalPages;
        }
        $this->firstRow = $this->listRows*($this->nowPage-1);

        return $this->nowPage;
    }

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


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

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

}

?>
具體步驟

接下來(lái),我們從控制器開(kāi)始一步一步地實(shí)現(xiàn)thinkphp無(wú)刷新分頁(yè)這個(gè)效果。

1.控制器部分

這只是控制器的一部分比較核心的代碼。

        //實(shí)例化數(shù)據(jù)模型
        $info=M("info");
        //統(tǒng)計(jì)要查詢數(shù)據(jù)的數(shù)量
        $count=$info->where("ID="$id"")->count();
        //實(shí)例化分頁(yè)類,傳入三個(gè)參數(shù),分別是數(shù)據(jù)總數(shù)、每頁(yè)顯示的數(shù)據(jù)條數(shù)、要調(diào)用的jQuery ajax方法名
        $p=new HostCommonAjaxPage($count,10,"server");
        //產(chǎn)生分頁(yè)信息
        $page=$p->show();
        //要查詢的數(shù)據(jù),limit表示每頁(yè)查詢的數(shù)量,這里為10條
        $data = $server_info->where("ID="$id"")->limit($p->firstRow.",".$p->listRows)->select();
        //assign方法往模板賦值
        $this->assign("list",$data);
        $this->assign("page",$page);
        //ajax返回信息
        $res["content"] = $this->fetch("Index/myinfolist")
        $this->ajaxReturn($res);
2.模板部分

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

 $res["content"] = $this->fetch("Index/myinfolist")

因?yàn)榍岸擞玫腷ootstrap框架,所以這個(gè)模板里的好多class是bootstrap里的,大家也不必過(guò)分糾結(jié)這個(gè),看整個(gè)過(guò)程的重點(diǎn)就好。




    
信息列表
//循環(huán)賦值
a b c d
{$info.a} {$info.b} {$info.c} {$info.d}
//分頁(yè)信息
{$page}
3.js部分

這一步是實(shí)現(xiàn)ajax無(wú)刷新分頁(yè)的重點(diǎn),用到了jQuery的ajax通信,通過(guò)與數(shù)據(jù)庫(kù)的ajax交互,將獲取到的數(shù)據(jù)寫(xiě)到模板中,替換掉之前的數(shù)據(jù)集,達(dá)到分頁(yè)的目的。
server.js

function server(id){  
         var id = id;
            $.get("/Server/myinfo", {"p":id}, function(data){  
            //用get方法發(fā)送信息到Server中的myinfo方法
             $("#server").replaceWith("
" +data.content+ "
"); }); }

這個(gè)方法名 server 就是控制器中實(shí)例化分頁(yè)類中傳的第三個(gè)參數(shù),每次在模板上點(diǎn)擊翻頁(yè),都會(huì)觸發(fā)這個(gè)js方法server(p),里面?zhèn)鬟f的是第幾頁(yè)的頁(yè)碼。

$p=new HostCommonAjaxPage($count,10,"server");

這里用到的是jQuery里ajax方法的.get形式進(jìn)行ajax與后臺(tái)通信,拿到返回的數(shù)據(jù)用replaceWith方法,用

+數(shù)據(jù)

替換模板中id為server的div,實(shí)現(xiàn)分頁(yè)效果。

小結(jié)

上面的步驟就是實(shí)現(xiàn)ajax分頁(yè)的具體步驟,jQuery的ajax方法有$.ajax 、$.get 、 $.post三種,這里就不做介紹,代碼粘自我的程序中,為了不公開(kāi)我的一些數(shù)據(jù)字段,就用abcd代替了,也做了一定的刪減,有問(wèn)題希望大家指正,多多交流。

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/21784.html

相關(guān)文章

  • jQuery+Ajax+PHP無(wú)刷新分頁(yè)

    摘要:下載演示地址本文使用,通過(guò)實(shí)例講解如何實(shí)現(xiàn)無(wú)刷新分頁(yè)效果。當(dāng)數(shù)據(jù)完全加載完畢后,調(diào)用函數(shù)生成分頁(yè),也可用程序來(lái)實(shí)現(xiàn)分頁(yè)。頁(yè)面可在分頁(yè)的屬性中獲取。 下載演示地址:http://www.erdangjiade.com/js...本文使用jQuery+Ajax+PHP+Mysql,通過(guò)實(shí)例講解如何實(shí)現(xiàn)Ajax無(wú)刷新分頁(yè)效果。 #ul_lists以列表的形式展現(xiàn)數(shù)據(jù),信...

    wangzy2019 評(píng)論0 收藏0
  • jQuery+Ajax+PHP無(wú)刷新分頁(yè)

    摘要:下載演示地址本文使用,通過(guò)實(shí)例講解如何實(shí)現(xiàn)無(wú)刷新分頁(yè)效果。當(dāng)數(shù)據(jù)完全加載完畢后,調(diào)用函數(shù)生成分頁(yè),也可用程序來(lái)實(shí)現(xiàn)分頁(yè)。頁(yè)面可在分頁(yè)的屬性中獲取。 下載演示地址:http://www.erdangjiade.com/js...本文使用jQuery+Ajax+PHP+Mysql,通過(guò)實(shí)例講解如何實(shí)現(xiàn)Ajax無(wú)刷新分頁(yè)效果。 #ul_lists以列表的形式展現(xiàn)數(shù)據(jù),信...

    Kosmos 評(píng)論0 收藏0
  • jQuery+Ajax+PHP無(wú)刷新分頁(yè)

    摘要:下載演示地址本文使用,通過(guò)實(shí)例講解如何實(shí)現(xiàn)無(wú)刷新分頁(yè)效果。當(dāng)數(shù)據(jù)完全加載完畢后,調(diào)用函數(shù)生成分頁(yè),也可用程序來(lái)實(shí)現(xiàn)分頁(yè)。頁(yè)面可在分頁(yè)的屬性中獲取。 下載演示地址:http://www.erdangjiade.com/js...本文使用jQuery+Ajax+PHP+Mysql,通過(guò)實(shí)例講解如何實(shí)現(xiàn)Ajax無(wú)刷新分頁(yè)效果。 #ul_lists以列表的形式展現(xiàn)數(shù)據(jù),信...

    lauren_liuling 評(píng)論0 收藏0
  • jQuery+Ajax+PHP無(wú)刷新分頁(yè)

    摘要:下載演示地址本文使用,通過(guò)實(shí)例講解如何實(shí)現(xiàn)無(wú)刷新分頁(yè)效果。當(dāng)數(shù)據(jù)完全加載完畢后,調(diào)用函數(shù)生成分頁(yè),也可用程序來(lái)實(shí)現(xiàn)分頁(yè)。頁(yè)面可在分頁(yè)的屬性中獲取。 下載演示地址:http://www.erdangjiade.com/js...本文使用jQuery+Ajax+PHP+Mysql,通過(guò)實(shí)例講解如何實(shí)現(xiàn)Ajax無(wú)刷新分頁(yè)效果。 #ul_lists以列表的形式展現(xiàn)數(shù)據(jù),信...

    mengbo 評(píng)論0 收藏0
  • ThinkPHP5踩過(guò)的坑

    摘要:函數(shù)在框架初始化方法中無(wú)效當(dāng)初做權(quán)限控制在判斷是否有權(quán)限如果無(wú)權(quán)限就執(zhí)行跳轉(zhuǎn)跳轉(zhuǎn)的函數(shù)是但是無(wú)論如何都無(wú)法跳轉(zhuǎn)出去當(dāng)時(shí)的版本是手冊(cè)還不太完善后來(lái)得知必須得才可以跳轉(zhuǎn)重定向完整代碼分頁(yè)參數(shù)的問(wèn)題如果是普通分頁(yè)沒(méi)毛病但是分頁(yè)后面有其他參數(shù)比 01:redirect函數(shù)在框架初始化方法中無(wú)效當(dāng)初做權(quán)限控制,在initialize判斷是否有權(quán)限,如果無(wú)權(quán)限就執(zhí)行跳轉(zhuǎn),跳轉(zhuǎn)的函數(shù)是redirec...

    enali 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<