示例#1
0
        public object Run(object obj, string[] parameters, IPropertyBag bag, IMarkupBase markup)
        {
            if (parameters == null || parameters.Length != 1)
                throw new ImpressionInterpretException("Filter " + Keyword + " expects 1 parameter.");

            var enumerable = obj as IEnumerable;
            if (enumerable == null)
                return null;

            int pageNumber = 1;
            object pageNumberObject = bag["Page.Number"];
            if (pageNumberObject != null) {
                Int32.TryParse(pageNumberObject.ToString(), out pageNumber);
                if (pageNumber < 1)
                    pageNumber = 1;
            }

            int pageSize = 12;
            string pageSizeString = parameters[0];
            object pageSizeObject = bag["Page.Size"];
            if (pageSizeObject != null) {
                pageSizeString = pageSizeObject.ToString();
            }
            Int32.TryParse(pageSizeString, out pageSize);
            if (pageSize < 1)
                pageSize = 12;

            int i = 0, start = (pageNumber - 1)*pageSize, end = (pageNumber*pageSize)-1;
            var list = new ModelListWithPages(pageSize);

            // add the "paged" items
            foreach(var item in enumerable) {
                if (i >= start && i <= end)
                        list.Add(item);

                i++;
            }

            // add the pages
            var totalPages = (int)Math.Ceiling(i / (double)pageSize);
            var totalPages0 = totalPages - 1;
            var pageNumber0 = pageNumber - 1;
            for (i = 0; i < totalPages; i++) {
                list.Pages.Add( new PageModel {
                    Current = (i == pageNumber0),
                    First = (i == 0),
                    Last = (i == (totalPages0)),
                    Number = (i+1),
                    Url = "?page=" + (i+1)
                } );
            }

            list.FirstPage = list.Pages[0];
            list.PrevPage = pageNumber != 1 ? list.Pages[pageNumber0-1]: null;
            list.CurrentPage = list.Pages[pageNumber0];
            list.NextPage = pageNumber != totalPages ? list.Pages[pageNumber]: null;
            list.LastPage = list.Pages[totalPages0];

            return list;
        }
示例#2
0
        public object Run(object obj, string[] parameters, IPropertyBag bag, IMarkupBase markup)
        {
            if (parameters == null || parameters.Length != 1)
            {
                throw new ImpressionInterpretException("Filter " + Keyword + " expects 1 parameter.");
            }

            var enumerable = obj as IEnumerable;

            if (enumerable == null)
            {
                return(null);
            }

            int    pageNumber       = 1;
            object pageNumberObject = bag["Page.Number"];

            if (pageNumberObject != null)
            {
                Int32.TryParse(pageNumberObject.ToString(), out pageNumber);
                if (pageNumber < 1)
                {
                    pageNumber = 1;
                }
            }

            int    pageSize       = 12;
            string pageSizeString = parameters[0];
            object pageSizeObject = bag["Page.Size"];

            if (pageSizeObject != null)
            {
                pageSizeString = pageSizeObject.ToString();
            }
            Int32.TryParse(pageSizeString, out pageSize);
            if (pageSize < 1)
            {
                pageSize = 12;
            }

            int i = 0, start = (pageNumber - 1) * pageSize, end = (pageNumber * pageSize) - 1;
            var list = new ModelListWithPages(pageSize);

            // add the "paged" items
            foreach (var item in enumerable)
            {
                if (i >= start && i <= end)
                {
                    list.Add(item);
                }

                i++;
            }

            // add the pages
            var totalPages  = (int)Math.Ceiling(i / (double)pageSize);
            var totalPages0 = totalPages - 1;
            var pageNumber0 = pageNumber - 1;

            for (i = 0; i < totalPages; i++)
            {
                list.Pages.Add(new PageModel {
                    Current = (i == pageNumber0),
                    First   = (i == 0),
                    Last    = (i == (totalPages0)),
                    Number  = (i + 1),
                    Url     = "?page=" + (i + 1)
                });
            }

            list.FirstPage   = list.Pages[0];
            list.PrevPage    = pageNumber != 1 ? list.Pages[pageNumber0 - 1]: null;
            list.CurrentPage = list.Pages[pageNumber0];
            list.NextPage    = pageNumber != totalPages ? list.Pages[pageNumber]: null;
            list.LastPage    = list.Pages[totalPages0];

            return(list);
        }