MVC4 5分页控件,支持Ajax AjaxOption支持

 


MVC4 5分页控件,支持Ajax AjaxOption支持


///
<summary> /// MVC4 5分页控件,支持Ajax AjaxOption支持 beta 1.0 /// 用法 /// @Ajax.Pages /// @Html.Pages /// </summary> public static class PagesExtension { public static MvcHtmlString Pages(this HtmlHelper htmlHelper, int rowCount, int pageSize, string url, string option = "") { if (rowCount <= pageSize) { return new MvcHtmlString(""); } string str = string.IsNullOrEmpty(url) ? HttpContext.Current.Request.Url.AbsolutePath : url; string str2 = HttpContext.Current.Request.Url.Query.Trim(new char[] { ? }); StringBuilder builder = new StringBuilder(); string[] strArray = str2.Split(new char[] { & }); foreach (string str3 in strArray) { if ((str3.Length > 0) && !str3.StartsWith("page=")) { string[] strArray2 = str3.Split(new char[] { = }); if (strArray2.Length == 2) { builder.AppendFormat("&{0}={1}", strArray2[0], HttpContext.Current.Server.UrlEncode(strArray2[1])); } } } string htmlString = GetPaginationHtml(rowCount, pageSize, str, builder.ToString() + option); return new MvcHtmlString(htmlString); } public static MvcHtmlString Pages(this AjaxHelper ajaxHelper, int rowCount, int pageSize, string formAction, AjaxOptions ajaxOptions, string option = "") { if (rowCount <= pageSize) { return new MvcHtmlString(""); } //组装表单 TagBuilder tagBuilder = new TagBuilder("form"); tagBuilder.MergeAttributes<string, object>(HtmlHelper.AnonymousObjectToHtmlAttributes(null)); tagBuilder.MergeAttribute("action", formAction); tagBuilder.MergeAttribute("method", "post"); ajaxOptions = GetAjaxOptions(ajaxOptions); if (ajaxHelper.ViewContext.UnobtrusiveJavaScriptEnabled) { tagBuilder.MergeAttributes<string, object>(ajaxOptions.ToUnobtrusiveHtmlAttributes()); } //表背单内容 StringBuilder inputTagBuilder = new StringBuilder(); StringBuilder urlBuilder = new StringBuilder(); string str = string.IsNullOrEmpty(formAction) ? HttpContext.Current.Request.Url.AbsolutePath : formAction; string str2 = HttpContext.Current.Request.Url.Query.Trim(new char[] { ? }); List<string> strArray = str2.Split(new char[] { & }).Where(a => !string.IsNullOrEmpty(a)).ToList(); string[] formKeys = HttpContext.Current.Request.Form.AllKeys; foreach(var v in formKeys) { if (v != "X-Requested-With") { strArray.Add(v + "=" + HttpContext.Current.Request.Form[v]); } } if(!strArray.Any(a=>a.StartsWith("page"))) { strArray.Add("page=1"); } foreach (string str3 in strArray) { string[] strArray2 = str3.Split(new char[] { = }); inputTagBuilder.AppendFormat("<input type=‘hidden‘ value=‘{1}‘ name=‘{0}‘ />", strArray2[0], HttpContext.Current.Server.UrlEncode(strArray2[1])); if ((str3.Length > 0) && !str3.StartsWith("page=")) { if (strArray2.Length == 2) { urlBuilder.AppendFormat("&{0}={1}", strArray2[0], HttpContext.Current.Server.UrlEncode(strArray2[1])); } } } string pageHtmlString = GetPaginationHtml(rowCount, pageSize, str, urlBuilder.ToString() + option); string outputHtmlString = tagBuilder.ToString().Replace("</form>", inputTagBuilder + pageHtmlString + "</form>"); return new MvcHtmlString(outputHtmlString); } private static AjaxOptions GetAjaxOptions(AjaxOptions ajaxOptions) { if (ajaxOptions == null) { return new AjaxOptions(); } return ajaxOptions; } private static string GetPaginationHtml(int rowCount, int pageSize, string url, string option) { int num = (int)Math.Ceiling((double)((rowCount * 1.0) / ((double)pageSize))); if (num < 2) { return string.Empty; } if (url == "") { url = HttpContext.Current.Request.Url.AbsolutePath; } int result = 1; if (HttpContext.Current.Request["page"] != null) { int.TryParse(HttpContext.Current.Request["page"], out result); } StringBuilder builder = new StringBuilder(); builder.AppendFormat("<div class=\"pagination-nav\"><div class=\"pull-left\" style=‘margin-left: 5px;‘>共 {1} 页,{0} 条记录</div><ul class=\"pagination pull-right\">", rowCount.ToString(), num.ToString()); int num3 = 0; if (result > 1) { builder.Append(string.Concat(new object[] { " <li><a href=\"", url, "?page=", result - 1, option, "\"", " data-page=\"" + (result - 1) + "\"", ">\x00ab</a></li> " })); } if (num > (result + 5)) { num3 = result + 5; } else { num3 = num; } for (int i = result - 6; i < num3; i++) { if (i >= 0) { if (result == (i + 1)) { builder.Append("<li class=\"active\"><a href=\"javascript:\">" + ((i + 1)).ToString() + "<span class=\"sr-only\">(current)</span></a></li>"); } else { builder.Append(string.Concat(new object[] { "<li><a href=\"", url, "?page=", i + 1, option, "\"", " data-page=\"" + (i + 1) + "\"", ">" + (i + 1), "</a></li>" })); } } } if (result != num) { builder.Append(string.Concat(new object[] { "<li><a href=\"", url, "?page=", result + 1, option, "\"", " data-page=\"" + (result + 1) + "\"", ">\x00bb</a></li>" })); } builder.Append("</ul></div><div class=\"clearfix\"></div>"); return builder.ToString(); } }

 

需要引用下面JS

基础:jquery.unobtrusive-ajax.min.js

 

var ajaxPage = function () {
    return {
        init: function () {

            $("form .pagination-nav a[data-page]").live(‘click‘, function () {

                $("input[name=page]", $(this).parents(‘form‘)).val($(this).attr(‘data-page‘));

                $(this).parents(‘form‘).submit();

                return false;
            });
        },
        //!init
    }
}();

//run init
$(function () { ajaxPage.init(); });

 使用:

 

ajax:
@Ajax.Pages(Model.TotalCount, Model.PageSize, @Url.Action("list"), new AjaxOptions { UpdateTargetId = "updateTargetID" })

html:
@Html.Pages(Model.TotalCount, Model.PageSize, @Url.Action("list"), new AjaxOptions { UpdateTargetId = "updateTargetID" })

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。