public LocaleResourcesIndexViewModel(PaginationModel<LocaleResource> items, 
     IEnumerable<Language> languages, 
     Guid currentLanguageId)
 {
     LanguageList = languages.ToSelectList(currentLanguageId);
     Items = items;
     CurrentLanguageId = currentLanguageId;
 }
        public ActionResult UsersPage(int? page)
        {
            var realPage = page ?? 1;
            string file = WebConfigurationManager.AppSettings["TestFile"];

            var users = HttpContext.Cache.Get(file) as List<UserModel>;
            if (users == null)
            {
                var fileText = System.IO.File.ReadAllText(Server.MapPath(file));
                users = new JavaScriptSerializer().Deserialize<List<UserModel>>(fileText);
                if (users != null)
                    HttpContext.Cache.Insert(file, users);
                else
                    return View();
            }

            var pageData = users.Skip((realPage - 1) * UsersOnPage).Take(UsersOnPage);
            var pagination = new PaginationModel()
                {
                    Page = realPage,
                    LastPage = Convert.ToInt32(Math.Ceiling((double)users.Count / UsersOnPage)),
                    NumberOfDisplayedPages = 5,
                    DefaultDestination = new ActionDestination() { Action = "UsersPage", Controller = "Home" }
                };

            if (Request.IsAjaxRequest())
            {
                return Json(new { pageData = pageData , 
                    pagination = pagination.GetShownPages(), 
                    current = realPage, 
                    next = !pagination.IsLast, 
                    previous = !pagination.IsFirst}, 
                    JsonRequestBehavior.AllowGet);
            }
            var model = new UsersPageModel()
            {
                PagginationData = pagination,
                Users = pageData
            };
            return View(model);
        }
예제 #3
0
        public virtual PaginationModel BuildPaginationModel(Func <int, string> generateUrl)
        {
            var pageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize);
            var model     = new PaginationModel {
                PageSize = this.pageSize, CurrentPage = this.currentPage, TotalItemCount = this.totalItemCount, PageCount = pageCount
            };

            // Previous
            model.PaginationLinks.Add(currentPage > 1 ? new PaginationLink {
                Active = true, DisplayText = "«", PageIndex = currentPage - 1, Url = generateUrl(currentPage - 1)
            } : new PaginationLink {
                Active = false, DisplayText = "«"
            });

            var start = 1;
            var end   = pageCount;
            var nrOfPagesToDisplay = this.pagerOptions.MaxNrOfPages;

            if (pageCount > nrOfPagesToDisplay)
            {
                var middle = (int)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1;
                var below  = (currentPage - middle);
                var above  = (currentPage + middle);

                if (below < 2)
                {
                    above = nrOfPagesToDisplay;
                    below = 1;
                }
                else if (above > (pageCount - 2))
                {
                    above = pageCount;
                    below = (pageCount - nrOfPagesToDisplay + 1);
                }

                start = below;
                end   = above;
            }

            if (start > 1)
            {
                model.PaginationLinks.Add(new PaginationLink {
                    Active = true, PageIndex = 1, DisplayText = "1", Url = generateUrl(1)
                });
                if (start > 3)
                {
                    model.PaginationLinks.Add(new PaginationLink {
                        Active = true, PageIndex = 2, DisplayText = "2", Url = generateUrl(2)
                    });
                }
                if (start > 2)
                {
                    model.PaginationLinks.Add(new PaginationLink {
                        Active = false, DisplayText = "...", IsSpacer = true
                    });
                }
            }

            for (var i = start; i <= end; i++)
            {
                if (i == currentPage || (currentPage <= 0 && i == 1))
                {
                    model.PaginationLinks.Add(new PaginationLink {
                        Active = true, PageIndex = i, IsCurrent = true, DisplayText = i.ToString()
                    });
                }
                else
                {
                    model.PaginationLinks.Add(new PaginationLink {
                        Active = true, PageIndex = i, DisplayText = i.ToString(), Url = generateUrl(i)
                    });
                }
            }
            if (end < pageCount)
            {
                if (end < pageCount - 1)
                {
                    model.PaginationLinks.Add(new PaginationLink {
                        Active = false, DisplayText = "...", IsSpacer = true
                    });
                }
                if (pageCount - 2 > end)
                {
                    model.PaginationLinks.Add(new PaginationLink {
                        Active = true, PageIndex = pageCount - 1, DisplayText = (pageCount - 1).ToString(), Url = generateUrl(pageCount - 1)
                    });
                }

                model.PaginationLinks.Add(new PaginationLink {
                    Active = true, PageIndex = pageCount, DisplayText = pageCount.ToString(), Url = generateUrl(pageCount)
                });
            }

            // Next
            model.PaginationLinks.Add(currentPage < pageCount ? new PaginationLink {
                Active = true, PageIndex = currentPage + 1, DisplayText = "»", Url = generateUrl(currentPage + 1)
            } : new PaginationLink {
                Active = false, DisplayText = "»"
            });

            // AjaxOptions
            if (pagerOptions.AjaxOptions != null)
            {
                model.AjaxOptions = pagerOptions.AjaxOptions;
            }

            model.Options = pagerOptions;
            return(model);
        }
        /// <summary>
        /// Initializes the view model
        /// </summary>
        /// <param name="rendering">The rendering</param>
        /// <param name="products">The list of child products</param>
        /// <param name="sortFields">The fields to allow sorting on</param>
        /// <param name="searchOptions">Any search options used to find products in this category</param>
        public void Initialize(Rendering rendering, SearchResults products, IEnumerable<CommerceQuerySort> sortFields, CommerceSearchOptions searchOptions)
        {
            base.Initialize(rendering);

            int itemsPerPage = (searchOptions != null) ? searchOptions.NumberOfItemsToReturn : 0;

            if (products != null)
            {
                ChildProducts = new List<ProductViewModel>();
                foreach (var child in products.SearchResultItems)
                {
                    var productModel = new ProductViewModel(child);
                    productModel.Initialize(this.Rendering);
                    this.ChildProducts.Add(productModel);
                }

                ChildProductFacets = products.Facets;
                if (itemsPerPage > products.SearchResultItems.Count)
                {
                    itemsPerPage = products.SearchResultItems.Count;
                }

                var alreadyShown = products.CurrentPageNumber * itemsPerPage;
                Pagination = new PaginationModel
                {
                    PageNumber = products.CurrentPageNumber,
                    TotalResultCount = products.TotalItemCount,
                    NumberOfPages = products.TotalPageCount,
                    PageResultCount = itemsPerPage,
                    StartResultIndex = alreadyShown + 1,
                    EndResultIndex = System.Math.Min(products.TotalItemCount, alreadyShown + itemsPerPage)
                };
            }

            SortFields = sortFields;
        }
        public virtual PaginationModel BuildPaginationModel(Func<int, string> generateUrl)
        {
            var pageCount = (int)Math.Ceiling(totalItemCount / (double)pageSize);
            var model = new PaginationModel { PageSize = this.pageSize, CurrentPage = this.currentPage, TotalItemCount = this.totalItemCount, PageCount = pageCount };

            // Previous
            model.PaginationLinks.Add(currentPage > 1 ? new PaginationLink { Active = true, DisplayText = "«", PageIndex = currentPage - 1, Url = generateUrl(currentPage - 1) } : new PaginationLink { Active = false, DisplayText = "«" });

            var start = 1;
            var end = pageCount;
            var nrOfPagesToDisplay = this.pagerOptions.MaxNrOfPages;

            if (pageCount > nrOfPagesToDisplay)
            {
                var middle = (int)Math.Ceiling(nrOfPagesToDisplay / 2d) - 1;
                var below = (currentPage - middle);
                var above = (currentPage + middle);

                if (below < 2)
                {
                    above = nrOfPagesToDisplay;
                    below = 1;
                }
                else if (above > (pageCount - 2))
                {
                    above = pageCount;
                    below = (pageCount - nrOfPagesToDisplay + 1);
                }

                start = below;
                end = above;
            }

            if (start > 1)
            {
                model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = 1, DisplayText = "1", Url = generateUrl(1) });
                if (start > 3)
                {
                    model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = 2, DisplayText = "2", Url = generateUrl(2) });
                }
                if (start > 2)
                {
                    model.PaginationLinks.Add(new PaginationLink { Active = false, DisplayText = "...", IsSpacer = true });
                }
            }

            for (var i = start; i <= end; i++)
            {
                if (i == currentPage || (currentPage <= 0 && i == 1))
                {
                    model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = i, IsCurrent = true, DisplayText = i.ToString() });
                }
                else
                {
                    model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = i, DisplayText = i.ToString(), Url = generateUrl(i) });
                }
            }
            if (end < pageCount)
            {
                if (end < pageCount - 1)
                {
                    model.PaginationLinks.Add(new PaginationLink { Active = false, DisplayText = "...", IsSpacer = true });
                }
                if (pageCount - 2 > end)
                {
                    model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = pageCount - 1, DisplayText = (pageCount - 1).ToString(), Url = generateUrl(pageCount - 1) });
                }

                model.PaginationLinks.Add(new PaginationLink { Active = true, PageIndex = pageCount, DisplayText = pageCount.ToString(), Url = generateUrl(pageCount) });
            }

            // Next
            model.PaginationLinks.Add(currentPage < pageCount ? new PaginationLink { Active = true, PageIndex = currentPage + 1, DisplayText = "»", Url = generateUrl(currentPage + 1) } : new PaginationLink { Active = false, DisplayText = "»" });

            // AjaxOptions
            if (pagerOptions.AjaxOptions != null)
            {
                model.AjaxOptions = pagerOptions.AjaxOptions;
            }

            model.Options = pagerOptions;
            return model;
        }