Using AJAX to implement no-refresh paging in thinkphp

Source: Internet
Author: User

Create a new AjaxPage.class.php in the lib\org\util\ directory of the thinkphp directory and write the contents:

<?php//+----------------------------------------------------------------------//| thinkphp [WE CAN do it JUST THINK it]//+----------------------------------------------------------------------//| Copyright (c) http://thinkphp.cn All rights reserved.//+------------------------------------------------------- ---------------// | Licensed (http://www.apache.org/licenses/LICENSE-2.0)//+------------------------------------------------------- ---------------// | author:liu21st <[email protected]>//+--------------------------------------------------------------- -------//$Id: Page.class.php 2712 2012-02-06 10:12:49z liu21st $class ajaxpage {//page bar display of the number of pages public $rollPage = 5    ;    The number of pages to be taken when the public parameter public $parameter;    Default list shows the number of rows per page public $listRows = 20;    Starting line number public $firstRow;    Total page count protected $totalPages;    Total number of protected $totalRows;    Current page protected $nowPage;    The total number of pages in the page protected $coolPages; Pagination Display FixedSystem protected $config = Array (' header ' = ' record ', ' prev ' = ' prev ', ' next ' = ' next ', ' first ' = ' ' page ', ' last ' = ') ' Last page ', ' theme ' = '%totalrow%%header%%nowpage%/%totalpage% page%uppage%%downpage%%first%%prepage%%linkpage%%nextP    Age%%end% ');    Default paging variable name 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);        Total pages $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 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);        }//Page up and down string $upRow = $this->nowpage-1;        $downRow = $this->nowpage+1; if ($upRow >0) {$upPage = "<a id= ' big ' href= ' javascript:". $this->ajax_func. " (". $upRoW. ") ' > ". $this->config[' prev ')."        </a> ";        }else{$upPage = ""; }if ($downRow <= $this->totalpages) {$downPage = "<a id= ' big ' href= ' javascript:". $this->ajax_func. " (". $downRow.") ' > ". $this->config[' next ')."        </a> ";        }else{$downPage = "";            }//<< < > >> if ($nowCoolPage = = 1) {$theFirst = "";        $prePage = "";            }else{$preRow = $this->nowpage-$this->rollpage; $prePage = "<a id= ' big ' href= ' javascript:". $this->ajax_func. " (". $preRow.") ' > ". $this->rollpage."            Page </a> "; $theFirst = "<a id= ' big ' href= ' javascript:". $this->ajax_func. " (1) ' > '. $this->config[' first '. "        </a> ";            } if ($nowCoolPage = = $this->coolpages) {$nextPage = "";        $theEnd = "";            }else{$nextRow = $this->nowpage+ $this->rollpage; $theEndRow = $this->totalPages; $nextPage = "<a id= ' big ' href= ' javascript:". $this->ajax_func. " (". $nextRow.") ' > under ". $this->rollpage."            Page </a> "; $theEnd = "<a id= ' big ' href= ' javascript:". $this->ajax_func. " (". $theEndRow.") ' > ". $this->config[' last ']."        </a> ";        }//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. = " ; <a id= ' big ' href= ' javascript: ". $this->ajax_func." (". $page.") ' >&nbsp; ". $page. "                &nbsp;</a> ";                }else{break; }}else{if ($this->totalpages! = 1) {$linkPage. = "&nbsp;<span clas s= ' current ' > ". $page."                </span> "; }}} $pageStr = Str_replace (arraY ('%header% ', '%nowpage% ', '%totalrow% ', '%totalpage% ', '%uppage% ', '%downpage% ', '%first% ', '%prepage% ', '%linkpage% ' , '%nextpage% ', '%end% '), Array ($this->config[' header '), $this->nowpage, $this->totalrows, $this->totalp        Ages, $upPage, $downPage, $theFirst, $prePage, $linkPage, $nextPage, $theEnd), $this->config[' theme ');    return $pageStr; }}?>

The controller writes the following:

<?php
Class Useraction extends action{
Public Function User () {
Import ("ORG. Util.ajaxpage ")//Import paging class Note the import is the Ajaxpage class that you write
$credit = M (' user ');
$count = $credit->count (); Calculate the number of records
$limitRows = 5; Set the number of records per page

$p = new Ajaxpage ($count, $limitRows, "user"); The third parameter is the name of the AJAX function that you need to invoke to change the page
$limit _value = $p->firstrow. "," . $p->listrows;

$data = $credit->order (' id desc ')->limit ($limit _value)->select (); Querying data
$page = $p->show (); Generate paging information, where AJAX connections are generated

$this->assign (' list ', $data);
$this->assign (' page ', $page);
$this->display ();

}
}

?>

The template file is as follows:

<title>ajax No Refresh page </title>
<script type= "Text/javascript" src= ". /public/jquery-1.7.2.min.js "></script>
<script type= "Text/javascript" >
Functions User (ID) {//user function name must be the same as the third parameter in the action above
var id = ID;
$.get (' User/user ', {' P ': id}, function (data) {//) send information to the User method in Useraction using the Get method
$ ("#user"). ReplaceWith ("<div id= ' user ' >" +data+ "</div>"); User must be consistent with the TPL
});
}

</script>

<body>
<div id= ' user ' > <!--here the user is the same as the test in JS below--
<volist id= ' list ' name= ' list ' > <!--content Output--
<{$list. id}>&nbsp;&nbsp;<{$list .username}><br/>
</volist>
<{$page}> <!--paging output--
</div>

</body>

Using AJAX to implement no-refresh paging in thinkphp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.