Javascript-how to implement the refreshing paging effect of ajax and js

Source: Internet
Author: User
Implement the paging effect of ajax data requests: similar to the image! Code implementation

Implement the paging effect of ajax data requests: similar to the image! Code implementation

Reply content:

Implement the paging effect of ajax data requests: similar to the image! Code implementation

Ajax requests data on a page, clears the table, and inserts new data into the table.

1.css makes paging styles;
2. listen to the event, pass the pageNo parameter, and re-assemble and render the list in the ajax callback.

The idea is to make an ajax request when triggering a paging event, and update the table after the request is successful:

Ele. onclick = function () {// assume that ele is the element that triggers pagination var index = ele. pageIndex; var xhr = new XMLHttpRequest (); xhr. open ('GET', 'URL', true); // The URL should contain paging information, such as pageIndex and pageSize xhr. onreadystatechange = function () {if (xhr. status = 200 & xhr. readyState = 4) {updateTable (xhr. responseText); // This updateTable is the method you use to update the table. it is executed on this page and will not be refreshed} xhr. send ('');}

When you click the page number:

  • Frontend value: currentPage (page of the current request) and perPage (page of data );

  • Backend return values: pages (total pages) and data (returned data );

  • When the frontend requests data for the first time, it displays the front-end page flip bar based on the total number of pages returned by the backend. if data binding is not used, such as react, vue, and knockout, in the next request, you can clear and recycle the cousin and insert it into html. if data binding is used, you only need to reset the returned data to re-render the page.

I think the subject should have four points of knowledge:

  1. Ajax

  2. Php paging technology

  3. Mysql limit

  4. Jquery

Details:

  1. ajaxTriggerclick(jQuery) Event, and then get the value of this page number and send it to the backend

  2. Php receives the page number parameter, which is calculated based on the current page number and number of displayed items.

$ Page = $ _ GET ['Page']; // Current page number $ page_num = 12; // number of displayed items $ offect = $ page_num * ($ page-1 ); $ SQL = "select * from table limit $ offset, $ page_num"; // 3. SQL limit syntax // assembled into html and returned to the front-end echo $ html; exit;

4. replace the p corresponding to the content in the front-end list.

This is an old method. I don't know what the latest method is.

Above. By the way, Baidu puts resources into this issue.Really, don't lie to you.

For ajax usage, see api
As for refreshing new pages, you only need to clear and replace the data in the table area. The subject can look at jquery and basically implement it. it is very simple...

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.