0

My pager works except for the last page that you click on. So if my last page is 11 that has been clicked the pager stops working when going backwards. If you click a page and then click another it works fine except for the last page. here it is on jsFiddle (its been updated and working now

Javascript:

$(document).ready(function () {
var pageIndex = 1;
function pagerControl(pageIndex, pageCount, step) {
    var result = "";

    if (pageCount > 1) {
        var startPoint = Math.floor((pageIndex / step)) * step;

        if ((pageIndex % step) == 0) {
            startPoint -= step;
        }
        if (pageIndex < pageCount) {
            result += '<a href="#" rel="' + (pageIndex + 1) + '">Next</a>';
        }
        else {
            result += '<span>Next<span>';
        }
        //alert(startPoint);
        for (var i = startPoint + 1; i <= pageCount && i <= (startPoint + step + 1); i++) {
            if (i != pageIndex) {
                result += '<a href="#" rel="' + i + '">' + i + '</a>';
            }
            else {
                result += '<span>' + i + '</span>';
            }
        }

        if (pageIndex > 1) {
            result += '<a href="#" rel="' + (pageIndex - 1) + '">Prev</a>';
        } else {
            result += '<span>Prev</span>';
        }

    }
    $('#pager').html(result);

    $('#pager > a').click(function (e) {
        reload($(e.target).attr('rel'));
        e.preventDefault();
    });
}
function reload(page) {
    pagerControl(page, 11, 4);
}
pagerControl(1, 11, 4);
});

jsFiddle

4
  • On page 3, when you press "Next", the numbers completely disappear, and the "Prev" link does nothing.
    – Kevin Ji
    Nov 23, 2011 at 1:57
  • oh that will have to be fixed aswell do you know how to fix that
    – ONYX
    Nov 23, 2011 at 2:00
  • The prev link works upto page 10 then it stops but I don't know why next link jumps
    – ONYX
    Nov 23, 2011 at 2:04
  • I've fixed the next link and it works now I've updated my code on jsFiddle but I'm still having problems with the last page being clicked and then clicking backwards
    – ONYX
    Nov 23, 2011 at 2:39

2 Answers 2

2

Try this ' $('#pager a').click ' while binding click event with links instead of present ' $('#pager > a').click '.

It's working on my side. I know this isn't an elaborated answer but a quick correction.

.. :)

0
1

Sometimes we complicate things too much. How about abolish the "if" statement ... for most cases?

    var pageIndex = 1;
    function pagerControl(pageIndex, pageCount, step) {
        var result = '';

        if (pageCount > 1) {
            pageIndex = (pageIndex > pageCount) ? pageCount : pageIndex;
            var startPoint = Math.floor((pageIndex / step)) * step;
            var endPoint = pageCount;

            startPoint = (startPoint < 1) ? 1 : startPoint;
            endPoint   = ((startPoint + step) > pageCount) ? pageCount : (startPoint + step -1);

            result = '<a href="#" rel="' + (startPoint-4) + '">PREV</a>   ';
            for (var i=startPoint;i<(endPoint+1);i++) 
                result += '<a href="#" rel="' + i + '">' + i + '</a>';
            result += '<a href="#" rel="' + endPoint+4 + '">   NEXT</a>';
        }
        $('#pager').html(result);
        $('#pager > a').click(function (e) {
          reload(parseInt($(e.target).attr('rel')));
          e.preventDefault();
        });
    }

    function reload(page) {
        pagerControl(page, 11, 4);
    }

    pagerControl(1, 11, 4);
11
  • That doesn't actually fix the problem.
    – Kevin Ji
    Nov 23, 2011 at 4:21
  • Well mc10, it worked fine, as expected, in the jsFiddle. Try it yourself! And, besides, makes it a much cleaner coding. Nov 23, 2011 at 4:24
  • I fixed my problem it was the e.preventDefault() after the reload function so I swapped them around and it worked. but thanks for you're answer I might use you're startpoint and endpoint for the {...} part going through the records
    – ONYX
    Nov 23, 2011 at 4:24
  • have a look at the jsFiddle now
    – ONYX
    Nov 23, 2011 at 4:25
  • Thank you. If you can, at least vote my answer up. Appreciated! Nov 23, 2011 at 4:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.