CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2009
    Posts
    1

    Pagination problem

    I can't work out the algorithm for a pager I want to use. I've been struggling with this for a few days now and need some help, I don't even have a good strategy.


    There are two groups, start and end. I want to represent missing pages with ... and to supply the minimum no of adjacent pages there should always be (where possible).

    the first two and last two pages should always be shown
    [n] = current page
    ... is attached to end, should only appear when there are missing pages

    The best way to describe it is by showing expected output


    total_pages = 6
    adjacent_pages = 1
    [1] 2 ... 5 6
    1 [2] 3 ... 5 6
    1 2 [3] 4 5 6
    1 2 3 [4] 5 6
    1 2 ... 4 [5] 6
    1 2 ... 5 [6]

    total_pages = 6
    adjacent_pages = 2
    [1] 2 3 ... 5 6
    1 [2] 3 4 5 6
    1 2 [3] 4 5 6
    1 2 3 [4] 5 6
    1 2 3 4 [5] 6
    1 2 ... 4 5 [6]

    total_pages = 7
    adjacent_pages = 2
    [1] 2 3 ... 6 7
    1 [2] 3 4 ... 6 7
    1 2 [3] 4 5 6 7
    1 2 3 [4] 5 6 7
    1 2 3 4 [5] 6 7
    1 2 ... 4 5 [6] 7
    1 2 ... 5 6 [7]


    Ideally I would also like to have a middle section as well eg

    total_pages = 9
    adjacent_pages = 1
    1 2 3 [4] 5 ... 8 9 (... attached to end group)
    1 2 ... 4 [5] 6 ... 8 9 (first ... attached to middle group)
    1 2 ... 5 [6] 7 8 9 (... attached to end group)

  2. #2
    Join Date
    Oct 2006
    Posts
    616

    Re: Pagination problem

    I can try to give you some pointers.
    Basically, you should iterate over all pages, and for each page, reach a decision whether you should show it's number or not.
    If you decided that you shouldn't show a certain page x, then remember that you might decide not to show page x+1 as well - in that case '...' will stand for both pages (or for some k following pages).
    Pseudo code should be something like:
    Code:
    PROCEDURE PRINT_PAGINATION(input N = total number of pages, n = current page)
        FOR i <-- 1 to N, DO
           IF SHOULD_SHOW( i ), THEN
               PRINT( i )
               PRINT( " " ) //print 'space'       
           ELSE //we shouldn't show page i - now, when should we print "... " ?!?
               IF NOT SHOULD_SHOW( i-1 ), THEN
                  PRINT( "... ")
               END IF
           END IF
        END FOR
    END PROCEDURE
    Please try to understand why the pseudo code above will work.
    You should still figure out by yourself how to write the SHOULD_SHOW function.

    Regards,
    Zachm

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured