With the continuous development of Web technology, more and more websites need to support the function of paging data query. jQuery is a very popular JavaScript library that can help developers more conveniently operate DOM, events, animations, etc., so using jQuery to implement paging query data is a good choice.
This article will introduce the basic principles, steps and code implementation methods of using jQuery to implement paging query data, and provide a simple example for readers' reference.
1. Basic Principle
The basic principle of using jQuery to implement paging query data is to send an asynchronous request to the background through AJAX technology, obtain the data that needs to be displayed, and display it on the page. During the implementation process, the following technologies need to be used:
- AJAX technology: Use jQuery's AJAX method to send asynchronous requests to the background to obtain the data that needs to be displayed.
- Paging algorithm: Calculate the data to be displayed in the background, and then determine the starting position and quantity of the data to be displayed based on the current page number and the number of records displayed on each page.
- HTML, CSS and JavaScript: Use jQuery to operate DOM elements, dynamically generate paging controls, and implement paging functions.
2. Steps
The following are the basic steps to use jQuery to implement paging query data:
- Define a DIV element to display the data that needs to be queried data and set an ID for the element.
- Define a JavaScript function to send an asynchronous request to the background and display the obtained data in the specified DIV element.
- Define a JavaScript function to generate a paging control and set event listeners for each button of the paging control.
- After the page is loaded, call the above two functions to display the data and paging controls of the first page.
- When the user clicks the button on the paging control, the first function is called to obtain the data of the specified page number and display it in the specified DIV element.
3. Code Implementation
The following is a simple example code that uses jQuery to implement paging query data:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>分頁(yè)查詢數(shù)據(jù)示例</title> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="paging.js"></script> <link rel="stylesheet" href="paging.css"> </head> <body> <div id="data"></div> <div id="paging"></div> <script> $(document).ready(function() { // 顯示第一頁(yè)數(shù)據(jù)和分頁(yè)控件 getDataWithPage(1); generatePaging(1); // 為分頁(yè)控件上的按鈕添加事件監(jiān)聽(tīng)器 $('#paging').on('click', '.page-btn', function() { var page = parseInt($(this).data('page')); getDataWithPage(page); generatePaging(page); }); }); </script> </body> </html>
/* * 分頁(yè)查詢數(shù)據(jù)相關(guān)的 JavaScript 函數(shù) */ var PAGE_SIZE = 10; // 每頁(yè)顯示的記錄數(shù) var TOTAL_PAGES = 20; // 總頁(yè)數(shù)(假設(shè)為 20) // 向后臺(tái)發(fā)送異步請(qǐng)求,獲取指定頁(yè)碼的數(shù)據(jù),并將其顯示在指定的 DIV 元素中 function getDataWithPage(page) { var startIndex = (page - 1) * PAGE_SIZE + 1; var endIndex = startIndex + PAGE_SIZE - 1; $.ajax({ url: 'data.php', // 后臺(tái)數(shù)據(jù)接口 URL method: 'GET', data: { startIndex: startIndex, endIndex: endIndex }, success: function(data) { // 將獲取到的數(shù)據(jù)顯示在指定的 DIV 元素中 $('#data').html(data); }, error: function() { alert('獲取數(shù)據(jù)失敗'); } }); } // 生成分頁(yè)控件,并為分頁(yè)控件的每個(gè)按鈕設(shè)置事件監(jiān)聽(tīng)器 function generatePaging(currentPage) { var pagingHTML = '<ul>'; if (currentPage == 1) { pagingHTML += '<li><span class="disabled">上一頁(yè)</span></li>'; } else { pagingHTML += '<li><a href="javascript:void(0);" class="page-btn" data-page="' + (currentPage - 1) + '">上一頁(yè)</a></li>'; } for (var i = 1; i <= TOTAL_PAGES; i++) { if (i === currentPage) { pagingHTML += '<li><span class="current">' + i + '</span></li>'; } else { pagingHTML += '<li><a href="javascript:void(0);" class="page-btn" data-page="' + i + '">' + i + '</a></li>'; } } if (currentPage == TOTAL_PAGES) { pagingHTML += '<li><span class="disabled">下一頁(yè)</span></li>'; } else { pagingHTML += '<li><a href="javascript:void(0);" class="page-btn" data-page="' + (currentPage + 1) + '">下一頁(yè)</a></li>'; } pagingHTML += '</ul>'; $('#paging').html(pagingHTML); }
/* * 分頁(yè)控件相關(guān)的 CSS 樣式 */ #paging ul { margin: 0; padding: 0; list-style-type: none; } #paging ul li { display: inline-block; margin: 0 5px; padding: 0; } #paging ul li span { display: inline-block; padding: 5px 15px; border: 1px solid #ddd; background-color: #fff; color: #333; cursor: default; } #paging ul li a { display: inline-block; padding: 5px 15px; border: 1px solid #ddd; background-color: #fff; color: #333; text-decoration: none; } #paging ul li a:hover { background-color: #f5f5f5; } #paging ul li .current { display: inline-block; padding: 5px 15px; border: 1px solid #ddd; background-color: #f5f5f5; color: #333; cursor: default; } #paging ul li .disabled { display: inline-block; padding: 5px 15px; border: 1px solid #ddd; background-color: #fff; color: #bbb; cursor: default; }
It should be noted that the above code is a A simple example, which needs to be adjusted and optimized according to actual conditions in actual applications. At the same time, it is also necessary to ensure the normal operation and data security of the background data interface to avoid SQL injection and other attacks.
The above is the detailed content of Using jquery to implement paging query data. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

ToimplementdarkmodeinCSSeffectively,useCSSvariablesforthemecolors,detectsystempreferenceswithprefers-color-scheme,addamanualtogglebutton,andhandleimagesandbackgroundsthoughtfully.1.DefineCSSvariablesforlightanddarkthemestomanagecolorsefficiently.2.Us

The topic differencebetweenem, Rem, PX, andViewportunits (VH, VW) LiesintheirreFerencepoint: PXISFixedandbasedonpixelvalues, emissrelative EtothefontsizeFheelementoritsparent, Remisrelelatotherootfontsize, AndVH/VwarebaseDontheviewporttimensions.1.PXoffersprecis

Choosing the correct display value in CSS is crucial because it controls the behavior of elements in the layout. 1.inline: Make elements flow like text, without occupying a single line, and cannot directly set width and height, suitable for elements in text, such as; 2.block: Make elements exclusively occupy one line and occupy all width, can set width and height and inner and outer margins, suitable for structured elements, such as; 3.inline-block: has both block characteristics and inline layout, can set size but still display in the same line, suitable for horizontal layouts that require consistent spacing; 4.flex: Modern layout mode, suitable for containers, easy to achieve alignment and distribution through justify-content, align-items and other attributes, yes

CSSHoudini is a set of APIs that allow developers to directly manipulate and extend the browser's style processing flow through JavaScript. 1. PaintWorklet controls element drawing; 2. LayoutWorklet custom layout logic; 3. AnimationWorklet implements high-performance animation; 4. Parser&TypedOM efficiently operates CSS properties; 5. Properties&ValuesAPI registers custom properties; 6. FontMetricsAPI obtains font information. It allows developers to expand CSS in unprecedented ways, achieve effects such as wave backgrounds, and have good performance and flexibility

ReactivitytransforminVue3aimedtosimplifyhandlingreactivedatabyautomaticallytrackingandmanagingreactivitywithoutrequiringmanualref()or.valueusage.Itsoughttoreduceboilerplateandimprovecodereadabilitybytreatingvariableslikeletandconstasautomaticallyreac

CSSgradientsenhancebackgroundswithdepthandvisualappeal.1.Startwithlineargradientsforsmoothcolortransitionsalongaline,specifyingdirectionandcolorstops.2.Useradialgradientsforcirculareffects,adjustingshapeandcenterposition.3.Layermultiplegradientstocre

InternationalizationandlocalizationinVueappsareprimarilyhandledusingtheVueI18nplugin.1.Installvue-i18nvianpmoryarn.2.CreatelocaleJSONfiles(e.g.,en.json,es.json)fortranslationmessages.3.Setupthei18ninstanceinmain.jswithlocaleconfigurationandmessagefil

In Vue, provide and inject are features for directly passing data across hierarchical components. The parent component provides data or methods through provide, and descendant components directly inject and use these data or methods through inject, without passing props layer by layer; 2. It is suitable for avoiding "propdrilling", such as passing global or shared data such as topics, user status, API services, etc.; 3. Note when using: non-responsive original values ??must be wrapped into responsive objects to achieve responsive updates, and should not be abused to avoid affecting maintainability.
