コード例 #1
0
ファイル: PagerTagHelper.cs プロジェクト: lovachen/FastCore
        /// <summary>
        ///
        /// </summary>
        /// <param name="context"></param>
        /// <param name="output"></param>
        public override void Process(TagHelperContext context, TagHelperOutput output)
        {
            //分页model为空
            if (Paging == null)
            {
                return;
            }
            //没有数据
            if (Paging.TotalCount <= 0)
            {
                return;
            }
            string routeName = Paging.RouteName;
            string controller = "", action = "";

            if (String.IsNullOrEmpty(routeName))
            {
                if (ViewContext.ActionDescriptor.AttributeRouteInfo != null)
                {
                    if (!String.IsNullOrEmpty(ViewContext.ActionDescriptor.AttributeRouteInfo.Name))
                    {
                        routeName = ViewContext.ActionDescriptor.AttributeRouteInfo.Name;
                    }
                }
            }

            if (String.IsNullOrEmpty(routeName))
            {
                controller = ViewContext.ActionDescriptor.RouteValues["controller"];
                action     = ViewContext.ActionDescriptor.RouteValues["action"];
            }

            IUrlHelper urlHelper = new UrlHelperFactory().GetUrlHelper(ViewContext);
            //连接地址
            string urlString = null;

            if (!String.IsNullOrEmpty(routeName))
            {
                urlString = urlHelper.RouteUrl(routeName, Paging.RouteArg);
            }
            else
            {
                urlString = urlHelper.Action(action, controller, Paging.RouteArg);
            }
            urlString = urlString.Any(o => o == '?') ? urlString + "&page={0}&size={1}" : urlString + "?page={0}&size={1}";
            StringBuilder sb = new StringBuilder();

            //默认显示最多7个连续按钮
            int display = 7;

            int minDisplay = 1;
            int maxDisplay = 7;

            if (Paging.TotalPages > display)
            {
                if (Paging.PageIndex + display / 2 >= Paging.TotalPages)
                {
                    maxDisplay = Paging.TotalPages;
                    minDisplay = Paging.TotalPages - display;
                }
                else if (Paging.PageIndex > display / 2)
                {
                    minDisplay = Paging.PageIndex - display / 2;
                    maxDisplay = Paging.PageIndex + display / 2;
                }
            }
            else
            {
                minDisplay = 1;
                maxDisplay = Paging.TotalPages;
            }

            string href = Ajax ? "href=\"javascript:\" data-href" : "href";

            sb.Append("<nav><ul class=\"pagination\">");

            sb.AppendFormat($"<li class=\"disabled\"><a>当前{Paging.PageIndex}/{Paging.TotalPages}页 共{Paging.TotalCount}条</a></li>");

            #region  一页

            if (Paging.HasPreviousPage)
            {
                sb.AppendFormat($"<li><a {href}=\"{String.Format(urlString, Paging.PageIndex - 1, Paging.PageSize)}\">上一页</a></li>");
            }
            else
            {
                sb.Append("<li class=\"disabled\"><a href=\"javascript: \">上一页</a></li>");
            }

            #endregion

            if (Paging.PageIndex > display / 2 + 2)
            {
                sb.Append($"<li><a {href} = \"{String.Format(urlString, 1, Paging.PageSize)}\" >1</a></li>");
                sb.Append("<li><a href=\"javascript:\">...</a></li>");
            }
            //else if (Paging.PageIndex == display / 2 + 2)
            //{
            //    sb.Append($"<li><a {href}=\"{String.Format(urlString, 1, Paging.PageSize)}\">1</a></li>");
            //}
            for (int i = minDisplay; i <= maxDisplay; i++)
            {
                if (i == Paging.PageIndex)
                {
                    sb.Append($"<li class=\"active\"><a href = \"javascript:\" >{i}</a></li>");
                }
                else
                {
                    sb.Append($"<li><a {href} = \"{String.Format(urlString, i, Paging.PageSize)}\">{i}</a></li>");
                }
            }
            if (maxDisplay + 1 < Paging.TotalPages)
            {
                sb.Append("<li><a href=\"javascript:\" >...</ a ></li>");
                sb.AppendFormat($"<li><a {href} = \"{String.Format(urlString, Paging.TotalPages, Paging.PageSize)}\" >{Paging.TotalPages}</a></li>");
            }
            else if (maxDisplay + 1 == Paging.TotalPages)
            {
                sb.Append($"<li><a {href}= \"{String.Format(urlString, Paging.TotalPages, Paging.PageSize)}\" >{Paging.TotalPages}</a></li>");
            }


            #region  一页

            if (Paging.HasNextPage)
            {
                sb.Append($"<li class=\"next\"><a {href}=\"{String.Format(urlString, Paging.PageIndex + 1, Paging.PageSize)}\">下一页</a></li>");
            }
            else
            {
                sb.Append("<li class=\"disabled\"><a href=\"javascript:\">下一页</a></li>");
            }
            #endregion

            sb.Append("<li>&nbsp;&nbsp;</li>");

            #region 每页条数选择按钮
            sb.Append("<li> ");
            sb.Append("<div class=\"btn-group\">");
            sb.Append("<button type=\"button\" class=\"btn btn-white dropdown-toggle btn-sm\" style=\"padding:6px 10px;\" data-toggle=\"dropdown\">");
            sb.AppendFormat("每页{0}条 <span class=\"ace-icon fa fa-caret-down icon-on-right\"></span>", Paging.PageSize);
            sb.Append("</button>");
            sb.Append("<ul class=\"dropdown-menu\" role=\"menu\">");
            sb.Append($"<li><a {href}=\"{String.Format(urlString, Paging.PageIndex, 20)}\">每页20条</a></li>");
            sb.Append($"<li><a {href}=\"{String.Format(urlString, Paging.PageIndex, 30)}\">每页30条</a></li>");
            sb.Append($"<li><a {href}=\"{String.Format(urlString, Paging.PageIndex, 50)}\">每页50条</a></li>");
            sb.Append($"<li><a {href}=\"{String.Format(urlString, Paging.PageIndex, 100)}\">每页100条</a></li>");
            sb.Append("</ul>");

            sb.Append("</div>");
            sb.Append("</li>");
            #endregion

            sb.Append("</ul></nav>");

            #region jquery操作

            if (Ajax)
            {
                sb.Append("<script>");
                sb.Append("$(function(){" +
                          "$('.pagination a[data-href]').click(function(e){" +
                          "e.preventDefault();" +
                          "layer.load(2);" +
                          "var href = $(this).data('href');" +
                          "$.ajax({" +
                          "url:href," +
                          "type:'POST'," +
                          "success:function(html){" +
                          "$('" + DataBox + "').html(html);" +
                          "$('html').scrollTop(0)}," +
                          "error:function(){" +
                          "layer.msg('请求数据错误');" +
                          "},complete:function(){" +
                          "layer.closeAll();" +
                          "}" +
                          "});" +
                          "});" +
                          "})");
                sb.Append("</script>");
            }

            #endregion

            output.Content.SetHtmlContent(sb.ToString());
        }
コード例 #2
0
        /// <summary>
        /// 分页方法
        /// </summary>
        /// <param name="html"></param>
        /// <param name="routeName">使用的路由名称</param>
        /// <param name="routeValues">使用的路由的值</param>
        /// <param name="option">分页选项</param>
        /// <returns></returns>
        public static HtmlString Pager(this IHtmlHelper html, string routeName, RouteValueDictionary routeValues, IPagerOption option)
        {
            //初始化
            var routeData = html.ViewContext.RouteData;

            if (routeData == null)
            {
                return(HtmlString.Empty);
            }
            var url = new UrlHelperFactory().GetUrlHelper(new ActionContext(html.ViewContext.HttpContext, routeData, html.ViewContext.ActionDescriptor));

            if (routeValues == null)
            {
                routeValues = new RouteValueDictionary();
            }
            //计算分页信息
            var pageCount = option.DataCount / option.PageSize;

            if (option.DataCount % option.PageSize != 0)
            {
                pageCount += 1;
            }
            var strs = new StringBuilder();

            if (option.PageIndex < 1)
            {
                option.PageIndex = 1;
            }
            else if (option.PageIndex > pageCount)
            {
                option.PageIndex = pageCount;
            }
            if (pageCount <= 1)
            {
                return(HtmlString.Empty);
            }
            strs.AppendFormat("<ul class='{0}'>", option.PagerClassName);
            if (option.ShowDataCount)
            {
                strs.AppendFormat("<li><span class='{1}'>{0}</span></li>", string.Format(option.DataCountTextFormat, option.DataCount), option.PagerStatusClassName);
            }
            //添加分页状态
            if (option.ShowPagerStatus)
            {
                strs.AppendFormat("<li><span class='{0}'>{1}</span></li>", option.PagerStatusClassName, string.Format(option.PagerStatusTextFormat, option.PageIndex, pageCount));
            }
            //判断路由值中是否包含页码参数,不存在就添加
            if (routeValues.ContainsKey(option.PageIndexParamName))
            {
                routeValues[option.PageIndexParamName] = 1;
            }
            else
            {
                routeValues.Add(option.PageIndexParamName, 1);
            }
            //添加首页按钮
            if (option.ShowFirstLastPageButton)
            {
                if (option.PageIndex != 1)
                {
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", url.RouteUrl(routeName, routeValues), option.FirstPageButtonText, option.FirstPageButtonClassName);
                }
                else
                {
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", "javascript:void(0)", option.FirstPageButtonText, option.FirstPageButtonClassName);
                }
            }
            //计算按钮分组
            int btnGroupNum = pageCount / option.PageButtonCount;//按钮分组的个数

            if (pageCount % option.PageButtonCount != 0)
            {
                btnGroupNum += 1;
            }
            int btnGroupIndex = option.PageIndex / option.PageButtonCount;//当前按钮分组的索引

            if (option.PageIndex % option.PageButtonCount != 0)
            {
                btnGroupIndex += 1;
            }
            //添加上一组按钮
            if (option.ShowPreNextGroupButton)
            {
                if (btnGroupIndex > 1)
                {
                    int preGroupIndex    = btnGroupIndex - 1;
                    int preGroupBtnStart = preGroupIndex * option.PageButtonCount;
                    routeValues[option.PageIndexParamName] = preGroupBtnStart;
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", url.RouteUrl(routeName, routeValues), option.PreGroupButtonText, option.PreGroupButtonClassName);
                }
                else
                {
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", "javascript:void(0)", option.PreGroupButtonText, option.PreGroupButtonClassName);
                }
            }
            //添加上一页按钮
            if (option.ShowPreNextPageButton)
            {
                if (option.PageIndex > 1)
                {
                    routeValues[option.PageIndexParamName] = option.PageIndex > 1 ? option.PageIndex - 1 : option.PageIndex;
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", url.RouteUrl(routeName, routeValues), option.PrePageButtonText, option.PrePageButtonClassName);
                }
                else
                {
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", "javascript:void(0)", option.PrePageButtonText, option.PrePageButtonClassName);
                }
            }

            //添加数字按钮
            var btnStart = option.PageIndex - option.PageButtonCount / 2;

            if (btnStart <= 0)
            {
                btnStart = 1;
            }
            if (!option.CurrentPageInCenter)
            {
                btnStart = ((btnGroupIndex - 1) * option.PageButtonCount) + 1;
            }
            var btnEnd = btnStart + option.PageButtonCount;

            for (int i = btnStart; i < btnEnd; i++)
            {
                if (i <= pageCount)
                {
                    routeValues[option.PageIndexParamName] = i;
                    if (i == option.PageIndex)
                    {
                        strs.AppendFormat("<li class='{2}'><a href='{0}' class='{2}'>{1}</a></li>", url.RouteUrl(routeName, routeValues), i, option.CurrentPageClassName);
                    }
                    else
                    {
                        strs.AppendFormat("<li><a href='{0}'>{1}</a></li>", url.RouteUrl(routeName, routeValues), i);
                    }
                }
            }

            //添加下一页按钮
            if (option.ShowPreNextPageButton)
            {
                if (option.PageIndex < pageCount)
                {
                    routeValues[option.PageIndexParamName] = option.PageIndex + 1 > pageCount ? pageCount : option.PageIndex + 1;
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", url.RouteUrl(routeName, routeValues), option.NextPageButtonText, option.NextPageButtonClassName);
                }
                else
                {
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", "javascript:void(0)", option.NextPageButtonText, option.NextPageButtonClassName);
                }
            }
            //添加下一组按钮
            if (option.ShowPreNextGroupButton)
            {
                if (btnGroupIndex < btnGroupNum)
                {
                    int nextGroupIndex    = btnGroupIndex + 1;
                    int nextGroupBtnStart = ((nextGroupIndex - 1) * option.PageButtonCount) + 1;
                    routeValues[option.PageIndexParamName] = nextGroupBtnStart;
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", url.RouteUrl(routeName, routeValues), option.NextGroupButtonText, option.NextGroupButtonClassName);
                }
                else
                {
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", "javascript:void(0)", option.NextGroupButtonText, option.NextGroupButtonClassName);
                }
            }
            //添加尾页按钮
            if (option.ShowFirstLastPageButton)
            {
                if (option.PageIndex < pageCount)
                {
                    routeValues[option.PageIndexParamName] = pageCount;
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", url.RouteUrl(routeName, routeValues), option.LastPageButtonText, option.LastPageButtonClassName);
                }
                else
                {
                    strs.AppendFormat("<li><a class='{2}' href='{0}'>{1}</a></li>", "javascript:void(0)", option.LastPageButtonText, option.LastPageButtonClassName);
                }
            }
            //添加跳转区域
            if (option.ShowGotoPanel)
            {
                var    txtId    = Guid.NewGuid().ToString();
                string funcName = "func_" + Guid.NewGuid().ToString().Replace("-", "");
                strs.AppendFormat("<li><input type='text' class='{0}' id='{1}'/>", option.GotoPageInputClassName, txtId);
                strs.AppendFormat("<a class='{0}' href='javascript:{2}();'>{1}</a></li>", option.GotoPageButtonClassName, option.GotoPageBtuttonText, funcName);
                string js = @"<script type='text/javascript'>
                                        function funcName()
                                        {
                                          var pageIndex=document.getElementById('{0}').value;
                                          if(pageIndex.length==0 || !/[1-9][0-9]*/.test(pageIndex))return;
                                          var url=location.toString().toLowerCase();
                                          if (url.indexOf('{1}')<0) {
                                                if (url.indexOf('?')<0) {
                                                    url+='?{1}='+pageIndex;
                                                }
                                                else {
                                                    url+='&{1}='+pageIndex;
                                                }
                                          }
                                          else
                                          {
                                            var reg=/{1}=-?[0-9]+/;
                                            url=url.replace(reg,'{1}='+pageIndex);
                                          }
                                           location=url;
                                        }
                                        </script>";
                js = js.Replace("funcName", funcName).Replace("{0}", txtId).Replace("{1}", option.PageIndexParamName.ToLower());
                strs.Append(js);
            }
            //结束
            strs.Append("</ul>");
            return(new HtmlString(strs.ToString()));
        }