【问题标题】:How to make a dynamic bootstrap pagination with JS?如何使用 JS 进行动态引导分页?
【发布时间】:2019-09-29 16:18:59
【问题描述】:

所以基本上我使用 AJAX 调用这个 840 Json 对象数据集,并使用 div 和足够简单的引导分页显示它。现在我的问题是,我有 94​​ 个页面,并且所有页面按钮都一直显示。我认为这既不实用、不美观也不对用户友好,所以我想解决这个问题。

所以我已经在互联网上搜索了这个问题。我发现了几个分页插件应该可以提供我需要的东西,比如 simplePagination.js 或 twbsPagination。后者对我来说效果最好,但仍然无法正常工作。我能够启动并运行新的分页,但它不会改变实际的页面内容。我现在尝试了更多插件并尝试修改我现有的代码,但没有任何效果。我现在将我的代码恢复到正常的分页。

pageSize = 9;

    var pageCount = $(".line-content").length / pageSize 
//calculating the number of pages needed
    var pagin = document.getElementById('pagin');
//deleting the previous displayed page buttons
    while (pagin.firstChild) {
        pagin.removeChild(pagin.firstChild);
    }

    for (var i = 0; i < pageCount; i++) { 
//creating the page items
        $("#pagin").append('<li class="page-item"><a class="page-link" href="#">' + (i + 1) + '</a></li> ');
    }
//styling the current page item
    $("#pagin li").first().find("a").addClass("current") 

    // the function to actually change the content on the selected page
    showPage = function (page) {
        $(".line-content").hide();
        $(".line-content").each(function (n) {
            if (n >= pageSize * (page - 1) && n < pageSize * page)
                $(this).show();
        });
    }

    showPage(1); //displaying the content

    //changing the style
    $("#pagin li a").click(function () {
        $("#pagin li a").removeClass("current");
        $(this).addClass("current");
        showPage(parseInt($(this).text()))
    });     

所以基本上我得到了这个工作分页,但我想让它更时尚。我对任何 jquery 插件和一切都持开放态度。只要它做我希望它做的事。 My pagination looks like this I'd like to have it looking somewhat like this

【问题讨论】:

    标签: javascript jquery html ajax bootstrap-4


    【解决方案1】:

    HTML

    <table class="table">
            <thead>
                <tr>
                    <th scope="col">#</th>
                    <th scope="col">Name</th>
                    <th scope="col">Salary</th>
                    <th scope="col">Age</th>
                </tr>
            </thead>
            <tbody class="page-data">
    
    
            </tbody>
        </table>
        <nav aria-label="Page navigation example">
            <ul class="pagination">
                <li onclick="prePage()" class="page-item page-list">
                    <a class="page-link" href="#" aria-label="Previous">
                        <span aria-hidden="true">&laquo;</span>
                        <span class="sr-only">Previous</span>
                    </a>
                </li>
    
                <li onclick="nextPage()" class="page-item">
                    <a class="page-link" href="#" aria-label="Next">
                        <span aria-hidden="true">&raquo;</span>
                        <span class="sr-only">Next</span>
                    </a>
                </li>
            </ul>
        </nav>
    

    JS 代码

    <script>
            let userData = null;
            $.ajax({
                url: "http://dummy.restapiexample.com/api/v1/employees",
                type: "get",
                async: false,
                success: function(users) {
                    userData = users.data;
                }
            });
            var currentPage = 0;
            let pages = "";
            let page_size = 5;
            pages = paginate(userData, page_size);
            pageLi = "";
            pages.forEach((element, index) => {
                if (index != 0)
                    pageLi += '<li onclick="pageChange(' + index + ')" id="page_' + index + '" class="page-item list-item" id="page_' + index + '"><a class="page-link" href="javascript:void(0)">' + index + '</a></li>';
            });
            $(".page-list").after(pageLi);
            page = pages[currentPage];
            printRows(page);
    
            function nextPage() {
                if (pages.length - 1 > currentPage)
                    page = currentPage + 1;
                pageChange(page);
            }
    
            function prePage() {
                if (currentPage < pages.length && currentPage != 0)
                    page = currentPage - 1;
                pageChange(page);
            }
    
            function pageChange(page) {
                currentPage = page;
                $(".list-item").removeClass("active");
                $("#page_" + page).addClass("active");
                $(".page-data").html("");
                page = pages[page];
                printRows(page);
            }
    
            function printRows(arr) {
                arr.forEach(element => {
                    $(".page-data").append("<tr><td>" + element.id + "</td><td>" + element.employee_name + "</td><td>" + element.employee_salary + "</td><td>" + element.employee_age + "</td></tr>");
    
                });
            }
    
            function paginate(arr, size) {
                return arr.reduce((acc, val, i) => {
                    let idx = Math.floor(i / size)
                    let page = acc[idx] || (acc[idx] = [])
                    page.push(val)
                    return acc
                }, [])
            }
        </script>
    

    【讨论】:

    • 好一个!帮助我使用 vue js 1 创建自定义组件。
    【解决方案2】:

    我建议你使用bootpag

    它非常简单,并且与引导程序配合得很好,它还有很好的文档来告诉你如何逐步使用它。
    网站上都有解释,这里就不用解释了。

    我希望这可以帮助你。

    链接:http://botmonster.com/jquery-bootpag/#pro-page-8

    【讨论】:

      猜你喜欢
      • 2016-03-29
      • 1970-01-01
      • 2016-07-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-27
      • 1970-01-01
      相关资源
      最近更新 更多