views:

52

answers:

1

Hai i am generating page numbers based on currentpage and lastpage using jquery ... Here is my function and as i am newbie i dont know how it can be done...

function generatePages(currentPage, LastPage) {
    if (LastPage <= 5) {
        var pages = '';
        for(var i=1;i<=5;i++)
        {
            pages += "<a class='page-numbers' href='#'>" + i + "</a>"
        }
        $("#PagerDiv").append(pages);
    }
    if (LastPage > 5) {
        var pages = '';
        for (var i = 1; i <= 5; i++) {
            pages += "<a class='page-numbers' href='#'>" + i + "</a>"
        }
        $("#PagerDiv").append(pages);
    }
}

I want the result to be like this

If it is the first page

istpage

If it is in the middle,

Pager

If it is the last page,

lastpage

I have the lastPage and currentPage values please help me out getting this...

+1  A: 

First of all, you have to find a correct algorithm for what you want to do and the function you posted shows that you don't have any.

The logic to implement could be:

  • Display page numbers from currentPage - 2 to currentPage + 2 (limited to [1, lastPage])
  • if page 1 is not is displayed, prepend "1 ..."
  • if lastPage is not displayed, append "... lastPage"

And additionally, you can add that:

  • if currentPage is greater than 1, prepend "prev"
  • if currentPage is less than lastPage, append "next"

That's for the theory. Now, for the implementation, just use jPaginate or another pagination plugin for jQuery instead...

ybo
@ybo i am not looking for a plugin plz... I have to write a function that takes the two parameters... ANy suggestion...
bala3569
@bala3569, I just gave you the algorithm, I'd like to see you try and implement it yourself first. Even if you don't want to use plugins, you can check their source code to see how they're implemented... The jQuery pagination plugin from d-scribe.de (first hit on google) does exactly what you want.
ybo
@ybo that does uses json but i am doing it for custom pagination (1.e) 5 records initially... And i have `lastpage` value that cangenerate links for me...
bala3569
@ybo it worked....
bala3569