繁体   English   中英

jqPagination - 如何指定每页的记录数?

[英]jqPagination - How do you specify the number of records per page?

我最近一直在玩jqPagination ,这是一个用于分页的jQuery插件。 这是一个非常简洁的插件,我喜欢你可以输入页码。 但是,我遇到了一些困难,因为没有太多的文档。 虽然这可能是一个简单的问题,但我很好奇是否有办法指定每页显示多少条记录。 我目前有分页工作每页一个记录,这是由jQuery选择器控制。 不过,我希望能够指定多少个数字。

显示我的一些代码可能更简单。 我实际上使用了以前的堆栈溢出问题作为基础:

<div id="container">
    <div id="div1">Any elements</div>
    <div id="div2">Any elements</div>
    <div id="div3">Any elements</div>
    ... and so on
</div>

而对于jQuery:

//Hide 
$("#container").children().not('#firstDiv').hide();

$('.pagination').jqPagination( {
    max_page    : $('#container').children().length,
    paged        : function(page) {

        //hide the other divs
        $('#container').children().hide();

        //show the divs on the specified page
        $($('#container').children()[page - 1]).show();

所以上面的代码一次只显示一个div,我想指定它可以显示多少个元素/ div。

哦,作为一个后续问题,是否可以指定某个div出现在某个页面上,例如页脚只出现在最后一页?

更新:已完成

在创作者的帮助下,我能够弄明白。 我将在下面发布我的解决方案以防将来帮助其他任何人。 我让用户在XML文件中选择每页的记录数(不包括XML选择器)。 如果每页的记录少于总数,则会显示分页。 我确信这是一种更有效的方式,但我对自己拥有的东西感到满意。 这是下面的代码:

/*---------------------------------------------------------------------------
Place this anywhere in your script.
Automatically hide pagination. 
If there are less scenes per page than the total amount, then include pagination.
----------------------------------------------------------------------------*/
$(".pagination").hide();
var recordsPerPage = 5
var totalNumRecords = 20;

if (recordsPerPage < totalNumRecords) {
     pagination(recordsPerPage, totalNumRecords);
}



//recordsPerPage is the number of items you want to display on each page
//totalNumRecords is the total number of items that you have

function pagination(recordsPerPage, totalNumRecords){
    //Show the pagination controls
    $(".pagination").show();

    //loop through all of the divs and hide them by default.
    for (var i=1; i <= totalNumRecords; i++) {
        $("#container").find("#div" + i).hide();
    }

    //then only display the number of divs the user dictated
    for (var i = 1; i <= recordsPerPage; i++) {
        $("#container").find("#div" + i).show();
    }

    //maxPages is the maximum amount of pages needed for pagination. (round up) 
    var maxPages = Math.ceil(totalNumRecords/recordsPerPage);   

    $('.pagination').jqPagination({
        link_string : '/?page={page_number}',
        max_page     : maxPages,
        paged        : function(page) { 

            //a new page has been requested

            //loop through all of the divs and hide them all.
            for (var i=1; i <= totalNumRecords; i++) {
                $("#container").find("#div" + i).hide();
            }

            //Find the range of the records for the page: 
            var recordsFrom = recordsPerPage * (page-1) + 1;
            var recordsTo = recordsPerPage * (page);

            //then display only the records on the specified page
            for (var i = recordsFrom; i <= recordsTo; i++) {
                $("#container").find("#div" + i).show();
            }      

            //scroll to the top of the page if the page is changed
            $("html, body").animate({ scrollTop: 0 }, "slow");
        }
    });
}

谢谢本!

你有一系列div元素是你的页面吗?在这些元素中只显示你想要的多个记录? 这样你就可以使用插件简单地显示/隐藏每个页面div

这可能就是我在那种情况下所做的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM