/// <summary> /// 根据指定的类型名称和属性名称获取对应的属性配置项 /// </summary> /// <param name="className">类型名称</param> /// <param name="propName">属性名称</param> /// <param name="attr">标签属性</param> /// <returns>属性配置项</returns> public static AdvDataConfigItem GetPropertyConfig(CatalogExtAttribute attr, string className, string propName) { AdvDataConfigItem foundItem = null; var classConfig = _configCache.FirstOrDefault(cfg => cfg.ClassName.Equals(className)); if (classConfig == null) { classConfig = new AdvDataConfig() { ClassName = className }; _configCache.Add(classConfig); _modified = true; } foundItem = classConfig.Items.FirstOrDefault(c => c.PropertyName == propName); if (foundItem == null) { foundItem = new AdvDataConfigItem { OverWrite = true, FormOrder = 1, PropertyName = propName, GridOrder = 1, AllowNull = attr.AllowNull, DataSource = attr.DataSource.ToStr(), DataSourceType = CommOp.ToStr(attr.DataSourceType), DataType = attr.DataType.ToString(), MaxLength = attr.MaxLength, MinLength = attr.MinLength, MaxValue = CommOp.ToStr(attr.MaxValue), MinValue = CommOp.ToStr(attr.MinValue), RegExpr = attr.RegExpr, DefaultValue = attr.DefaultValue, InputFormat = attr.InputFormat, DisplayFormat = attr.DisplayFormat, Browsable = attr.Browsable, }; classConfig.Items.Add(foundItem); _modified = true; } else if (foundItem.OverWrite) { attr.DefaultValue = foundItem.DefaultValue; attr.DataType = CommOp.ToEnum <ExtDataType>(foundItem.DataType); attr.AllowNull = foundItem.AllowNull; attr.DataSource = foundItem.DataSource; attr.DataSourceType = CommOp.ToEnum <ExtDataSourceType>(foundItem.DataSourceType); attr.MaxLength = foundItem.MaxLength; attr.MinLength = foundItem.MinLength; attr.MaxValue = foundItem.MaxValue; attr.MinValue = foundItem.MinValue; attr.RegExpr = foundItem.RegExpr; attr.InputFormat = foundItem.InputFormat; attr.DisplayFormat = foundItem.DisplayFormat; attr.Browsable = foundItem.Browsable; } return(foundItem); }
/// <summary> /// 批量获取某个对象下所有非集合属性的数据规则和约束 /// </summary> /// <param name="modelType"></param> /// <returns></returns> void GetModelRules(Type modelType) { Attr = modelType.GetCustomAttributes(true).FirstOrDefault(attr => attr is CatalogExtAttribute) as CatalogExtAttribute ?? new CatalogExtAttribute(); SingleRules = GetSingleModelRules(modelType).ToList(); foreach (var prop in modelType.GetProperties()) { var attrs = prop.GetCustomAttributes(true); //只处理非字符串的集合类型 if (typeof(IEnumerable).IsAssignableFrom(prop.PropertyType) && prop.PropertyType != typeof(string)) { Type itemType = prop.PropertyType.GetGenericArguments().FirstOrDefault(); //如果属性是非泛型集合类型,则pass掉 if (itemType == null || itemType == typeof(Sys_DataLanguage)) { continue; } CatalogExtAttribute extAttr = attrs.FirstOrDefault(attr => attr is CatalogExtAttribute) as CatalogExtAttribute; if (extAttr != null && (extAttr.Browsable == false || extAttr.Editable == false || extAttr.DataSourceType == ExtDataSourceType.Hidden)) { continue; } ModelRule rule = new ModelRule(itemType); rule.Name = prop.Name; rule.Attr.Property = prop; CollectionRules.Add(rule); } } }
/// <summary> /// 根据sql语句返回下拉列表所需数据 /// </summary> /// <param name="catExt"></param> /// <returns></returns> public static IEnumerable <SelectListItem> GetSqlList(this CatalogExtAttribute catExt) { string sql = catExt.DataSource; var dbHelper = SiteManager.Get <DBHelper>(); List <IDataParameter> ps = new List <IDataParameter>(); if (sql.Contains("@UserId")) { ps.Add(dbHelper.CreateParameter("@UserId", AppManager.Instance.GetCurrentUserId())); } DataTable dt = dbHelper.ExecDataTable(sql, ps.ToArray()); return(GetSelectList(dt)); }
JsonResult GetList(CatalogExtAttribute catAttr) { if (catAttr == null) { return(null); } IEnumerable <SelectListItem> items = new List <SelectListItem>(); AdvDataConfigManager.GetPropertyConfig(catAttr); switch (catAttr.DataSourceType) { case ExtDataSourceType.DirectList: case ExtDataSourceType.MultipleList: items = catAttr.GetSelectList(); break; case ExtDataSourceType.SqlQuery: case ExtDataSourceType.SqlQueryMultipleList: items = catAttr.GetSqlList(); break; case ExtDataSourceType.UserDefine: return(GetUserDefineList(catAttr.Property.Name)); default: if (catAttr.Property.PropertyType.IsEnum) { items = catAttr.Property.PropertyType.GetSelectList(); } break; } return(Json(items.Select(it => new { id = it.Value, text = it.Text, }), JsonRequestBehavior.AllowGet)); }
/// <summary> /// 根据Model中定义的方法来获取下拉列表所需数据 /// 方法可以返回一个字符串或一个DataTable或一个System.Web.MVC.SelectListItem集合 /// </summary> /// <param name="catExt"></param> /// <param name="model"></param> /// <returns></returns> public static IEnumerable <SelectListItem> GetUserDefineList(this CatalogExtAttribute catExt, object model) { string funcName = catExt.DataSource; MethodInfo m = model.GetType().GetMethod(funcName); if (m == null) { return(null); } object o = m.Invoke(model, null); if (o is string) { return(GetSelectList((string)o)); } else if (o is DataTable) { return(GetSelectList((DataTable)o)); } return(o as IEnumerable <SelectListItem>); }
/// <summary> /// 标该标签的控件不会显示,但是会用隐藏域传值 /// </summary> /// <param name="attr"></param> /// <returns></returns> public static bool IsHidden(this CatalogExtAttribute attr) { return(attr.DataSourceType == ExtDataSourceType.Hidden); }
/// <summary> /// 根据栏目扩展属性标签生成下拉列表项 /// </summary> /// <param name="catExt"></param> /// <returns></returns> public static IEnumerable <SelectListItem> GetSelectList(this CatalogExtAttribute catExt) { return(GetSelectList(catExt.DataSource)); }
/// <summary> /// 返回属性的数据规则和约束条件 /// </summary> /// <param name="prop"></param> /// <returns></returns> CatalogExtAttribute GetAdvDataRule(PropertyInfo prop) { var attrs = prop.GetCustomAttributes(true); CatalogExtAttribute extAttr = attrs.FirstOrDefault(attr => attr is CatalogExtAttribute) as CatalogExtAttribute; if (extAttr == null) { extAttr = new CatalogExtAttribute(); if (prop.Name == "Id") { extAttr.Editable = false; extAttr.DataType = ExtDataType.SingleLineText; } if (prop.PropertyType.IsValueType && !prop.PropertyType.IsNullableType()) { extAttr.AllowNull = false; } } if (extAttr.Name.IsEmpty()) { extAttr.Name = prop.Name; } RequiredAttribute required = attrs.FirstOrDefault(attr => attr is RequiredAttribute) as RequiredAttribute; if (required != null) { extAttr.AllowNull = false; } StringLengthAttribute stringLen = attrs.FirstOrDefault(attr => attr is StringLengthAttribute) as StringLengthAttribute; if (stringLen != null) { extAttr.MaxLength = stringLen.MaximumLength; extAttr.MinLength = stringLen.MinimumLength; } DisplayColumnAttribute displayProp = attrs.FirstOrDefault(attr => attr is DisplayColumnAttribute) as DisplayColumnAttribute; if (displayProp != null) { extAttr.DisplayProperty = displayProp.DisplayColumn; } DisplayFormatAttribute displayFmt = attrs.FirstOrDefault(attr => attr is DisplayFormatAttribute) as DisplayFormatAttribute; if (displayFmt != null) { extAttr.DisplayFormat = displayFmt.DataFormatString; } RegularExpressionAttribute reg = attrs.FirstOrDefault(attr => attr is RegularExpressionAttribute) as RegularExpressionAttribute; if (reg != null) { extAttr.RegExpr = reg.Pattern; } RangeAttribute rng = attrs.FirstOrDefault(attr => attr is RangeAttribute) as RangeAttribute; if (rng != null) { extAttr.MinValue = rng.Minimum; extAttr.MaxValue = rng.Maximum; } DisplayAttribute dsp = attrs.FirstOrDefault(attr => attr is DisplayAttribute) as DisplayAttribute; if (dsp != null) { extAttr.Name = dsp.Name; } BrowsableAttribute brw = attrs.FirstOrDefault(attr => attr is BrowsableAttribute) as BrowsableAttribute; if (brw != null) { extAttr.Browsable = brw.Browsable; } if (extAttr.DataType == ExtDataType.Auto) { Type type = prop.PropertyType; if (type.IsNullableType()) { type = type.GetGenericArguments()[0]; } if (type.IsEnum) { } else if (type == typeof(DateTime)) { extAttr.DataType = ExtDataType.Date; } else if (type == typeof(bool)) { extAttr.DataType = ExtDataType.Bool; } else if (type == typeof(decimal)) { extAttr.DataType = ExtDataType.Currency; } else if (type == typeof(float) || type == typeof(double)) { extAttr.DataType = ExtDataType.FloatNumber; } else if (type.IsValueType) { extAttr.DataType = ExtDataType.SingleNumber; } } if (!extAttr.LinkedProperty.IsEmpty() && extAttr.DisplayProperty.IsEmpty()) { extAttr.DisplayProperty = extAttr.Name; } if (extAttr.DisplayFormat == null) { switch (extAttr.DataType) { case ExtDataType.Date: extAttr.DisplayFormat = "yyyy-MM-dd"; break; case ExtDataType.DateAndTime: extAttr.DisplayFormat = "yyyy-MM-dd HH:mm:ss"; break; case ExtDataType.FloatNumber: extAttr.DisplayFormat = "#0.00"; //extAttr.MaxValue = int.MaxValue; break; case ExtDataType.SingleNumber: extAttr.DisplayFormat = "#0"; //extAttr.MaxValue = int.MaxValue; break; case ExtDataType.Currency: char currChar = 100.ToString("C")[0]; extAttr.DisplayFormat = currChar + "#0.00"; //extAttr.MaxValue = int.MaxValue; break; case ExtDataType.Percent: extAttr.DisplayFormat = "p2"; //extAttr.MaxValue = 1; break; case ExtDataType.Time: extAttr.DisplayFormat = "H:mm"; break; } } if (extAttr.InputFormat == null) { extAttr.InputFormat = extAttr.DisplayFormat; } switch (extAttr.DataType) { case ExtDataType.FloatNumber: case ExtDataType.SingleNumber: case ExtDataType.Currency: case ExtDataType.Percent: if (extAttr.MaxValue == null && extAttr.MinValue == null) { extAttr.MinValue = 0; extAttr.MaxValue = int.MaxValue; } break; } extAttr.Property = prop; return(extAttr); }