PagerAjax跟Pager基本一样。不同点是在点击链接的时候Pager是转到相应页面,PagerAjax是在点击paer内的链接的时候利用jquery Post获取链接指定页面的html代码替换内容包裹容器的html。实际上就是这句jquery语句
复制代码 代码如下:
<script type="text/javascript">$("#ctrlId a").click(function () {$.post($(this).attr("href"), function (data) {$("#ctnrId").html(data);});return false; });</script>
PagerAjax的整个内容
public static MvcHtmlString PagerAjax(this HtmlHelper htmlHelper, string ctnrId, string actionName, string controllerName, RouteValueDictionary routeValues, PagerConfig pageConfig, string ctrlId, string cssClass, int digitalLinkNum, bool showTotalRecord, bool showCurrentPage, bool showTotalPage, bool showSelect, bool showInput) { UrlHelper _url = new UrlHelper(htmlHelper.ViewContext.RequestContext); StringBuilder _strBuilder = new StringBuilder("<div id=https://www.jb51.net/article/\"" + ctrlId + "https://www.jb51.net/article/\" class=https://www.jb51.net/article/\"" + cssClass + "https://www.jb51.net/article/\">"); if (showTotalRecord) _strBuilder.Append("共" + pageConfig.TotalRecord + pageConfig.RecordUnit + pageConfig.RecordName + " "); if (showCurrentPage) _strBuilder.Append("每页" + pageConfig.PageSize + pageConfig.RecordUnit + " "); if (showTotalPage) _strBuilder.Append("第" + pageConfig.CurrentPage + "页/共" + pageConfig.TotalPage + "页 "); //首页链接 if (pageConfig.CurrentPage > 1) { routeValues["page"] = 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbtn\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">首页</a>"); } else _strBuilder.Append("<span class=https://www.jb51.net/article/\"btn\">首页</span>"); //上一页 if (pageConfig.CurrentPage > 1) { routeValues["page"] = pageConfig.CurrentPage - 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbtn\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">上一页</a>"); } else _strBuilder.Append("<span class=https://www.jb51.net/article/\"btn\">上一页</span>"); //数字导航开始 int _startPage, _endPage; //总页数少于要显示的页数,页码全部显示 if (digitalLinkNum >= pageConfig.TotalPage) { _startPage = 1; _endPage = pageConfig.TotalPage; } else//显示指定数量的页码 { int _forward = (int)Math.Ceiling(digitalLinkNum / 2.0); if (pageConfig.CurrentPage > _forward)//起始页码大于1 { _endPage = pageConfig.CurrentPage + digitalLinkNum - _forward; if (_endPage > pageConfig.TotalPage)//结束页码大于总页码结束页码为最后一页 { _startPage = pageConfig.TotalPage - digitalLinkNum; _endPage = pageConfig.TotalPage; } else _startPage = pageConfig.CurrentPage - _forward; } else//起始页码从1开始 { _startPage = 1; _endPage = digitalLinkNum; } } //向上… if (_startPage > 1) { routeValues["page"] = _startPage - 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbatch\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">…</a>"); } //数字 for (int i = _startPage; i <= _endPage; i++) { if (i != pageConfig.CurrentPage) { routeValues["page"] = i; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linknum\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">" + i.ToString() + "</a>"); } else { _strBuilder.Append("<span>" + i.ToString() + "</span>"); } } //向下… if (_endPage < pageConfig.TotalPage) { routeValues["page"] = _endPage + 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbatch\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">…</a>"); } ////数字导航结束 //下一页和尾页 if (pageConfig.CurrentPage < pageConfig.TotalPage) { routeValues["page"] = pageConfig.CurrentPage + 1; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbtn\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">下一页</a>"); routeValues["page"] = pageConfig.TotalPage; _strBuilder.Append("<a class=https://www.jb51.net/article/\"linkbtn\" href=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">尾页</a>"); } else _strBuilder.Append("<span class=https://www.jb51.net/article/\"btn\">下一页</span><span class=https://www.jb51.net/article/\"btn\">尾页</span>"); //显示页码下拉框 if (showSelect) { routeValues["page"] = "-nspageselecturl-"; _strBuilder.Append(" 跳转到第<select id=https://www.jb51.net/article/\"nspagerselect\" data-url=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\">"); for (int i = 1; i <= pageConfig.TotalPage; i++) { if (i == pageConfig.CurrentPage) _strBuilder.Append("<option selected=https://www.jb51.net/article/\"selected\" value=https://www.jb51.net/article/\"" + i + "https://www.jb51.net/article/\">" + i + "</option>"); else _strBuilder.Append("<option value=https://www.jb51.net/article/\"" + i + "https://www.jb51.net/article/\">" + i + "</option>"); } _strBuilder.Append("</select>页"); _strBuilder.Append("<script type=https://www.jb51.net/article/\"text/javascript\">$(\"#" + ctrlId + " #nspagerselect\").change(function () {$.post($(\"#" + ctrlId + " #nspagerselect\").attr(\"data-url\").replace(\"-nspageselecturl-\", $(\"#" + ctrlId + " #nspagerselect\").val()), function (data) {$(\"#" + ctnrId + "https://www.jb51.net/article/\").html(data);});});</script>"); } //显示页码输入框 if (showInput) { routeValues["page"] = "-nspagenumurl-"; _strBuilder.Append(" 转到第<input id=https://www.jb51.net/article/\"nspagernum\" type=https://www.jb51.net/article/\"text\" data-url=https://www.jb51.net/article/\"" + _url.Action(actionName, controllerName, routeValues) + "https://www.jb51.net/article/\" />页"); _strBuilder.Append("<script type=https://www.jb51.net/article/\"text/javascript\">$(\"#" + ctrlId + " #nspagernum\").keydown(function (event) {if (event.keyCode == 13) { $.post($(\"#" + ctrlId + " #nspagernum\").attr(\"data-url\").replace(\"-nspagenumurl-\", $(\"#" + ctrlId + " #nspagernum\").val()), function (data) {$(\"#" + ctnrId + "https://www.jb51.net/article/\").html(data);}); } });</script>"); } _strBuilder.Append("<script type=https://www.jb51.net/article/\"text/javascript\">$(\"#" + ctrlId + " a\").click(function () {$.post($(this).attr(\"href\"), function (data) {$(\"#" + ctnrId + "https://www.jb51.net/article/\").html(data);});return false; });</script>"); _strBuilder.Append("</div>"); return MvcHtmlString.Create(_strBuilder.ToString()); }