Mini.Crm/Mini.Web/WebHelper/ExtendMvcHtml.cs

532 lines
24 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
{
/// <summary>
/// 普通按钮
/// </summary>
/// <param name="helper">htmlhelper</param>
/// <param name="id">控件Id</param>
/// <param name="icon">控件icon图标class</param>
/// <param name="text">控件的名称</param>
/// <param name="hr">分割线</param>
/// <returns>html</returns>
public static IHtmlContent ToolButton(this IHtmlHelper helper, string id, string icon, string text, bool hr)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<a id=\"{0}\" style=\"float: left;\" class=\"l-btn l-btn-plain\">", id);
sb.AppendLine("");
sb.AppendFormat("<span class=\"l-btn-left\"><span class=\"l-btn-text {0}\" style=\"padding-left: 20px;\">", icon);
sb.AppendLine("");
sb.AppendFormat("{0}</span></span></a>", text);
sb.AppendLine("");
if (hr)
{
sb.Append("<div class=\"datagrid-btn-separator\"></div>");
sb.AppendLine("");
}
return new HtmlString(sb.ToString());
}
/// <summary>
/// 普通按钮
/// </summary>
/// <param name="helper">htmlhelper</param>
/// <param name="id">控件Id</param>
/// <param name="icon">控件icon图标class</param>
/// <param name="text">控件的名称</param>
/// <param name="click">click事件名称</param>
/// <param name="hr">分割线</param>
/// <returns>html</returns>
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("<a id=\"{0}\" style=\"float: left;\" class=\"l-btn l-btn-plain\" onclick='{1}()'>", id, click);
else
sb.AppendFormat("<a id=\"{0}\" style=\"float: left;\" class=\"l-btn l-btn-plain\" >", id);
sb.AppendLine("");
sb.AppendFormat("<span class=\"l-btn-left\"><span class=\"l-btn-text {0}\" style=\"padding-left: 20px;\">", icon);
sb.AppendLine("");
sb.AppendFormat("{0}</span></span></a>", text);
sb.AppendLine("");
if (hr)
{
sb.Append("<div class=\"datagrid-btn-separator\"></div>");
sb.AppendLine("");
}
return new HtmlString(sb.ToString());
}
/// <summary>
/// 不浮动
/// </summary>
/// <param name="helper"></param>
/// <param name="id"></param>
/// <param name="icon"></param>
/// <param name="text"></param>
/// <param name="hr"></param>
/// <param name="attribute"></param>
/// <returns></returns>
public static IHtmlContent ToolButtonPlain(this IHtmlHelper helper, string id, string icon, string text, bool hr, string attribute)
{
StringBuilder sb = new StringBuilder();
sb.AppendFormat("<a id=\"{0}\" " + attribute + " class=\"l-btn l-btn-plain\">", id);
sb.AppendLine("");
sb.AppendFormat("<span class=\"l-btn-left\"><span class=\"l-btn-text {0}\" {1} >", icon, icon == "" ? "" : " style=\"padding-left: 20px;\"");
sb.AppendLine("");
sb.AppendFormat("{0}</span></span></a>", text);
sb.AppendLine("");
if (hr)
{
sb.Append("<span class=\"tool-btn-separator\"></span>");
sb.AppendLine("");
}
return new HtmlString(sb.ToString());
}
/// <summary>
/// 分割按钮
/// </summary>
/// <returns></returns>
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("<a style=\"float: left;\" href='javascript:void(0)' id=\"{0}\" class=\"easyui-splitbutton\" data-options=\"menu:'#{3}',iconCls:'{1}'\" >{2}</a>", id, icon, text, childId);
sb.AppendLine("");
sb.AppendFormat("<div id=\"{0}\" style=\"width:auto;\"> ", childId);
sb.AppendLine("");
foreach (ChildButton button in buttonList)
{
sb.AppendFormat("<div id=\"{2}\" onclick='{3}()' data-options=\"iconCls:'{0}'\">{1}</div> ", button.icon, button.text, button.id, button.click);
sb.AppendLine("");
}
sb.AppendLine("</div>");
sb.AppendLine("");
if (hr)
{
sb.Append("<div class=\"datagrid-btn-separator\"></div>");
sb.AppendLine("");
}
return new HtmlString(sb.ToString());
}
/// <summary>
/// 获取未知ToolBar按钮
/// </summary>
/// <param name="helper"></param>
/// <param name="button"></param>
/// <returns></returns>
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);
}
}
/// <summary>
///
/// </summary>
/// <param name="helper"></param>
/// <param name="name">名称和ID</param>
/// <param name="selectValue">默认值</param>
/// <param name="obj">数据源</param>
/// <returns></returns>
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("<select id='{0}' name='{0}' {1}>", name, attribute));
foreach (var item in obj.selectListItem)
{
if (item.Value == selectValue)
sb.AppendLine(string.Format("<option value='{0}' selected='selected'>{1}</option>", item.Value, item.Text));
else
sb.AppendLine(string.Format("<option value='{0}'>{1}</option>", item.Value, item.Text));
}
foreach (OptGroup group in obj.groupList)
{
if (canChooseParent == false)
{
sb.AppendLine("<optgroup label=\"" + group.lable + "\">");
foreach (var item in group.selectListItem)
{
if (item.Value == selectValue)
sb.AppendLine(string.Format("<option value='{0}' selected='selected'>{1}</option>", item.Value, item.Text));
else
sb.AppendLine(string.Format("<option value='{0}'>{1}</option>", item.Value, item.Text));
}
sb.AppendLine("</optgroup>");
}
else
{
sb.AppendLine("<option style='font-weight: bold;' parentValue='" + group.labValue + "' value='" + group.labValue + "'>" + group.lable + "</option>");
foreach (var item in group.selectListItem)
{
if (item.Value == selectValue)
sb.AppendLine(string.Format("<option value='{0}' selected='selected'>&nbsp;&nbsp;{1}</option>", item.Value, item.Text));
else
sb.AppendLine(string.Format("<option value='{0}'>&nbsp;&nbsp;{1}</option>", item.Value, item.Text));
}
}
}
sb.AppendLine("</select>");
return new HtmlString(sb.ToString());
}
/// <summary>
/// 时间控件
/// </summary>
/// <param name="helper"></param>
/// <param name="id"></param>
/// <param name="text"></param>
/// <returns></returns>
public static IHtmlContent WdatePickerText(this IHtmlHelper helper, string id, string text, bool hasHour = false, string attribute = "")
{
StringBuilder sb = new StringBuilder();
if (hasHour)
{
sb.Append("<input id='" + id + "' name='" + id + "' class='Wdate' type='text' " + attribute + " value='" + text + "' onclick='WdatePicker({dateFmt:&#39;yyyy-MM-dd HH:mm:ss&#39;})' />");
}
else
sb.Append("<input id='" + id + "' name='" + id + "' class='Wdate' type='text' " + attribute + " value='" + text + "' onclick='WdatePicker()' />");
return new HtmlString(sb.ToString());
}
/// <summary>
/// 时间控件(限定范围)
/// </summary>
/// <param name="helper"></param>
/// <param name="id"></param>
/// <param name="text"></param>
/// <param name="minDate"></param>
/// <param name="maxDate"></param>
/// <param name="hasHour"></param>
/// <param name="attribute"></param>
/// <returns></returns>
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("<input id='" + id + "' name='" + id + "' class='Wdate " + nclass + "' type='text' " + attribute + " value='" + text + "' onclick=\"WdatePicker({dateFmt:\"yyyy-MM-dd HH:mm:ss\"" + option + "})\" />");
}
else
{
if (option.Length > 0)
option = "{" + option.Substring(1, option.Length - 1) + "}";
sb.Append("<input id='" + id + "' name='" + id + "' class='Wdate " + nclass + "' type='text' " + attribute + " value='" + text + "' onclick=\"WdatePicker(" + option + ")\" />");
}
return new HtmlString(sb.ToString());
}
//public static IHtmlContent CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> 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(@"<table>");
// 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(@"<tr>");
// foreach (var item in items.Skip(j * RepeatColumns).Take(RepeatColumns))
// {
// i++;
// string id = string.Format("{0}_{1}", name, i);
// str.AppendFormat("<td class='checktd'><input id=\"{0}\" type=\"{3}\" name=\"{1}\" value=\"{2}\"", id, name, item.Value, type);
// if (!string.IsNullOrEmpty(radioSelect) && item.Value == radioSelect)
// {
// str.Append(" checked='checked'");
// }
// else if (item.Selected)
// {
// str.Append(" checked='checked'");
// }
// str.AppendFormat("/><lable for=\"{0}\">{1}</lable></td>", id, item.Text);
// }
// str.Append("</tr>");
// }
// }
// else
// {
// foreach (var item in items)
// {
// i++;
// string id = string.Format("{0}_{1}", name, i);
// str.Append(@"<tr>");
// str.AppendFormat("<td class='checktd'><input id=\"{0}\" type=\"{3}\" name=\"{1}\" value=\"{2}\"", id, name, item.Value, type);
// if (!string.IsNullOrEmpty(radioSelect) && radioSelect == item.Value)
// {
// str.Append(" checked='checked'");
// }
// else if (item.Selected)
// {
// str.Append(" checked='checked'");
// }
// str.AppendFormat("/><lable for=\"{0}\">{1}</lable></td>", id, item.Text);
// str.Append("</tr>");
// }
// }
// str.Append("</table>");
// return new IHtmlContent(str.ToString());
//}
#region
///// <summary>
///// 复选框,selValue为选中项
///// </summary>
///// <param name="htmlHelper"></param>
///// <param name="name"></param>
///// <param name="selectList"></param>
///// <param name="selValue"></param>
///// <returns></returns>
//public static IHtmlContent CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable<ListItem> selectList, IEnumerable<string> selValue)
//{
// return CheckBoxAndRadioFor<object, string>(name, selectList, false, selValue);
//}
//public static IHtmlContent CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable<ListItem> selectList, string selValue)
//{
// return CheckBoxList(htmlHelper, name, selectList, new List<string> { selValue });
//}
///// <summary>
///// 复选框
///// </summary>
///// <param name="htmlHelper"></param>
///// <param name="name"></param>
///// <param name="selectList"></param>
///// <returns></returns>
//public static IHtmlContent CheckBoxForList(this HtmlHelper htmlHelper, string name, IEnumerable<ListItem> selectList)
//{
// return CheckBoxList(htmlHelper, name, selectList, new List<string>());
//}
///// <summary>
///// 根据列表输出checkbox
///// </summary>
///// <typeparam name="TModel"></typeparam>
///// <typeparam name="TProperty"></typeparam>
///// <param name="htmlHelper"></param>
///// <param name="expression"></param>
///// <param name="selectList"></param>
///// <returns></returns>
//public static IHtmlContent CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ListItem> selectList)
//{
// return CheckBoxListFor(htmlHelper, expression, selectList, null);
//}
///// <summary>
///// 根据列表输出checkbox,selValue为默认选中的项
///// </summary>
///// <typeparam name="TModel"></typeparam>
///// <typeparam name="TProperty"></typeparam>
///// <param name="htmlHelper"></param>
///// <param name="expression"></param>
///// <param name="selectList"></param>
///// <param name="selValue"></param>
///// <returns></returns>
//public static IHtmlContent CheckBoxListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ListItem> selectList, string selValue)
//{
// string name = ExpressionHelper.GetExpressionText(expression);
// return CheckBoxAndRadioFor<TModel, TProperty>(name, selectList, false, new List<string> { selValue });
//}
/// <summary>
/// 输出单选框和复选框
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="expression"></param>
/// <param name="selectList"></param>
/// <param name="isRadio"></param>
/// <param name="selValue"></param>
/// <returns></returns>
//static IHtmlContent CheckBoxAndRadioFor<TModel, TProperty>(
// string name,
// IEnumerable<ListItem> selectList,
// bool isRadio,
// IEnumerable<string> 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("<span><input type='{3}' value='{0}' name={1} id={1}{2} " + check + "/>", item.Value, name, c, type);
// str.AppendFormat("<lable for='{0}{1}' {3}>{2}</lable></span>", name, c, item.Text, activeClass);
// }
// return IHtmlContent.Create(str.ToString());
//}
//public static IHtmlContent RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable<ListItem> selectList, IEnumerable<string> selValue)
//{
// return CheckBoxAndRadioFor<object, string>(name, selectList, true, selValue);
//}
/// <summary>
/// 单选按钮组seletList为选中项
/// </summary>
/// <param name="htmlHelper"></param>
/// <param name="name"></param>
/// <param name="selectList"></param>
/// <param name="selValue"></param>
/// <returns></returns>
//public static IHtmlContent RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable<ListItem> selectList, string selValue)
//{
// return RadioButtonList(htmlHelper, name, selectList, new List<string> { selValue });
//}
///// <summary>
///// 单选按钮组
///// </summary>
///// <param name="htmlHelper"></param>
///// <param name="name"></param>
///// <param name="selectList"></param>
///// <returns></returns>
//public static IHtmlContent RadioButtonList(this HtmlHelper htmlHelper, string name, IEnumerable<ListItem> selectList)
//{
// return RadioButtonList(htmlHelper, name, selectList, new List<string>());
//}
///// <summary>
///// 根据列表输出radiobutton
///// </summary>
///// <typeparam name="TModel"></typeparam>
///// <typeparam name="TProperty"></typeparam>
///// <param name="htmlHelper"></param>
///// <param name="expression"></param>
///// <param name="selectList"></param>
///// <returns></returns>
//public static IHtmlContent RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ListItem> selectList)
//{
// return RadioButtonListFor(htmlHelper, expression, selectList, new List<string>());
//}
//public static IHtmlContent RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ListItem> selectList, IEnumerable<string> selValue)
//{
// string name = ExpressionHelper.GetExpressionText(expression);
// return CheckBoxAndRadioFor<TModel, TProperty>(name, selectList, true, selValue);
//}
/// <summary>
/// 根据列表输出radiobutton,selValue为默认选中的项
/// </summary>
/// <typeparam name="TModel"></typeparam>
/// <typeparam name="TProperty"></typeparam>
/// <param name="htmlHelper"></param>
/// <param name="expression"></param>
/// <param name="selectList"></param>
/// <param name="selValue"></param>
/// <returns></returns>
//public static IHtmlContent RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<ListItem> selectList, string selValue)
//{
// return RadioButtonListFor(htmlHelper, expression, selectList, new List<string> { selValue });
//}
//public static IHtmlContent RadioButtonListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> 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
{
/// <summary>
/// 分组部分
/// </summary>
public List<OptGroup> groupList { get; set; }
/// <summary>
/// 部分组部分
/// </summary>
public List<SelectListItem> selectListItem { get; set; }
}
public class OptGroup
{
/// <summary>
/// 组名称
/// </summary>
public string lable { get; set; }
public string labValue { get; set; }
/// <summary>
/// 组下面 数据行
/// </summary>
public List<SelectListItem> selectListItem { get; set; }
}
}