• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

PAgination by grouping pages

Hi I am looking for solution for Pagination by grouping. Currently I have a form from which the user can select the  number of rows or records he wants to see. This information is passed to another form where the total numbers of records is got and then divided by the number of rows passed to get the total number of pages and is shown as strvariable  For ex if the number of records is 1000 and number of rows passed is 25 there will be 40 pages with links to all and next and previous links. But I want the pages to be shown in groups of 10. If there are more than ten pages the first 10 will be shown as 1,2,3,4,5,6,7,8,9,10..>>>Next 10 and so on. The links to previous should be active. Can someone help?

1 Solution
so, which language are you using ?
PNKJAuthor Commented:
Hi There !

Am posting a code that contains an exact implementation of this logic you are trying to build in. Had to write this sometime back for an application. The code is not too complicated (though trust me the algo is !!).
C_NEXTLINK and C_... are all constants declared in a class called OtherConstants so u needn't worry abt them, just replace them wit >> or << or watever !

OtherConstants.C_MAXPAGES = 10 that is the maximum number of pages u want the system to display at once. Keep on changing it to whatever value u want and everythin would still work !!

 Dim lsDelimiter As String
    Dim liLoop As Integer
    Dim lsHyperLink As String
    Dim liPageCount As Integer = 1
    Dim lsPrevLink As String = ""
    Dim lsNextLink As String = ""
    Dim liPrevPage As Integer = 0
    Dim liNextPage As Integer = 0
    Dim liQuotent As Integer = 0
    Dim liRemainder As Integer = 0
    Dim liTemp As Integer = 0
    Dim liMaxPages As Integer = OtherConstants.C_MAXPAGES


      'General Rules
      '1. Previous link would come only if current page is > MaxNumberofAllowedPages and the user is not viewing a page in the first batch of pages
      '2. Next link would come if the total number of pages is >MaxNumberofAllowedPages and totalnumberofpages - currentpage > MaxNumberofAllowedPages

      lsDelimiter = "<img src='images/gifDotedLine.gif' hspace='3' align='absbottom'>"

      'find if the Prev link is to be rendered or not and make the string if it is to be rendered
      If miCurrentPage > liMaxPages Then
        'Find the previous page that the Previous link should point to

        liQuotent = Math.DivRem(miCurrentPage, liMaxPages, liRemainder)
        If liRemainder = 0 Then
          liPrevPage = miCurrentPage - liMaxPages
          liPrevPage = miCurrentPage - liRemainder
        End If

        'Render previous link
        lsHyperLink = "&nbsp;<a href = ""javascript:togglePage('" & liPrevPage & "')"""
        lsHyperLink = lsHyperLink & ">" & OtherConstants.C_PREVLINK & "</a>&nbsp;"
        lsPrevLink = lsHyperLink
      End If

      liQuotent = 0
      liRemainder = 0
      'Find if the Next LInk is required and calculate and render the string if it is required

      If miTotalPages > liMaxPages Then
        liQuotent = Math.DivRem(miTotalPages, liMaxPages, liRemainder)
        If liRemainder = 0 Then
          liTemp = (liQuotent - 1) * liMaxPages + 1
          liTemp = (liQuotent * liMaxPages) + 1
        End If

        If miCurrentPage < liTemp Then
          'Next link required, calculate and generate the string
          liNextPage = liPrevPage + liMaxPages + 1
          lsHyperLink = "&nbsp;<a href = ""javascript:togglePage('" & liNextPage & "')"""
          lsHyperLink = lsHyperLink & ">" & OtherConstants.C_NEXTLINK & "</a>"
          lsNextLink = lsHyperLink
        End If

      End If
      liTemp = 0
      'Now we will calculate the boundaries of our For Loop
      'Lower bound is always liPrevPage+1
      'Upper bound will be lower of liPrevPage+maxpages and miTotalPages

      liTemp = Math.Min(liPrevPage + liMaxPages, miTotalPages)
      For liLoop = liPrevPage + 1 To liTemp
        If liLoop <> miCurrentPage Then
          lsHyperLink = "<a href = ""javascript:togglePage('" & liLoop & "')"""
          lsHyperLink = lsHyperLink & ">" & liLoop & "</a>"

          lsHyperLink = liLoop
        End If
        If liLoop <> liTemp Then
          lsHyperLink = lsHyperLink & lsDelimiter
        End If
        msPageNum = msPageNum & lsHyperLink

      'Append the prev next srtings
      If lsNextLink.Trim <> "" Then
        lsNextLink = lsDelimiter & lsNextLink
      End If
      If lsPrevLink <> "" Then
        lsPrevLink = lsPrevLink & lsDelimiter
      End If
      msPageNum = "Page: " & lsPrevLink & msPageNum & lsNextLink

    Catch ex As Exception
      Throw ex
    End Try

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now