CommonHtmlHelper.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Mvc;
  6. using Ant.Service.Common;
  7. namespace CommonToolHelper
  8. {
  9. /// <summary>
  10. /// HTML帮助类
  11. /// </summary>
  12. public class CommonHtmlHelper
  13. {
  14. /// <summary>
  15. /// 过滤字符,输出HTML格式数据
  16. /// </summary>
  17. public static MvcHtmlString RtfTextToHtml(string rtfText)
  18. {
  19. if (String.IsNullOrWhiteSpace(rtfText))
  20. {
  21. return new MvcHtmlString("");
  22. }
  23. else
  24. {
  25. var text = rtfText
  26. .Replace(" ", "&nbsp;")
  27. .Replace("\t", string.Format("{0}{0}{0}{0}", "&nbsp;"))
  28. .Replace("<", "&lt;")
  29. .Replace(">", "&gt;")
  30. .Replace("\n", "<br/>");
  31. return new MvcHtmlString(text);
  32. }
  33. }
  34. /// <summary>
  35. ///
  36. /// </summary>
  37. /// <param name="roleid"></param>
  38. /// <returns></returns>
  39. public static string getRolse(int roleid)
  40. {
  41. string str = string.Empty;
  42. switch (roleid)
  43. {
  44. case 1:
  45. str = "农机手"; break;
  46. case 2:
  47. str = "经销商"; break;
  48. case 3:
  49. str = "销售人员"; break;
  50. case 4:
  51. str = "仓管人员"; break;
  52. }
  53. return str;
  54. }
  55. /// <summary>
  56. /// 显示错层方法
  57. /// </summary>
  58. public static object GetDepartmentName(string name, decimal? level)
  59. {
  60. if (level > 1)
  61. {
  62. string nbsp = "&nbsp;&nbsp;";
  63. for (int i = 0; i < level; i++)
  64. {
  65. nbsp += "&nbsp;&nbsp;";
  66. }
  67. name = nbsp + "|--" + name;
  68. }
  69. return name;
  70. }
  71. /// <summary>
  72. /// 普通列表分页结果 Page:当前页 PageSize:显示行数 Count:总条数 Url:跳转URL PageNum
  73. /// </summary>
  74. /// <param name="page">当前页</param>
  75. /// <param name="PageSize">显示行数</param>
  76. /// <param name="count">总条数</param>
  77. /// <param name="PageNum">中间页码处理方式</param>
  78. /// <param name="url">跳转URL</param>
  79. /// <returns></returns>
  80. public static MvcHtmlString PaginationPager(int Page, int PageSize, int Count, int PageNum, string Url)
  81. {
  82. int index;
  83. int num = 0;
  84. //数据总条数小于等于显示行数
  85. if (Count <= PageSize)
  86. {
  87. return new MvcHtmlString("");
  88. }
  89. if (Count == 0)
  90. {
  91. return new MvcHtmlString("");
  92. }
  93. if ((Count % PageSize) == 0)
  94. {
  95. num = Count / PageSize;
  96. }
  97. else
  98. {
  99. num = (Count / PageSize) + 1;
  100. }
  101. if (string.IsNullOrEmpty(Url))
  102. {
  103. Url = HttpContext.Current.Request.Url.AbsoluteUri.ToString();
  104. }
  105. System.Text.StringBuilder str = new System.Text.StringBuilder(15000);
  106. //str.Append("<div class=\"black3\">");
  107. bool flag = false;
  108. if (Url.IndexOf("@p@") < 0)
  109. {
  110. index = Url.IndexOf("?");
  111. if ((index > 0) && (index < Url.Length))
  112. {
  113. int length = Url.ToLower().IndexOf("page=", index);
  114. if (length > 0)
  115. {
  116. int startIndex = Url.IndexOf("&", (int)(length + 1));
  117. if (startIndex > 0)
  118. {
  119. Url = Url.Substring(0, length) + Url.Substring(startIndex);
  120. Url = Url + "&";
  121. }
  122. else
  123. {
  124. Url = Url.Substring(0, length);
  125. }
  126. }
  127. else
  128. {
  129. Url = Url + "&";
  130. }
  131. }
  132. else
  133. {
  134. Url = "?";
  135. }
  136. }
  137. str.Append("<div class=\"dataTables_paginate paging_full_numbers\" style=\"padding-top: 0px;\" id=\"example_paginate\">");
  138. str.Append("<ul class=\"pagination\">");
  139. str.Append("<div class=\"dataTables_paginate paging_bs_full\" id=\"datatable_paginate\">");
  140. str.Append("<ul class=\"pagination\">");
  141. //处理首页页码
  142. if (Page > 1)
  143. {
  144. //记录分页栏首个页码 index
  145. str.Append("<li class=\"paginate_button first\" id=\"example_first\">");
  146. str.Append("<a href=\"" + ((Url.IndexOf("@p@") >= 0) ? Url.Replace("@p@", "1") : (Url + "page=1")) + "\" aria-controls=\"example\" data-dt-idx=\"0\" tabindex=\"0\">首页</a></li>");
  147. }
  148. else
  149. {
  150. str.Append("<li class=\"paginate_button first disabled\" id=\"example_first\"><a href=\"\" aria-controls=\"example\" data-dt-idx=\"0\" tabindex=\"0\">首页</a></li>");//第一个分页栏中首页不可点击
  151. //str.Append("<li class=\"paginate_button first disabled\" id=\"example_first\">首页</li>");//第一个分页栏中首页不可点击
  152. }
  153. //处理上一页页码
  154. if (Page > 1)
  155. {
  156. index = Page - 1;
  157. str.Append("<li class=\"paginate_button previous\" id=\"example_previous\">");
  158. str.Append("<a href=\"" + ((Url.IndexOf("@p@") >= 0) ? Url.Replace("@p@", index.ToString()) : (Url + "page=" + index.ToString())) + "\" aria-controls=\"example\" data-dt-idx=\"1\" tabindex=\"0\">上一页</a></li>");
  159. }
  160. else
  161. {
  162. str.Append("<li class=\"paginate_button previous disabled\" id=\"example_previous\">");
  163. str.Append("<a href=\"\" aria-controls=\"example\" data-dt-idx=\"1\" tabindex=\"0\">上一页</a></li>");
  164. }
  165. str.Append("");
  166. //中间页码处理方式
  167. if ((Page + (PageNum / 2)) <= num)
  168. {
  169. index = (Page + (PageNum / 2)) - PageNum;
  170. }
  171. else
  172. {
  173. index = (num - PageNum) + 1;
  174. }
  175. if (index <= 0)
  176. {
  177. index = 1;
  178. }
  179. for (int i = 1; i <= PageNum; i++)
  180. {
  181. if (index > num)//非正常情况P传值
  182. {
  183. break;
  184. }
  185. if (index != Page)
  186. {
  187. str.Append("<li class=\"paginate_button \"><a href=\"" + ((Url.IndexOf("@p@") >= 0) ? Url.Replace("@p@", index.ToString()) : (Url + "page=" + index.ToString())) + "\" aria-controls=\"example\" data-dt-idx=\"3\" tabindex=\"0\">" + index.ToString() + "</a></li>");
  188. flag = true;//这里控制是不是只有一页,如果加了判断,只有一页数据,分页不显示
  189. }
  190. else
  191. {
  192. str.Append("<li class=\"paginate_button active\"><a href=\"JavaScript:void(0)\" aria-controls=\"example\" data-dt-idx=\"2\" tabindex=\"0\">" + index.ToString() + "</a></li>");
  193. }
  194. index++;
  195. }
  196. //下一页
  197. if (Page < num)
  198. {
  199. index = Page + 1;
  200. str.Append("<li class=\"paginate_button next\" id=\"example_next\">");
  201. str.Append("<a href=\"" + ((Url.IndexOf("@p@") >= 0) ? Url.Replace("@p@", index.ToString()) : (Url + "page=" + index.ToString())) + "\" aria-controls=\"example\" data-dt-idx=\"4\" tabindex=\"0\">下一页</a></li>");
  202. }
  203. else
  204. {
  205. str.Append("<li class=\"paginate_button next disabled\" id=\"example_next\"><a href=\"javascript:void(0)\" aria-controls=\"example\" data-dt-idx=\"4\" tabindex=\"0\">下一页</a></li>");
  206. }
  207. //末页
  208. if (Page < num)
  209. {
  210. str.Append("<li class=\"paginate_button last\" id=\"example_last\">");
  211. str.Append("<a href=\"" + ((Url.IndexOf("@p@") >= 0) ? Url.Replace("@p@", num.ToString()) : (Url + "page=" + num.ToString())) + "\" aria-controls=\"example\" data-dt-idx=\"5\" tabindex=\"0\">末页</a></li>");
  212. }
  213. else
  214. {
  215. str.Append("<li class=\"paginate_button last disabled\"><a id=\"datatable_last\">末页</a></li>");
  216. }
  217. //尾注
  218. str.Append("</ul>");
  219. str.Append("</div>");
  220. if (!flag)
  221. {
  222. return new MvcHtmlString("");//用来控制不显示分页
  223. }
  224. return new MvcHtmlString(str.ToString());
  225. }
  226. }
  227. }