Example #1
0
        private static int GetStartingPage(PagerData pagerData, PagerSettings settings)
        {
            // Total pages = 5 = number of pages to display.. so starting page is 1;
            if (pagerData.CurrentPage <= settings.NumberPagesToDisplay)
            {
                return(1);
            }
            int range       = GetRange(pagerData.CurrentPage, settings.NumberPagesToDisplay);
            int totalRanges = GetTotalRanges(pagerData.TotalPages, settings.NumberPagesToDisplay);

            // Current page = 17
            // Total pages = 18
            // Pages to show = 5
            // Range = 4
            // Starting page = 14, 15, 16, 17, 18
            if (range == totalRanges)
            {
                return((pagerData.TotalPages - settings.NumberPagesToDisplay) + 1);
            }
            // current page = 7
            // Total pages = 18
            // Pages to show = 5
            // Range = 2
            // starting page = 6
            range--;
            return((range * settings.NumberPagesToDisplay) + 1);
        }
        /// <summary>
        /// Build page numbers list.
        /// </summary>
        /// <param name="ctx">Pager action context</param>
        /// <param name="pagerSettings"></param>
        public static void BuildPageNumbersList(IList <PageNumber> pageNumbersList, PagerData pagerData, PagerSettings pagerSettings)
        {
            // Clear existing to build them.
            pageNumbersList.Clear();

            // Build each page number.
            for (int ndx = pagerData.StartingPage; ndx <= pagerData.EndingPage; ndx++)
            {
                string cssClass = (ndx == pagerData.CurrentPage) ? pagerSettings.CssCurrentPage : string.Empty;
                pageNumbersList.Add(new PageNumber(ndx, cssClass));
            }
        }
Example #3
0
        private static int GetEndingPage(PagerData pagerData, PagerSettings settings)
        {
            // Total pages = 5 = Number to display 1 - 5
            if (pagerData.TotalPages <= settings.NumberPagesToDisplay)
            {
                return(pagerData.TotalPages);
            }

            int range       = GetRange(pagerData.CurrentPage, settings.NumberPagesToDisplay);
            int totalRanges = GetTotalRanges(pagerData.TotalPages, settings.NumberPagesToDisplay);

            if (range == totalRanges)
            {
                return(pagerData.TotalPages);
            }

            // if page = 8, ending page is 10.
            return(range * settings.NumberPagesToDisplay);
        }
Example #4
0
        public void Calculate(PagerData pagerData, PagerSettings pagerSettings)
        {
            // Check bounds.
            if (pagerData.CurrentPage < 0)
            {
                pagerData.CurrentPage = 1;
            }
            if (pagerData.CurrentPage > pagerData.TotalPages)
            {
                pagerData.CurrentPage = 1;
            }

            int currentPage = pagerData.CurrentPage;

            // Calculate the starting and ending page.
            pagerData.StartingPage = GetStartingPage(pagerData, pagerSettings);
            pagerData.EndingPage   = GetEndingPage(pagerData, pagerSettings);

            // Calculate next page.
            if (currentPage + 1 <= pagerData.TotalPages)
            {
                pagerData.NextPage = currentPage + 1;
            }
            else
            {
                pagerData.NextPage = currentPage;
            }

            // Calculate previous page number.
            if (currentPage - 1 >= 1)
            {
                pagerData.PreviousPage = currentPage - 1;
            }
            else
            {
                pagerData.PreviousPage = currentPage;
            }
        }
Example #5
0
 /// <summary>
 /// Constructor to initialize properties.
 /// </summary>
 /// <param name="action"></param>
 /// <param name="pagerData"></param>
 public PagerActionContext(PagerAction action, PagerData pagerData, int newSelectedPage)
 {
     ActionType      = action;
     PageData        = pagerData;
     NewSelectedPage = newSelectedPage;
 }
        /// <summary>
        /// Build the page number links as url links.
        /// </summary>
        /// <param name="ctx"></param>
        /// <param name="settings"></param>
        /// <returns></returns>
        public string Build(PagerActionContext ctx, PagerSettings settings)
        {
            PagerData pagerData = ctx.PageData;

            // Get reference to the default or custom url builder for this pager.
            // A custom url builder may be used for SEO.
            PagerUrlBuilder urlBuilder = null;

            if (ctx.UrlBuilder == null)
            {
                urlBuilder = new PagerUrlBuilder(PagerHelper.BuildUrlWithParams);
            }
            else
            {
                urlBuilder = ctx.UrlBuilder;
            }

            StringBuilder buffer    = new StringBuilder();
            string        cssClass  = string.Empty;
            string        urlParams = string.Empty;
            string        url       = ctx.Url;

            buffer.Append("<table><tr>");

            // Build the previous page link.
            if (pagerData.CanShowPrevious)
            {
                // Previous
                url = urlBuilder(ctx.Url, pagerData.CurrentPage - 1);
                buffer.Append("<td><span class=\"" + settings.CssClass + "\">");
                buffer.Append("<a href=\"" + url + "\">" + "&#171;" + "</a>");
                buffer.Append("</span></td>");
            }

            // Build the starting page link.
            if (pagerData.CanShowFirst)
            {
                // First
                url = urlBuilder(ctx.Url, 1);
                buffer.Append("<td><span class=\"" + settings.CssClass + "\">");
                buffer.Append("<a href=\"" + url + "\">" + 1 + "</a>");
                buffer.Append("</span></td>");

                // This is to avoid putting ".." between 1 and 2 for example.
                // If 1 is the starting page and we want to display 2 as starting page.
                if (pagerData.CanShowPrevious)
                {
                    buffer.Append("<td> .. </td>");
                }
            }

            // Each page number.
            for (int ndx = pagerData.StartingPage; ndx <= pagerData.EndingPage; ndx++)
            {
                cssClass = (ndx == pagerData.CurrentPage) ? settings.CssCurrentPage : string.Empty;
                url      = urlBuilder(ctx.Url, ndx);

                // Build page number link. <a href="<%=Url %>" class="<%=cssClass %>" ><%=ndx %></a>
                buffer.Append("<td><span class=\"" + settings.CssClass + "\">");
                buffer.Append("<a href=\"" + url + "\" class=\"" + cssClass + "\">" + ndx + "</a>");
                buffer.Append("</span></td>");
            }

            // Build the  ending page link.
            if (pagerData.CanShowLast)
            {
                url = urlBuilder(ctx.Url, pagerData.TotalPages);

                // This is to avoid putting ".." between 7 and 8 for example.
                // If 7 is the ending page and we want to display 8 as total pages.
                if (pagerData.CanShowNext)
                {
                    buffer.Append("<td> .. </td>");
                }
                buffer.Append("<td><span class=\"" + settings.CssClass + "\">");
                buffer.Append("<a href=\"" + url + "\">" + pagerData.TotalPages + "</a>");
                buffer.Append("</span></td>");
            }

            // Build the next page link.
            if (pagerData.CanShowNext)
            {
                // Previous
                url = urlBuilder(ctx.Url, pagerData.CurrentPage + 1);
                buffer.Append("<td><span class=\"" + settings.CssClass + "\">");
                buffer.Append("<a href=\"" + url + "\">" + "&#187;" + "</a>");
                buffer.Append("</span></td>");
            }

            buffer.Append("</tr></table>");
            string content = buffer.ToString();

            return(content);
        }
Example #7
0
 /// <summary>
 /// Calculate the starting page and ending page.
 /// </summary>
 /// <param name="pagerData"></param>
 /// <param name="pagerSettings"></param>
 public static void Calculate(PagerData pagerData, PagerSettings pagerSettings)
 {
     _instance.Calculate(pagerData, pagerSettings);
 }