Modify the Page class of thinkphp to support ajax.

Source: Internet
Author: User
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

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.