public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var urlHelper = UrlHelperFactory.GetUrlHelper(
                new ActionContext(ContextAccessor.HttpContext,
                                  new RouteData(),
                                  new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor()));

            var itemsPerPage = (ItemsPerPage ?? 0);

            if (itemsPerPage == 0)
            {
                int.TryParse(ViewOptions.PerPageDefault, out itemsPerPage);
            }

            var currentPage = (CurrentPage ?? 1);

            var totalItems = (TotalItems ?? 0);

            if (totalItems > itemsPerPage)
            {
                var totalPages = (totalItems + (itemsPerPage - 1)) / itemsPerPage;

                string controller  = String.IsNullOrEmpty(Controller) ? (ViewContext.ActionDescriptor as ControllerActionDescriptor)?.ControllerName : Controller;
                string action      = String.IsNullOrEmpty(Action) ? "Index" : Action;
                var    httpContext = ContextAccessor.HttpContext;

                var pageList = new List <string> {
                    "«",
                    "‹"
                };

                switch (totalPages)
                {
                case 2:
                    pageList.Add("1");
                    pageList.Add("2");
                    break;

                case 3:
                case 4:
                case 5:
                    for (var i = 1; i < totalPages; i++)
                    {
                        pageList.Add(i.ToString());
                    }
                    break;

                default:
                    if (currentPage < 4)
                    {
                        for (var i = 1; i < 6; i++)
                        {
                            pageList.Add(i.ToString());
                        }
                    }
                    else
                    {
                        var endPage = currentPage + 2 < totalPages ? currentPage + 3 : totalPages + 1;
                        for (var i = endPage - 5; i < endPage; i++)
                        {
                            pageList.Add(i.ToString());
                        }
                    }
                    break;
                }

                pageList.Add("›");
                pageList.Add("»");

                var list = new FluentTagBuilder("ul").AddCssClass("pagination pagination-sm")
                           .MergeAttribute("style", "display:inline;");

                dynamic routeValues = new ExpandoObject();
                foreach (var page in pageList)
                {
                    routeValues = new ExpandoObject();

                    if (page.IsNumeric())
                    {
                        routeValues = new { page, itemsPerPage }
                    }
                    ;
                    else if (page == "«")
                    {
                        routeValues = new { page = 1, itemsPerPage }
                    }
                    ;
                    else if (page == "‹")
                    {
                        routeValues = new {
                            page = currentPage - 1 > 0 ? currentPage - 1 : 1,
                            itemsPerPage
                        }
                    }
                    ;
                    else if (page == "›")
                    {
                        routeValues = new {
                            page = currentPage + 1 <= totalPages ? currentPage + 1 : totalPages,
                            itemsPerPage
                        }
                    }
                    ;
                    else if (page == "»")
                    {
                        routeValues = new {
                            page = totalPages,
                            itemsPerPage
                        }
                    }
                    ;
                    foreach (var obj in httpContext.Request.Query)
                    {
                        if (!obj.Key.Equals("page", StringComparison.CurrentCultureIgnoreCase))
                        {
                            routeValues[obj.Key] = obj.Value;
                        }
                    }

                    var li = new FluentTagBuilder("li")
                             .AppendHtml(Generator.GenerateActionLink(ViewContext, page, action, controller, string.Empty, string.Empty, string.Empty, routeValues, null));

                    if (page.IsNumeric())
                    {
                        if (Convert.ToInt32(page) == currentPage)
                        {
                            li.AddCssClass("active");
                        }
                    }
                    list.AppendHtml(li);
                }

                routeValues = new ExpandoObject();
                foreach (var obj in httpContext.Request.Query)
                {
                    if (!obj.Key.Equals("page", StringComparison.CurrentCultureIgnoreCase))
                    {
                        routeValues[obj.Key] = obj.Value;
                    }
                }

                var perPageSelect = new FluentTagBuilder("select")
                                    .GenerateId("ddlResultsPerPage", "")
                                    .MergeAttribute("style", "width:auto;float:left;margin-right:15px;")
                                    .MergeAttribute("data-url", urlHelper.Action(Action, Controller, (object)routeValues))
                                    .AddCssClass("itemCountDropdown form-control")
                                    .MergeAttribute("onchange", "javascript:window.location.href = $('.itemCountDropdown option:selected').attr('tag')");

                routeValues.Page = 1;

                foreach (var item in ViewOptions.PerPageList)
                {
                    routeValues.itemsPerPage = item.Value;
                    item.Selected            = item.Value == itemsPerPage.ToString();
                    var option = new FluentTagBuilder("option")
                                 .MergeAttribute("value", item.Value)
                                 .Append(item.Text)
                                 .MergeAttribute("tag", urlHelper.Action(action, controller, (object)routeValues));

                    if (item.Selected)
                    {
                        option.MergeAttribute("selected", "selected");
                    }

                    perPageSelect.AppendHtml(option);
                }

                output.PostElement.AppendHtml(perPageSelect);
                output.PostElement.AppendHtml(list);
            }
        }
    }
Beispiel #2
0
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            var modelMetadata = ViewContext.ViewData.ModelMetadata;

            var props = new List <ModelMetadata>();            //			GetProperties(modelMetadata);

            props.AddRange(MetadataProvider.GetMetadataForProperties(modelMetadata.ElementType ?? modelMetadata.ModelType));

            var columnName = string.Empty;

            if (!DisplayFor.Contains("."))
            {
                var chosen = props.SingleOrDefault(p => p.PropertyName == DisplayFor);
                if (chosen != null)
                {
                    output.Content.SetContent(chosen.GetDisplayName());
                    columnName = chosen.PropertyName;
                }
            }
            else
            {
                var propertyNames = DisplayFor.Split('.');
                foreach (var propertyName in propertyNames)
                {
                    modelMetadata = props.SingleOrDefault(p => p.PropertyName == propertyName);
                    if (modelMetadata == null)
                    {
                        break;
                    }
                    if (propertyName == propertyNames[propertyNames.Length - 1])
                    {
                        var displayName = modelMetadata.GetDisplayName();
                        if (String.IsNullOrEmpty(displayName))
                        {
                            displayName = propertyName;
                        }
                        output.Content.SetContent(displayName);
                        columnName = modelMetadata.PropertyName;
                    }
                    else
                    {
                        props.Clear();
                        props.AddRange(MetadataProvider.GetMetadataForProperties(modelMetadata.ElementType));
                    }
                }
            }

            if (Sort)
            {
                var urlHelper = UrlHelperFactory.GetUrlHelper(new ActionContext(ContextAccessor.HttpContext, new Microsoft.AspNetCore.Routing.RouteData(), new Microsoft.AspNetCore.Mvc.Abstractions.ActionDescriptor()));                 //ContextAccessor.HttpContext.RequestServices.GetRequiredService<IUrlHelper>();

                var itemsPerPage = 0;
                int.TryParse(ContextAccessor.HttpContext.Request.Query["itemsPerPage"], out itemsPerPage);

                if (itemsPerPage == 0)
                {
                    int.TryParse(ViewOptions.PerPageDefault, out itemsPerPage);
                }

                var currentPage = 1;
                int.TryParse(ContextAccessor.HttpContext.Request.Query["page"], out currentPage);
                if (currentPage == 0)
                {
                    currentPage = 1;
                }

                var controller = String.IsNullOrEmpty(Controller) ? (ViewContext.ActionDescriptor as ControllerActionDescriptor)?.ControllerName : Controller;

                var action = String.IsNullOrEmpty(Action) ? ViewContext.RouteData.Values["action"].ToString() : Action;
                if (string.IsNullOrEmpty(action))
                {
                    action = "Index";
                }

                dynamic routeValues = new ExpandoObject();
                foreach (var obj in ContextAccessor.HttpContext.Request.Query)
                {
                    ((System.Collections.Generic.IDictionary <string, object>)routeValues)[obj.Key] = obj.Value;
                }

                routeValues.sortColumn = columnName;
                routeValues.sortOrder  = SortOrder.Ascending;

                var ul = new FluentTagBuilder("ul")
                         .AddCssClass("sort-spinner")
                         .AppendHtml(new FluentTagBuilder("li")
                                     .AppendHtml(Generator.GenerateActionLink(ViewContext,
                                                                              string.Empty,
                                                                              action,
                                                                              controller,
                                                                              string.Empty,
                                                                              string.Empty,
                                                                              string.Empty,
                                                                              routeValues,
                                                                              new { @class = "fa fa-sort-asc" })));
                routeValues.sortOrder = SortOrder.Descending;
                ul.AppendHtml(new FluentTagBuilder("li")
                              .AppendHtml(Generator.GenerateActionLink(ViewContext,
                                                                       string.Empty,
                                                                       action,
                                                                       controller,
                                                                       string.Empty,
                                                                       string.Empty,
                                                                       string.Empty,
                                                                       routeValues,
                                                                       new { @class = "fa fa-sort-desc" })));

                output.Content.AppendHtml(ul);
            }
        }