当前位置:首页 > jquery

jquery 分页

2026-04-07 22:05:53jquery

jQuery 分页实现方法

jQuery 分页可以通过多种方式实现,包括使用插件或手动编写代码。以下是几种常见的实现方法。

使用 jQuery Pagination 插件

引入 jQuery 和 pagination 插件库:

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://pagination.js.org/dist/2.1.5/pagination.min.js"></script>
<link rel="stylesheet" href="https://pagination.js.org/dist/2.1.5/pagination.css">

初始化分页:

$(document).ready(function() {
    $('#pagination-container').pagination({
        dataSource: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
        pageSize: 3,
        callback: function(data, pagination) {
            var html = template(data);
            $('#data-container').html(html);
        }
    });
});

function template(data) {
    var html = '';
    $.each(data, function(index, item) {
        html += '<div>' + item + '</div>';
    });
    return html;
}

手动实现分页逻辑

定义分页函数:

jquery 分页

function paginate(items, pageNumber, pageSize) {
    const startIndex = (pageNumber - 1) * pageSize;
    return items.slice(startIndex, startIndex + pageSize);
}

绑定分页事件:

$(document).ready(function() {
    const allItems = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
    const pageSize = 3;
    let currentPage = 1;

    function renderPage() {
        const pageItems = paginate(allItems, currentPage, pageSize);
        $('#data-container').empty();
        $.each(pageItems, function(i, item) {
            $('#data-container').append('<div>' + item + '</div>');
        });
    }

    $('#prev-btn').click(function() {
        if (currentPage > 1) {
            currentPage--;
            renderPage();
        }
    });

    $('#next-btn').click(function() {
        if (currentPage < Math.ceil(allItems.length / pageSize)) {
            currentPage++;
            renderPage();
        }
    });

    renderPage();
});

使用 DataTables 插件实现分页

引入 DataTables 库:

jquery 分页

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.11.5/css/jquery.dataTables.css">
<script type="text/javascript" src="https://cdn.datatables.net/1.11.5/js/jquery.dataTables.js"></script>

初始化表格分页:

$(document).ready(function() {
    $('#example').DataTable({
        "pagingType": "full_numbers",
        "pageLength": 5
    });
});

分页注意事项

确保分页逻辑与数据加载方式匹配,避免一次性加载大量数据导致性能问题。

分页控件应提供明确的导航选项,包括页码跳转、上一页和下一页按钮。

对于动态加载的数据,考虑使用 AJAX 分页,每次只请求当前页的数据。

标签: 分页jquery
分享给朋友:

相关文章

jquery 之家

jquery 之家

jQuery 之家是一个专注于 jQuery 相关资源的中文网站,提供插件、教程、代码示例等内容。以下是相关信息整理: jQuery 之家网站内容 该网站通常包含以下资源: jQuery…

vue搜索过后实现分页

vue搜索过后实现分页

实现 Vue 搜索后分页的方法 数据绑定与搜索逻辑 在 Vue 组件中定义数据属性,包括原始数据列表、搜索关键词、分页相关参数: data() { return { originalLi…

jquery特效

jquery特效

jQuery 常见特效实现方法 淡入淡出效果 使用 fadeIn() 和 fadeOut() 方法实现元素的渐显和渐隐效果。可通过参数控制动画持续时间(毫秒)和缓动函数。 $("#element")…

jquery 元素

jquery 元素

jQuery 元素选择与操作 jQuery 是一个快速、简洁的 JavaScript 库,简化了 HTML 文档遍历、事件处理、动画和 Ajax 交互。以下是 jQuery 中常用的元素选择与操作方法…

jquery 函数

jquery 函数

jQuery 函数基础 jQuery 是一个快速、简洁的 JavaScript 库,通过封装原生 JavaScript 功能简化 DOM 操作、事件处理、动画等。核心函数 $() 或 jQuery()…

jquery 循环

jquery 循环

jQuery 循环方法 jQuery 提供了多种循环遍历元素或数组的方法,以下是常用的几种方式: each() 方法 each() 是 jQuery 中最常用的循环方法,用于遍历 jQuery 对象…