using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
namespace Mini.Web.WebHelper
{
public static class ExtendMvcHtml
{
///
/// 普通按钮
///
/// htmlhelper
/// 控件Id
/// 控件icon图标class
/// 控件的名称
/// 分割线
/// html
public static IHtmlContent ToolButton(this IHtmlHelper 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 HtmlString(sb.ToString());
}
///
/// 普通按钮
///
/// htmlhelper
/// 控件Id
/// 控件icon图标class
/// 控件的名称
/// click事件名称
/// 分割线
/// html
public static IHtmlContent ToolButton(this IHtmlHelper 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 HtmlString(sb.ToString());
}
///
/// 不浮动
///
///
///
///
///
///
///
///
public static IHtmlContent ToolButtonPlain(this IHtmlHelper 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 HtmlString(sb.ToString());
}
///
/// 分割按钮
///
///
public static IHtmlContent SplitButton(this IHtmlHelper 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 HtmlString(sb.ToString());
}
///
/// 获取未知ToolBar按钮
///
///
///
///
public static IHtmlContent ToolBarOtherButton(this IHtmlHelper 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 IHtmlContent OptGroupSelect(this IHtmlHelper helper, string name, string selectValue, ListOptGroup obj, string attribute = "", bool canChooseParent = false)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine(string.Format("", name, attribute));
foreach (var item in obj.selectListItem)
{
if (item.Value == selectValue)
sb.AppendLine(string.Format("{1} ", item.Value, item.Text));
else
sb.AppendLine(string.Format("{1} ", item.Value, item.Text));
}
foreach (OptGroup group in obj.groupList)
{
if (canChooseParent == false)
{
sb.AppendLine("");
foreach (var item in group.selectListItem)
{
if (item.Value == selectValue)
sb.AppendLine(string.Format("{1} ", item.Value, item.Text));
else
sb.AppendLine(string.Format("{1} ", item.Value, item.Text));
}
sb.AppendLine(" ");
}
else
{
sb.AppendLine("" + group.lable + " ");
foreach (var item in group.selectListItem)
{
if (item.Value == selectValue)
sb.AppendLine(string.Format(" {1} ", item.Value, item.Text));
else
sb.AppendLine(string.Format(" {1} ", item.Value, item.Text));
}
}
}
sb.AppendLine(" ");
return new HtmlString(sb.ToString());
}
///
/// 时间控件
///
///
///
///
///
public static IHtmlContent WdatePickerText(this IHtmlHelper helper, string id, string text, bool hasHour = false, string attribute = "")
{
StringBuilder sb = new StringBuilder();
if (hasHour)
{
sb.Append(" ");
}
else
sb.Append(" ");
return new HtmlString(sb.ToString());
}
///
/// 时间控件(限定范围)
///
///
///
///
///
///
///
///
///
public static IHtmlContent WdatePickerText(this IHtmlHelper helper, string id, string text, string minDate, string maxDate, bool hasHour = false, string attribute = "", string nclass = "")
{
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 HtmlString(sb.ToString());
}
//public static IHtmlContent 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(@"");
// return new IHtmlContent(str.ToString());
//}
#region 单选框和复选框的扩展
/////
///// 复选框,selValue为选中项
/////
/////
/////
/////
/////
/////
//public static IHtmlContent CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, IEnumerable selValue)
//{
// return CheckBoxAndRadioFor(name, selectList, false, selValue);
//}
//public static IHtmlContent CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, string selValue)
//{
// return CheckBoxList(htmlHelper, name, selectList, new List { selValue });
//}
/////
///// 复选框
/////
/////
/////
/////
/////
//public static IHtmlContent CheckBoxForList(this HtmlHelper htmlHelper, string name, IEnumerable selectList)
//{
// return CheckBoxList(htmlHelper, name, selectList, new List());
//}
/////
///// 根据列表输出checkbox
/////
/////
/////
/////
/////
/////
/////
//public static IHtmlContent CheckBoxListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList)
//{
// return CheckBoxListFor(htmlHelper, expression, selectList, null);
//}
/////
///// 根据列表输出checkbox,selValue为默认选中的项
/////
/////
/////
/////
/////
/////
/////
/////
//public static IHtmlContent CheckBoxListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string selValue)
//{
// string name = ExpressionHelper.GetExpressionText(expression);
// return CheckBoxAndRadioFor(name, selectList, false, new List { selValue });
//}
///
/// 输出单选框和复选框
///
///
///
///
///
///
///
///
//static IHtmlContent 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 IHtmlContent.Create(str.ToString());
//}
//public static IHtmlContent RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, IEnumerable selValue)
//{
// return CheckBoxAndRadioFor(name, selectList, true, selValue);
//}
///
/// 单选按钮组,seletList为选中项
///
///
///
///
///
///
//public static IHtmlContent RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, string selValue)
//{
// return RadioButtonList(htmlHelper, name, selectList, new List { selValue });
//}
/////
///// 单选按钮组
/////
/////
/////
/////
/////
//public static IHtmlContent RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable selectList)
//{
// return RadioButtonList(htmlHelper, name, selectList, new List());
//}
/////
///// 根据列表输出radiobutton
/////
/////
/////
/////
/////
/////
/////
//public static IHtmlContent RadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList)
//{
// return RadioButtonListFor(htmlHelper, expression, selectList, new List());
//}
//public static IHtmlContent 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 IHtmlContent RadioButtonListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string selValue)
//{
// return RadioButtonListFor(htmlHelper, expression, selectList, new List { selValue });
//}
//public static IHtmlContent 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; }
}
}