开发者

Pagination page saves page in to cookie

I have a pagination which is done like this:

this.pagination = function(limit, length){
        //number of pages
        var nrpages = Math.ceil(length/limit);
        var currPage = this.pagingcurrPage;

        console.log('l- '+length);
        console.log('p- '+nrpages);
        console.log('c- '+currPage);
        $.cookie('presssection-cp',currPage);
        var thisSection = this;

        //active class for first page
        if(currPage == 1){
            var activeClass = ' class="active"';
        }
        else{
            var activeClass = '';
        }

        var output = '<li class="page-item"><a'+activeClass+' href="0">1</a></li>';

        if(nrpages > 1){
            if(nrpages > 7){
                if(currPage <= 4){
                    for( i=2; i < 7; i++){
                        offset = (i-1) * limit;
                        //active class for current page
                        if((currPage-1) * limit == offset){
                            activeClass = ' class="active"';
                        }
                        else{
                            activeClass = '';
                        }
                        output += '<li class="page-item"><a'+activeClass+' href="'+offset+'">'+i+'</a></li>';
                    }
                    output += '<li class="rightDot page-item">...</li>';
                }
                else if(currPage >= (nrpages - 4)){
                    var pageFrom = nrpages - 5;
                    var pageTo = nrpages;

                    output += '<li class="leftDot page-item">...</li>';
                    for( i=pageFrom; i < pageTo; i++){
                        offset = (i-1) * limit;
                        //active class for current page
                        if((currPage-1) * limit == offset){
                            activeClass = ' class="active"';
                        }
                        else{
                            activeClass = '';
                        }
                        output += '<li class="page-item"><a'+activeClass+' href="'+offset+'">'+i+'</a></li>';
                    }
                }
                else{
                    var pageFrom = currPage - 2;
                    var pageTo = pageFrom + 5;

                    output += '<li class="leftDot page-item">...</li>';
                    for( i=pageFrom; i < pageTo; i++){
                        offset = (i-1) * limit;
                        //active class for current page
                        if((currPage-1) * limit == offset){
                            activeClass = ' class="active"';
                        }
                        el开发者_JAVA技巧se{
                            activeClass = '';
                        }
                        output += '<li class="page-item"><a'+activeClass+' href="'+offset+'">'+i+'</a></li>';
                    }
                    output += '<li class="rightDot page-item">...</li>';
                }
            }
            else{
                for(i=2; i < nrpages; i++){
                    if( i==1 ){
                        offset = 0;
                    }
                    else{
                        offset = (i-1) * limit;
                    }
                    if(currPage == i){
                        activeClass = ' class="active"';
                    }
                    else{
                        activeClass = '';
                    }
                    output += '<li class="page-item"><a'+activeClass+' href="'+offset+'">'+i+'</a></li>';
                }
            }

            //active class for last page
            if(currPage == nrpages){
                activeClass = ' class="active"';
            }
            else{
                activeClass = '';
            }
            output += '<li class="page-item"><a'+activeClass+' href="'+(nrpages-1)*limit+'">'+nrpages+'</a></li>';
            $('#pressItemsPagination ul.pager').html(output);
            $('#pressItemsPagination ul.pager').show();
        }
        else{
            $('#pressItemsPagination ul.pager').hide();
        }

            //debugTime('section '+this.sectionId+' init start');
        var html = '';

        html += '<div id="'+this.sectionId+'" class="tSection '+this.type+' '+this.section+'">';

            html += '\
            <div class="clearFix">\
                <ul class="tPaging pagination" style="float: left;">&nbsp;</ul>\
            </div>';
        html += '</div>'

        //register pagination clicks
        $('#pressItemsPagination ul.pager li a').live('click',function() {

            //get the offset
            var offset = $(this).attr('href');

            if( offset.indexOf('/') ) {
                offset = offset.substr(offset.lastIndexOf('/')+1);
            }

How can I make that script to remember the where user is? If user clicks on page 2 on pagination and then goes on different page and the ncomes back to the page where the pagination is, it would be nice if it could remember that user was on page 2. Now it resets back to page 1. I have currPage saved on cookie atm. Is that correct? What else i need to change in that script so it would remember the paginaition page.


You should save the current page whenever it is switched and read the cookie on document ready. If the cookie is not set, do nothing. Otherwise, jump to the saved page. I recommend taking a look at quirksmode.org's handy cookie functions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜