Provides various official and user-released code examples and code reference. You are welcome to exchange ideas and learn how to transform thinkphp's Page class to support ajax.
Effect:
Note: thinkphp version: 3.2.1
Steps:
1. Open Page. class. php (Path: ThinkPHP/Library/Think)
Find row 48th:
empty($_GET[$this->p]) ? 1 : intval($_GET[$this->p]);
Changed:
empty(I($this->p)) ? 1 : intval(I($this->p));
Actually, the built-in I function is used to read the passed P parameter, which does not affect the original function.
2. ajax call:
Front-end html page
The page content is displayed here.
Javascript:
Function jzry (p ){
Var request = $. ajax ({
Url: "_ MODULE _/Home/jzrh", // load paging data with ajax
Type: "POST ",
Async: false,
Data: {p: p },
BeforeSend: function (xhr ){
Upload ('{message'{.html ("loading data. Please wait. ");
}
});
Request. done (function (msg ){
If (msg. bj = 'OK '){
Response ('{rs'}.html (msg. page); // display the page
$ ("# Rs a"). removeAttr ("href"); // Delete the original connection address,
// Use the ajax button event
$ (". Num,. end,. first"). on ("click", function (){
Jzry ($ (this). text ());
});
$ (". Next"). on ("click", function (){
Jzry (parseInt ($ (". current"). text () + 1 );
});
$ (". Prev"). on ("click", function (){
Jzry (parseInt ($ (". current"). text ()-1 );
});
}});
Request. fail (function (jqXHR, textStatus ){
Certificate ('{message'{.html ("error:" + textStatus );
});
}
Style:
3. The background is used the same as the original one.
$ User = M ("user ");
$ User = $ user-> page ($ _ POST ['P']. ', 20')-> order ('id')-> select ();
If ($ User> 0 ){
$ Val ['bj '] =' OK ';
$ Count = $ user-> count ();
$ Page = new \ Think \ Page ($ count, 20 );
$ Val ['page'] = $ page-> show ();
$ This-> ajaxReturn ($ val); // ajax return
}
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB