Пример #1
0
        private static int GetStartingPage(Pager 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);
        }
Пример #2
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);
        }
Пример #3
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;
            }
        }
Пример #4
0
 /// <summary>
 /// Constructor to set properties.
 /// </summary>
 /// <param name="currentPage">Current page.</param>
 /// <param name="totalPages">Total number of pages.</param>
 /// <param name="settings">Settings for pager.</param>
 public Pager(int currentPage, int totalPages, PagerSettings settings)
 {
     _pagerSettings = settings;
     SetCurrentPage(currentPage, totalPages);
 }
Пример #5
0
        /// <summary>
        /// Get the pager data using current page and totalPages.
        /// </summary>
        /// <param name="currentPage">Current page.</param>
        /// <param name="totalPages">Total number of pages.</param>
        /// <param name="settings">Pager settings.</param>
        public static Pager Get(int currentPage, int totalPages, PagerSettings settings)
        {
            Pager data = new Pager(currentPage, totalPages, settings);

            return(data);
        }
Пример #6
0
        /// <summary>
        /// Builds the html for non-ajax based url based paging.
        /// </summary>
        /// <param name="urlBuilder">Function to build html.</param>
        /// <param name="settings">Pager settings.</param>
        /// <returns>Created html.</returns>
        public string ToHtml(Func <int, string> urlBuilder, PagerSettings settings)
        {
            string html = PagerBuilderWeb.Instance.Build(this, settings, urlBuilder);

            return(html);
        }
Пример #7
0
 /// <summary>
 /// Calculate the starting page and ending page.
 /// </summary>
 /// <param name="pagerData">Pager data.</param>
 /// <param name="pagerSettings">Pager settings.</param>
 public static void Calculate(Pager pagerData, PagerSettings pagerSettings)
 {
     _instance.Calculate(pagerData, pagerSettings);
 }
Пример #8
0
        /// <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));
            }
        }
Пример #9
0
        /// <summary>
        /// Build the entire html for the pager.
        /// </summary>
        /// <param name="pager"></param>
        /// <param name="settings"></param>
        /// <param name="urlBuilder"></param>
        /// <returns></returns>
        public string Build(Pager pager, PagerSettings settings, Func <int, string> urlBuilder)
        {
            Pager pagerData = pager;

            // Get reference to the default or custom url builder for this pager.
            StringBuilder buffer    = new StringBuilder();
            string        cssClass  = string.Empty;
            string        urlParams = string.Empty;
            string        url       = string.Empty;

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

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

                // 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("&nbsp;&nbsp;&nbsp;");
                }
            }

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

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

            // Build the  ending page link.
            if (pagerData.CanShowLast)
            {
                url = urlBuilder(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("&nbsp;&nbsp;&nbsp;");
                }
                buffer.Append("<a class=\"" + settings.CssClass + "\" href=\"" + url + "\">" + pagerData.TotalPages + "</a>");
            }

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

            return(content);
        }
Пример #10
0
        /// <summary>
        /// Builds the entire html for the specified page index / total pages.
        /// </summary>
        /// <param name="pageIndex">Index of the page.</param>
        /// <param name="totalPages">The total pages.</param>
        /// <param name="settings">The settings for the pager.</param>
        /// <param name="urlBuilder">The URL builder.</param>
        /// <returns></returns>
        public string Build(int pageIndex, int totalPages, PagerSettings settings, Func <int, string> urlBuilder)
        {
            Pager pager = Pager.Get(pageIndex, totalPages, settings);

            return(Build(pager, pager.Settings, urlBuilder));
        }
Пример #11
0
        /// <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);
        }
Пример #12
0
 /// <summary>
 /// Initialize default settings
 /// </summary>
 static PagerSettings()
 {
     Default = new PagerSettings(7, "pager_currentpage", "pager_page");
 }