protected override void Render(System.Web.UI.HtmlTextWriter writer) { writer.WriteLine(string.Format("<li class='{0}' {1}>", CssClass, Href.Length > 0 ? "href=\"" + Href.Replace("\"", "\\\"") + "\"" : "")); writer.WriteLine(Text); writer.WriteLine("</li>"); }
/// <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); }
/// <summary> /// 访问并呈现当前标签实例。 /// </summary> /// <param name="context">当前HTML标签上下文,包含当前HTML相关信息。</param> /// <param name="output">当前标签输出实例,用于呈现标签相关信息。</param> public override void Process(TagHelperContext context, TagHelperOutput output) { if (Data == null) { if (ViewContext.ViewData.Model is IPageEnumerable data) { Data = data; } } if (Data == null || Data.Pages == 0) { output.SuppressOutput(); return; } if (Href == null && Action == null && Page == null && Route == null) { var query = new Dictionary <string, string>(); foreach (var current in HttpContext.Request.Query) { query[current.Key] = current.Value; } query["pageindex"] = "$page;"; Href = $"?{string.Join("&", query.Select(x => $"{x.Key}={x.Value}"))}"; } if (Href != null) { _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"); var startIndex = GetRange(Data.PageIndex, Data.Pages, Factor, out var endIndex); if (Data.PageIndex > 1) { builder.InnerHtml.AppendHtml(CreateLink(Data.PageIndex - 1, Resources.PageTagHelper_LastPage, Resources.PageTagHelper_LastPage)); } if (startIndex > 1) { builder.InnerHtml.AppendHtml(CreateLink(1, "1")); } if (startIndex > 2) { builder.InnerHtml.AppendHtml("<li class=\"page-item\"><span>…</span></li>"); } for (var i = startIndex; i < endIndex; i++) { builder.InnerHtml.AppendHtml(CreateLink(i, i.ToString())); } if (endIndex < Data.Pages) { builder.InnerHtml.AppendHtml("<li class=\"page-item\"><span>…</span></li>"); } if (endIndex <= Data.Pages) { builder.InnerHtml.AppendHtml(CreateLink(Data.Pages, Data.Pages.ToString())); } if (Data.PageIndex < Data.Pages) { builder.InnerHtml.AppendHtml(CreateLink(Data.PageIndex + 1, Resources.PageTagHelper_NextPage, Resources.PageTagHelper_NextPage)); } output.TagName = "ul"; output.MergeAttributes(builder); output.Content.AppendHtml(builder.InnerHtml); }
/// <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); }