Exemplo n.º 1
0
        private static void SetDefaultValue(PagerBarSettings settings)
        {
            if (settings.barStyle == BarStyle.defaultStyle)
            {
                settings.barStyle = BarStyle.quotes;
            }

            if (settings.PageIndex == 0)
            {
                settings.PageIndex = 1;
            }

            if (settings.ShowPageNumber == 0)
            {
                settings.ShowPageNumber = 3;
            }

            if (string.IsNullOrEmpty(settings.PreWord))
            {
                settings.PreWord = "上一页";
            }

            if (string.IsNullOrEmpty(settings.NextWord))
            {
                settings.NextWord = "下一页";
            }

            if (settings.PageCount == 0)
            {
                settings.PageCount = 20;
            }

            if (string.IsNullOrEmpty(settings.CurrentPageWordFormat))
            {
                settings.CurrentPageWordFormat = "{0}";
            }

        }
Exemplo n.º 2
0
        public static string PagerBar(this HtmlHelper html, PagerBarSettings settings)
        {
            SetDefaultValue(settings);//设置默认值

            StringBuilder sb = new StringBuilder();

            sb.AppendFormat("<div class=\"{0}\" >", settings.barStyle.ToString());

            //判断空记录
            if (settings.TotalCount == 0)
            {
                sb.Append(settings.NoRecordTip);
                sb.Append("</div>");

                return(sb.ToString());
            }

            int totalPage = Convert.ToInt32((settings.TotalCount - 1) / settings.PageCount) + 1;//总页数

            string backPageStyle = (settings.PageIndex <= 1) ? "disabled" : "";
            string nextPageStyle = (settings.PageIndex >= totalPage) ? "disabled" : "";

            var firstDisplayPageEnd  = 0; //从第1页显示到xx页
            var bodyDisplayPageStart = 0; //当前页临近最左页码
            var bodyDisplayPageEnd   = 0; //当前页临近最右页码
            var endDisplayPageStart  = 0; //从第xx页显示到最后一页


            //设定 bodyDisplayPageStart
            bodyDisplayPageStart = (settings.PageIndex - settings.ShowPageNumber <= 1) ? 1 : settings.PageIndex - settings.ShowPageNumber; // (ViewData.pageIndex - ViewData.showPageNumber <= ViewData.showPageNumber) ? ViewData.showPageNumber + 1 : ViewData.pageIndex - ViewData.showPageNumber;


            //设定 bodyDisplayPageEnd
            bodyDisplayPageEnd = (settings.PageIndex + settings.ShowPageNumber >= totalPage) ? totalPage : settings.PageIndex + settings.ShowPageNumber;


            //设定 firstDisplayPageEnd
            if (bodyDisplayPageStart > 1)
            {
                if (bodyDisplayPageStart - settings.ShowPageNumber <= 1)
                {
                    firstDisplayPageEnd = bodyDisplayPageStart - 1;
                }
                else
                {
                    firstDisplayPageEnd = settings.ShowPageNumber;
                }
            }
            else
            {
                firstDisplayPageEnd = 0;
            }

            //设定 endDisplayPageStart
            if (bodyDisplayPageEnd < totalPage)
            {
                if (bodyDisplayPageEnd + settings.ShowPageNumber >= totalPage)
                {
                    endDisplayPageStart = bodyDisplayPageEnd + 1;
                }
                else
                {
                    endDisplayPageStart = totalPage - settings.ShowPageNumber + 1;
                }
            }
            else
            {
                endDisplayPageStart = totalPage + 1;
            }
            //页面参数设定结束


            //开始输出

            // 上一条
            if (settings.PageIndex <= 1)
            {
                sb.Append("<span class=\"").Append(backPageStyle).Append("\">").Append(settings.PreWord).Append("</span>");
            }
            else
            {
                sb.Append(GetPageLink(settings.PageIndex - 1, settings.PageIndex, settings.PreWord, settings.CurrentPageWordFormat, settings.Onclick, settings.Url, settings.BarMark, settings.InIframe));
            }


            //first
            for (var i = 1; i <= firstDisplayPageEnd; i++)
            {
                sb.Append(GetPageLink(i, settings.PageIndex, i.ToString(), settings.CurrentPageWordFormat, settings.Onclick, settings.Url, settings.BarMark, settings.InIframe));
            }

            //省略号
            if (firstDisplayPageEnd + 1 < bodyDisplayPageStart)
            {
                sb.Append("<span>... </span>");
            }

            //body
            for (var i = bodyDisplayPageStart; i <= bodyDisplayPageEnd; i++)
            {
                sb.Append(GetPageLink(i, settings.PageIndex, i.ToString(), settings.CurrentPageWordFormat, settings.Onclick, settings.Url, settings.BarMark, settings.InIframe));
            }

            //省略号
            if (bodyDisplayPageEnd + 1 < endDisplayPageStart)
            {
                sb.Append("<span>... </span>");
            }

            //end
            for (var i = endDisplayPageStart; i <= totalPage; i++)
            {
                sb.Append(GetPageLink(i, settings.PageIndex, i.ToString(), settings.CurrentPageWordFormat, settings.Onclick, settings.Url, settings.BarMark, settings.InIframe));
            }

            // 下一条
            if (settings.PageIndex >= totalPage)
            {
                sb.Append("<span class=\"").Append(nextPageStyle).Append("\">").Append(settings.NextWord).Append("</span>");
            }
            else
            {
                sb.Append(GetPageLink(settings.PageIndex + 1, settings.PageIndex, settings.NextWord, settings.CurrentPageWordFormat, settings.Onclick, settings.Url, settings.BarMark, settings.InIframe));
            }

            //总数
            if (settings.ShowTotalCount)
            {
                int recordStart = (settings.PageIndex - 1) * settings.PageCount + 1;
                int recordEnd   = recordStart + settings.PageCount - 1;
                if (recordEnd > settings.TotalCount)
                {
                    recordEnd = settings.TotalCount;//如果超出最大值,则修正为最大值
                }
                sb.Append("<span class=\"total\" title=\"当前显示第" + (recordStart == recordEnd ? recordStart.ToString() : (recordStart + "-" + recordEnd)) + "条\">总共 <b>" + settings.TotalCount.ToString() + "</b> 条</span>");
            }

            sb.Append("</div>");

            return(sb.ToString());
        }
Exemplo n.º 3
0
 public static string PagerBar <T>(this HtmlHelper html, PagedList <T> dataSource, PagerBarSettings settings) where T : class
 {
     settings            = settings ?? new PagerBarSettings();
     settings.PageIndex  = dataSource.PageIndex;
     settings.PageCount  = dataSource.PageCount;
     settings.TotalCount = dataSource.TotalCount;
     return(PagerBar(html, settings));
 }