using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; using System.Text; using System.Web.Mvc; using System.Web.UI.WebControls; namespace WX.CRM.WebHelper { public static class ExtendMvcHtml { /// /// 普通按钮 /// /// htmlhelper /// 控件Id /// 控件icon图标class /// 控件的名称 /// 分割线 /// html public static MvcHtmlString ToolButton(this HtmlHelper helper, string id, string icon, string text, bool hr) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("", id); sb.AppendLine(""); sb.AppendFormat("", icon); sb.AppendLine(""); sb.AppendFormat("{0}", text); sb.AppendLine(""); if (hr) { sb.Append("
"); sb.AppendLine(""); } return new MvcHtmlString(sb.ToString()); } /// /// 普通按钮 /// /// htmlhelper /// 控件Id /// 控件icon图标class /// 控件的名称 /// click事件名称 /// 分割线 /// html public static MvcHtmlString ToolButton(this HtmlHelper helper, string id, string icon, string text, string click, bool hr) { StringBuilder sb = new StringBuilder(); if (!string.IsNullOrEmpty(click)) sb.AppendFormat("", id, click); else sb.AppendFormat("", id); sb.AppendLine(""); sb.AppendFormat("", icon); sb.AppendLine(""); sb.AppendFormat("{0}", text); sb.AppendLine(""); if (hr) { sb.Append("
"); sb.AppendLine(""); } return new MvcHtmlString(sb.ToString()); } /// /// 不浮动 /// /// /// /// /// /// /// /// public static MvcHtmlString ToolButtonPlain(this HtmlHelper helper, string id, string icon, string text, bool hr, string attribute) { StringBuilder sb = new StringBuilder(); sb.AppendFormat("", id); sb.AppendLine(""); sb.AppendFormat("", icon, icon == "" ? "" : " style=\"padding-left: 20px;\""); sb.AppendLine(""); sb.AppendFormat("{0}", text); sb.AppendLine(""); if (hr) { sb.Append(""); sb.AppendLine(""); } return new MvcHtmlString(sb.ToString()); } /// /// 分割按钮 /// /// public static MvcHtmlString SplitButton(this HtmlHelper helper, string id, string icon, string text, ChildButton[] buttonList, bool hr) { Random rd = new Random(); string childId = id + rd.Next(100, 1000); StringBuilder sb = new StringBuilder(); sb.AppendFormat("{2}", id, icon, text, childId); sb.AppendLine(""); sb.AppendFormat("
", childId); sb.AppendLine(""); foreach (ChildButton button in buttonList) { sb.AppendFormat("
{1}
", button.icon, button.text, button.id, button.click); sb.AppendLine(""); } sb.AppendLine("
"); sb.AppendLine(""); if (hr) { sb.Append("
"); sb.AppendLine(""); } return new MvcHtmlString(sb.ToString()); } /// /// 获取未知ToolBar按钮 /// /// /// /// public static MvcHtmlString ToolBarOtherButton(this HtmlHelper helper, ToolBarButton button) { if (button.child == null) { return ToolButton(helper, button.id, button.icon, button.text, button.click, button.hr); } else { ChildButton[] childs = new ChildButton[button.child.Count]; for (int i = 0; i < button.child.Count; i++) { childs[i] = new ChildButton() { click = button.child[i].click, icon = button.child[i].icon, id = button.child[i].id, text = button.child[i].text }; } return SplitButton(helper, button.id, button.icon, button.text, childs, true); } } /// /// /// /// /// 名称和ID /// 默认值 /// 数据源 /// public static MvcHtmlString OptGroupSelect(this HtmlHelper helper, string name, string selectValue, ListOptGroup obj, string attribute = "", bool canChooseParent = false) { StringBuilder sb = new StringBuilder(); sb.AppendLine(string.Format(""); return new MvcHtmlString(sb.ToString()); } /// /// 时间控件 /// /// /// /// /// public static MvcHtmlString WdatePickerText(this HtmlHelper helper, string id, string text, bool hasHour = false, string attribute = "") { StringBuilder sb = new StringBuilder(); if (hasHour) { sb.Append(""); } else sb.Append(""); return new MvcHtmlString(sb.ToString()); } /// /// 时间控件(限定范围) /// /// /// /// /// /// /// /// /// public static MvcHtmlString WdatePickerText(this HtmlHelper helper, string id, string text, string minDate, string maxDate, bool hasHour = false, string attribute = "") { StringBuilder sb = new StringBuilder(); string option = ""; if (!string.IsNullOrEmpty(minDate)) { option += ",minDate:" + minDate; } if (!string.IsNullOrEmpty(maxDate)) { option += ",maxDate:" + maxDate; } if (hasHour) { sb.Append(""); } else { if (option.Length > 0) option = "{" + option.Substring(1, option.Length - 1) + "}"; sb.Append(""); } return new MvcHtmlString(sb.ToString()); } public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable items, RepeatDirection repeatDirection = RepeatDirection.Horizontal, int RepeatColumns = 3, bool isRadio = false, string radioSelect = null) { StringBuilder str = new StringBuilder(); string type = isRadio ? "Radio" : "checkbox"; str.Append(@""); int i = 0; if (repeatDirection == RepeatDirection.Horizontal) { int count_items = items.Count(); if (RepeatColumns < 1) RepeatColumns = 3; int repeat_num = (int)Math.Ceiling(Convert.ToDouble(count_items / RepeatColumns)); for (int j = 0; j <= repeat_num; j++) { str.Append(@""); foreach (var item in items.Skip(j * RepeatColumns).Take(RepeatColumns)) { i++; string id = string.Format("{0}_{1}", name, i); str.AppendFormat("", id, item.Text); } str.Append(""); } } else { foreach (var item in items) { i++; string id = string.Format("{0}_{1}", name, i); str.Append(@""); str.AppendFormat("", id, item.Text); str.Append(""); } } str.Append("
{1}
{1}
"); return new MvcHtmlString(str.ToString()); } #region 单选框和复选框的扩展 /// /// 复选框,selValue为选中项 /// /// /// /// /// /// public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, IEnumerable selValue) { return CheckBoxAndRadioFor(name, selectList, false, selValue); } public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, string selValue) { return CheckBoxList(htmlHelper, name, selectList, new List { selValue }); } /// /// 复选框 /// /// /// /// /// public static MvcHtmlString CheckBoxForList(this HtmlHelper htmlHelper, string name, IEnumerable selectList) { return CheckBoxList(htmlHelper, name, selectList, new List()); } /// /// 根据列表输出checkbox /// /// /// /// /// /// /// public static MvcHtmlString CheckBoxListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList) { return CheckBoxListFor(htmlHelper, expression, selectList, null); } /// /// 根据列表输出checkbox,selValue为默认选中的项 /// /// /// /// /// /// /// /// public static MvcHtmlString CheckBoxListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string selValue) { string name = ExpressionHelper.GetExpressionText(expression); return CheckBoxAndRadioFor(name, selectList, false, new List { selValue }); } /// /// 输出单选框和复选框 /// /// /// /// /// /// /// /// static MvcHtmlString CheckBoxAndRadioFor( string name, IEnumerable selectList, bool isRadio, IEnumerable selValue) { StringBuilder str = new StringBuilder(); int c = 0; string check, activeClass = string.Empty; string type = isRadio ? "Radio" : "checkbox"; foreach (var item in selectList) { c++; if (selValue != null && selValue.Contains(item.Value)) { check = "checked='checked'"; //activeClass = "style=color:red"; } else { check = string.Empty; activeClass = string.Empty; } str.AppendFormat("", item.Value, name, c, type); str.AppendFormat("{2}", name, c, item.Text, activeClass); } return MvcHtmlString.Create(str.ToString()); } public static MvcHtmlString RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, IEnumerable selValue) { return CheckBoxAndRadioFor(name, selectList, true, selValue); } /// /// 单选按钮组,seletList为选中项 /// /// /// /// /// /// public static MvcHtmlString RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, string selValue) { return RadioButtonList(htmlHelper, name, selectList, new List { selValue }); } /// /// 单选按钮组 /// /// /// /// /// public static MvcHtmlString RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable selectList) { return RadioButtonList(htmlHelper, name, selectList, new List()); } /// /// 根据列表输出radiobutton /// /// /// /// /// /// /// public static MvcHtmlString RadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList) { return RadioButtonListFor(htmlHelper, expression, selectList, new List()); } public static MvcHtmlString RadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, IEnumerable selValue) { string name = ExpressionHelper.GetExpressionText(expression); return CheckBoxAndRadioFor(name, selectList, true, selValue); } /// /// 根据列表输出radiobutton,selValue为默认选中的项 /// /// /// /// /// /// /// /// public static MvcHtmlString RadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string selValue) { return RadioButtonListFor(htmlHelper, expression, selectList, new List { selValue }); } public static MvcHtmlString RadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, RepeatDirection repeatDirection = RepeatDirection.Horizontal, int RepeatColumns = 2) { string name = ExpressionHelper.GetExpressionText(expression); string fullHtmlFieldName = htmlHelper.ViewContext.ViewData.TemplateInfo.GetFullHtmlFieldName(name); string value = (string)GetModelStateValue(htmlHelper.ViewData, fullHtmlFieldName, typeof(string)); if (value == null) { var getValue = htmlHelper.ViewData.Eval(fullHtmlFieldName); value = (getValue ?? "").ToString(); } return CheckBoxList(htmlHelper, name, selectList, repeatDirection, RepeatColumns, true, value); } #endregion private static object GetModelStateValue(ViewDataDictionary viewData, string key, Type destinationType) { ModelState modelState; if (viewData.ModelState.TryGetValue(key, out modelState) && modelState.Value != null) { return modelState.Value.ConvertTo(destinationType, null); } return null; } } public class ChildButton { public string id { get; set; } public string icon { get; set; } public string text { get; set; } public string click { get; set; } } public class ListOptGroup { /// /// 分组部分 /// public List groupList { get; set; } /// /// 部分组部分 /// public List selectListItem { get; set; } } public class OptGroup { /// /// 组名称 /// public string lable { get; set; } public string labValue { get; set; } /// /// 组下面 数据行 /// public List selectListItem { get; set; } } }