Exemplo n.º 1
0
 protected abstract ActionResult ExecuteView <TModel>(
     TModel model,
     List <TItemView> objectsToView,
     ListViewOptions listViewOptions,
     PagedView pages,
     InfoCount infoCount
     ) where TModel : class;
Exemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="query"></param>
        /// <param name="pages"></param>
        /// <param name="infoCount"></param>
        /// <param name="listViewOptions"></param>
        /// <param name="IdPage">Если не задан, то отображается весь список объектов</param>
        /// <returns></returns>
        protected virtual List <TItemView> ViewPrepareItems(
            IQueryable <TItemSource> query,
            out PagedView pages,
            out InfoCount infoCount,
            ListViewOptions listViewOptions,
            int?IdPage = null)
        {
            pages     = null;
            infoCount = null;

            var startPosition = 0;

            var itemsCount = query.Count();

            if (IdPage.HasValue)
            {
                /*
                 * Рассчитываем страницы
                 * */
                pages = new PagedView();

                //Выбираем из настроек или из listViewOptions количество объектов на одной странице.
                var numberPerPage = listViewOptions != null && listViewOptions.numpage != 0 ? listViewOptions.numpage : 10;

                //Считаем текущую страницу. Первая страница начинается с 1.
                var currentPage = Math.Max(1, IdPage.Value);

                //Считаем стартовую позицию в списке объектов.
                startPosition = (currentPage - 1) * numberPerPage;

                //Считаем количество страниц.
                var countPages = itemsCount == 0 ? 0 : Math.Max(1, (int)Math.Ceiling(1.0 * itemsCount / numberPerPage));

                if (startPosition >= itemsCount)
                {
                    if (itemsCount == 0 && currentPage == 1)
                    {
                    }
                    else
                    {
                        pages.PageFound = false;
                        return(null);
                    }
                }

                var stpages      = new Dictionary <int, int>();
                var currentPage2 = currentPage - 1;

                if (currentPage2 > 3)
                {
                    for (int i = currentPage2 - 1; i <= currentPage2; i++)
                    {
                        stpages[i] = i;
                    }
                }
                else
                {
                    for (int i = 1; i <= currentPage2; i++)
                    {
                        stpages[i] = i;
                    }
                }

                var fnpages = new Dictionary <int, int>();
                if (currentPage2 < (countPages - 3))
                {
                    for (int i = currentPage2 + 2; i <= currentPage2 + 3; i++)
                    {
                        fnpages[i] = i;
                    }
                }
                else
                {
                    for (int i = currentPage2 + 2; i <= countPages; i++)
                    {
                        fnpages[i] = i;
                    }
                }

                pages.pages   = countPages;
                pages.curpage = currentPage;
                pages.stpg    = stpages;
                pages.fnpg    = fnpages;
                pages.np      = countPages - 3;

                infoCount = new InfoCount()
                {
                    all   = itemsCount,
                    start = startPosition + 1,
                    page  = currentPage * numberPerPage,
                    objectsPerPageTheory = numberPerPage,
                    objectsPerPage       = Math.Min(itemsCount - (currentPage - 1) * numberPerPage, numberPerPage)
                };
            }
            else
            {
                infoCount = new InfoCount()
                {
                    all   = itemsCount,
                    start = 1,
                    page  = 0,
                    objectsPerPageTheory = itemsCount,
                    objectsPerPage       = itemsCount
                };
            }

            //if (listViewOptions != null && listViewOptions.skip.HasValue) startPosition += listViewOptions.skip.Value;
            var querySorted = listViewOptions.BuildSortQuery(query);

            var list = querySorted.
                       Skip(infoCount.start - 1).
                       Take(infoCount.objectsPerPage).
                       OrderBy(x => x.RowNumber).
                       ToList().
                       Select(x => GetItemSourceToViewConverter()(x.Row)).ToList();

            return(list);
        }