示例#1
0
        /// <summary>
        /// 访问并呈现当前标签实例。
        /// </summary>
        /// <param name="context">当前HTML标签上下文,包含当前HTML相关信息。</param>
        /// <param name="output">当前标签输出实例,用于呈现标签相关信息。</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            Data = Data ?? ViewContext.ViewData.Model as IPageEnumerable;
            if (Data == null || Data.Pages == 0)
            {
                output.SuppressOutput();
                return;
            }
            if (context.AllAttributes.ContainsName(HrefAttributeName))
            {
                _createAnchor = page =>
                {
                    var tagBuilder = new TagBuilder("a");
                    tagBuilder.MergeAttribute("href", Href.Replace("$page;", page.ToString()));
                    return(tagBuilder);
                };
            }
            else
            {
                IDictionary <string, object> routeValues = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);
                if (_routeValues != null && _routeValues.Count > 0)
                {
                    foreach (var routeValue in _routeValues)
                    {
                        routeValues.Add(routeValue.Key, routeValue.Value);
                    }
                }

                if (Area != null)
                {
                    routeValues["area"] = Area;
                }

                if (Route == null)
                {
                    _createAnchor = page =>
                    {
                        routeValues["page"] = page;
                        return(Generator.GenerateActionLink(
                                   ViewContext,
                                   linkText: string.Empty,
                                   actionName: Action,
                                   controllerName: Controller,
                                   protocol: Protocol,
                                   hostname: Host,
                                   fragment: Fragment,
                                   routeValues: routeValues,
                                   htmlAttributes: null));
                    };
                }
                else
                {
                    _createAnchor = page =>
                    {
                        routeValues["page"] = page;
                        return(Generator.GenerateRouteLink(
                                   ViewContext,
                                   linkText: string.Empty,
                                   routeName: Route,
                                   protocol: Protocol,
                                   hostName: Host,
                                   fragment: Fragment,
                                   routeValues: routeValues,
                                   htmlAttributes: null));
                    };
                }
            }

            var builder = new TagBuilder("ul");

            builder.AddCssClass("pagination");
            if (!Border)
            {
                builder.AddCssClass("borderless");
            }
            int endIndex;
            var startIndex = Cores.GetRange(Data.Page, Data.Pages, Factor, out endIndex);

            if (Data.Page > 1)
            {
                builder.InnerHtml.AppendHtml(CreateLink(Data.Page - 1, Resources.LastPage, title: Resources.LastPage));
            }
            if (startIndex > 1)
            {
                builder.InnerHtml.AppendHtml(CreateLink(1, "1"));
            }
            if (startIndex > 2)
            {
                builder.InnerHtml.AppendHtml("<li><span>…</span></li>");
            }
            for (int i = startIndex; i < endIndex; i++)
            {
                builder.InnerHtml.AppendHtml(CreateLink(i, i.ToString(), "active"));
            }
            if (endIndex < Data.Pages)
            {
                builder.InnerHtml.AppendHtml("<li><span>…</span></li>");
            }
            if (endIndex <= Data.Pages)
            {
                builder.InnerHtml.AppendHtml(CreateLink(Data.Pages, Data.Pages.ToString()));
            }
            if (Data.Page < Data.Pages)
            {
                builder.InnerHtml.AppendHtml(CreateLink(Data.Page + 1, Resources.NextPage, title: Resources.NextPage));
            }
            output.TagName = "ul";
            output.MergeAttributes(builder);
            output.Content.AppendHtml(builder.InnerHtml);
        }
示例#2
0
        /// <summary>
        /// 访问并呈现当前标签实例。
        /// </summary>
        /// <param name="context">当前HTML标签上下文,包含当前HTML相关信息。</param>
        /// <param name="output">当前标签输出实例,用于呈现标签相关信息。</param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            Data = Data ?? ViewContext.ViewData.Model as IPageEnumerable;
            if (Data == null || Data.Pages == 0)
            {
                output.SuppressOutput();
                return;
            }
            if (context.AllAttributes.ContainsName(HrefAttributeName))
            {
                _createAnchor = page =>
                {
                    var tagBuilder = new TagBuilder("a");
                    tagBuilder.MergeAttribute("href", Href.Replace("$page;", page.ToString()));
                    return(tagBuilder);
                };
            }
            else
            {
                _createAnchor = GenerateActionLink();
            }

            var builder = new TagBuilder("ul");

            builder.AddCssClass("pagination");
            if (!Border)
            {
                builder.AddCssClass("borderless");
            }
            int endIndex;
            var startIndex = Cores.GetRange(Data.Page, Data.Pages, Factor, out endIndex);

            if (Data.Page > 1)
            {
                builder.InnerHtml.AppendHtml(CreateLink(Data.Page - 1, Resources.LastPage, Resources.LastPage));
            }
            if (startIndex > 1)
            {
                builder.InnerHtml.AppendHtml(CreateLink(1, "1"));
            }
            if (startIndex > 2)
            {
                builder.InnerHtml.AppendHtml("<li><span>…</span></li>");
            }
            for (int i = startIndex; i < endIndex; i++)
            {
                builder.InnerHtml.AppendHtml(CreateLink(i, i.ToString()));
            }
            if (endIndex < Data.Pages)
            {
                builder.InnerHtml.AppendHtml("<li><span>…</span></li>");
            }
            if (endIndex <= Data.Pages)
            {
                builder.InnerHtml.AppendHtml(CreateLink(Data.Pages, Data.Pages.ToString()));
            }
            if (Data.Page < Data.Pages)
            {
                builder.InnerHtml.AppendHtml(CreateLink(Data.Page + 1, Resources.NextPage, Resources.NextPage));
            }
            output.TagName = "ul";
            output.MergeAttributes(builder);
            output.Content.AppendHtml(builder.InnerHtml);
        }