protected virtual void Fields_AfterItemAdd(ModelField item) { if (item.Validations != null) { this.Validations.AddRange(item.Validations); } }
/// <summary> /// /// </summary> public ModelField.Builder ModelField(ModelField component) { #if MVC component.ViewContext = this.HtmlHelper != null ? this.HtmlHelper.ViewContext : null; #endif return(new ModelField.Builder(component)); }
public virtual void RemoveField(ModelField field) { this.Call("removeField", new JRawValue(new ClientConfig().Serialize(field))); }
public virtual void AddField(ModelField field, int index, bool rebuildMeta) { if (this.Model.Primary != null) { if (index >= 0 && index < this.Model.Primary.Fields.Count) { this.Model.Primary.Fields.Insert(index, field); } else { this.Model.Primary.Fields.Add(field); } } this.Call("addField", new JRawValue(new ClientConfig().Serialize(field)), index, rebuildMeta); }
/// <summary> /// /// </summary> public virtual void AddField(ModelField field, int index) { this.AddField(field, index, true); }
public virtual void AddField(ModelField field) { this.AddField(field, true); }
public virtual void AddField(ModelField field, bool rebuildMeta) { this.AddField(field, -1, rebuildMeta); }
/// <summary> /// /// </summary> public virtual TBuilder RemoveField(ModelField field) { this.ToComponent().RemoveField(field); return(this as TBuilder); }
/// <summary> /// /// </summary> public ModelField.Builder ModelField(ModelField component) { return(new ModelField.Builder(component)); }
public static ModelField CreateModelFieldFromMeta(ModelMetadata propertyMetadata, Ext.Net.Model storeModel = null, ModelFieldAttribute modelAttr = null, ControllerContext controllerContext = null) { var modelField = new ModelField(); if (modelAttr == null) { modelAttr = propertyMetadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (ModelFieldAttribute)propertyMetadata.AdditionalValues[ModelFieldAttribute.KEY] : null; } modelField.Name = propertyMetadata.PropertyName; if (Store.IsDate(propertyMetadata.ModelType)) { modelField.Type = ModelFieldType.Date; } else if (Store.IsInteger(propertyMetadata.ModelType)) { modelField.Type = ModelFieldType.Int; } else if (Store.IsFloat(propertyMetadata.ModelType)) { modelField.Type = ModelFieldType.Float; } else if (Store.IsBoolean(propertyMetadata.ModelType)) { modelField.Type = ModelFieldType.Boolean; } else if (propertyMetadata.ModelType == typeof(string)) { modelField.Type = ModelFieldType.String; } if (propertyMetadata.IsComplexType && modelAttr != null) { modelField.IsComplex = true; } if (modelAttr != null && storeModel != null) { modelAttr.CopyTo(modelField, storeModel); } if(storeModel != null) { storeModel.Fields.Add(modelField); } ValidationCollection validations = new ValidationCollection(); if (propertyMetadata.AdditionalValues.ContainsKey(AbstractValidationAttribute.KEY)) { validations = (ValidationCollection)propertyMetadata.AdditionalValues[AbstractValidationAttribute.KEY]; } foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext)) { var rule = v.GetClientValidationRules().FirstOrDefault(); if (rule != null) { switch (rule.ValidationType) { case "required": validations.Add(new PresenceValidation { Message = rule.ErrorMessage }); break; case "regex": if (rule.ValidationParameters.ContainsKey("pattern")) { validations.Add(new FormatValidation { Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage }); } break; case "length": int num; LengthValidation length = new LengthValidation(); length.Message = rule.ErrorMessage; if (rule.ValidationParameters.ContainsKey("max")) { if (int.TryParse(rule.ValidationParameters["max"].ToString(), out num)) { length.Max = num; } else { continue; } } if (rule.ValidationParameters.ContainsKey("min")) { if (int.TryParse(rule.ValidationParameters["min"].ToString(), out num)) { length.Min = num; } else { continue; } } validations.Add(length); break; } } } if (controllerContext != null) { foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext)) { var rule = v.GetClientValidationRules().FirstOrDefault(); if (rule != null) { switch (rule.ValidationType) { case "required": validations.Add(new PresenceValidation { Field = modelField.Name, Message = rule.ErrorMessage }); break; case "regex": if (rule.ValidationParameters.ContainsKey("pattern")) { validations.Add(new FormatValidation { Field = modelField.Name, Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage }); } break; case "length": var lengthVal = new LengthValidation(); if (rule.ValidationParameters.ContainsKey("max")) { lengthVal.Max = (int)Convert.ChangeType(rule.ValidationParameters["max"], typeof(int)); } if (rule.ValidationParameters.ContainsKey("min")) { lengthVal.Min = (int)Convert.ChangeType(rule.ValidationParameters["min"], typeof(int)); } lengthVal.Message = rule.ErrorMessage; validations.Add(lengthVal); break; } } } } if (validations.Count > 0) { if (storeModel != null) { storeModel.Validations.AddRange(validations); } else { modelField.Validations = validations; } } return modelField; }
public virtual void InitByType(Type type, bool extractGeneric = true) { if (extractGeneric) { type = Store.GetEnumerableGenericType(type); } ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, type); if (metadata != null) { var storeModel = this.Model.Primary; if (storeModel == null) { storeModel = new Model(); this.Model.Add(storeModel); } ModelAttribute storeModelAttr = metadata.AdditionalValues.ContainsKey(ModelAttribute.KEY) ? (ModelAttribute)metadata.AdditionalValues[ModelAttribute.KEY] : null; if (storeModelAttr != null) { storeModelAttr.CopyTo(storeModel); } if (metadata.AdditionalValues.ContainsKey(AbstractValidationAttribute.KEY)) { storeModel.Validations.AddRange((ValidationCollection)metadata.AdditionalValues[AbstractValidationAttribute.KEY]); } if (metadata.AdditionalValues.ContainsKey(AbstractAssociationAttribute.KEY)) { storeModel.Associations.AddRange((AssociationCollection)metadata.AdditionalValues[AbstractAssociationAttribute.KEY]); } if (metadata.AdditionalValues.ContainsKey(ProxyAttribute.KEY)) { this.Proxy.Add((AbstractProxy)metadata.AdditionalValues[ProxyAttribute.KEY]); } if (metadata.AdditionalValues.ContainsKey(AbstractReaderAttribute.KEY)) { if (this.Proxy.Primary != null && this.Proxy.Primary is ServerProxy) { ((ServerProxy)this.Proxy.Primary).Reader.Add((AbstractReader)metadata.AdditionalValues[AbstractReaderAttribute.KEY]); } else { this.Reader.Add((AbstractReader)metadata.AdditionalValues[AbstractReaderAttribute.KEY]); } } if (metadata.AdditionalValues.ContainsKey(AbstractWriterAttribute.KEY)) { if (this.Proxy.Primary != null && this.Proxy.Primary is ServerProxy) { ((ServerProxy)this.Proxy.Primary).Writer.Add((AbstractWriter)metadata.AdditionalValues[AbstractWriterAttribute.KEY]); } else { this.Writer.Add((AbstractWriter)metadata.AdditionalValues[AbstractWriterAttribute.KEY]); } } var virtualFieldsAttr = metadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (List<VirtualModelField>)metadata.AdditionalValues[ModelFieldAttribute.KEY] : null; if (virtualFieldsAttr != null) { foreach (VirtualModelField attr in virtualFieldsAttr) { var modelField = new ModelField(); if (storeModel != null) { attr.CopyTo(modelField, storeModel); storeModel.Fields.Add(modelField); } } } var registrator = metadata.AdditionalValues.ContainsKey(ClientResourceAttribute.KEY) ? (ResourcesRegistrator)metadata.AdditionalValues[ClientResourceAttribute.KEY] : null; if (registrator != null) { this.Controls.Add(registrator); } foreach (ModelMetadata propertyMetadata in metadata.Properties) { var modelAttr = propertyMetadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (ModelFieldAttribute)propertyMetadata.AdditionalValues[ModelFieldAttribute.KEY] : null; if (modelAttr == null && this.InitForModelOnly) { continue; } if (modelAttr != null && modelAttr.Ignore) { continue; } Store.CreateModelFieldFromMeta(propertyMetadata, storeModel, modelAttr, this.ControllerContext); } } }
public static ModelField CreateModelFieldFromMeta(ModelMetadata propertyMetadata, Ext.Net.Model storeModel = null, ModelFieldAttribute modelAttr = null, ControllerContext controllerContext = null) { var modelField = new ModelField(); if (modelAttr == null) { modelAttr = propertyMetadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (ModelFieldAttribute)propertyMetadata.AdditionalValues[ModelFieldAttribute.KEY] : null; } modelField.Name = propertyMetadata.PropertyName; if (Store.IsDate(propertyMetadata.ModelType)) { modelField.Type = ModelFieldType.Date; } else if (Store.IsInteger(propertyMetadata.ModelType)) { modelField.Type = ModelFieldType.Int; } else if (Store.IsFloat(propertyMetadata.ModelType)) { modelField.Type = ModelFieldType.Float; } else if (Store.IsBoolean(propertyMetadata.ModelType)) { modelField.Type = ModelFieldType.Boolean; } else if (propertyMetadata.ModelType == typeof(string)) { modelField.Type = ModelFieldType.String; } if (propertyMetadata.IsComplexType && modelAttr != null) { modelField.IsComplex = true; } if (modelAttr != null && storeModel != null) { modelAttr.CopyTo(modelField, storeModel); } if (storeModel != null) { storeModel.Fields.Add(modelField); } ValidationCollection validations = new ValidationCollection(); if (propertyMetadata.AdditionalValues.ContainsKey(AbstractValidationAttribute.KEY)) { validations = (ValidationCollection)propertyMetadata.AdditionalValues[AbstractValidationAttribute.KEY]; } foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext)) { var rule = v.GetClientValidationRules().FirstOrDefault(); if (rule != null) { switch (rule.ValidationType) { case "required": validations.Add(new PresenceValidation { Message = rule.ErrorMessage }); break; case "regex": if (rule.ValidationParameters.ContainsKey("pattern")) { validations.Add(new FormatValidation { Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage }); } break; case "length": int num; LengthValidation length = new LengthValidation(); length.Message = rule.ErrorMessage; if (rule.ValidationParameters.ContainsKey("max")) { if (int.TryParse(rule.ValidationParameters["max"].ToString(), out num)) { length.Max = num; } else { continue; } } if (rule.ValidationParameters.ContainsKey("min")) { if (int.TryParse(rule.ValidationParameters["min"].ToString(), out num)) { length.Min = num; } else { continue; } } validations.Add(length); break; } } } if (controllerContext != null) { foreach (ModelValidator v in propertyMetadata.GetValidators(controllerContext)) { var rule = v.GetClientValidationRules().FirstOrDefault(); if (rule != null) { switch (rule.ValidationType) { case "required": validations.Add(new PresenceValidation { Field = modelField.Name, Message = rule.ErrorMessage }); break; case "regex": if (rule.ValidationParameters.ContainsKey("pattern")) { validations.Add(new FormatValidation { Field = modelField.Name, Matcher = rule.ValidationParameters["pattern"].ToString(), Message = rule.ErrorMessage }); } break; case "length": var lengthVal = new LengthValidation(); if (rule.ValidationParameters.ContainsKey("max")) { lengthVal.Max = (int)Convert.ChangeType(rule.ValidationParameters["max"], typeof(int)); } if (rule.ValidationParameters.ContainsKey("min")) { lengthVal.Min = (int)Convert.ChangeType(rule.ValidationParameters["min"], typeof(int)); } lengthVal.Message = rule.ErrorMessage; validations.Add(lengthVal); break; } } } } if (validations.Count > 0) { if (storeModel != null) { storeModel.Validations.AddRange(validations); } else { modelField.Validations = validations; } } return(modelField); }
public virtual void InitByType(Type type, bool extractGeneric = true) { if (extractGeneric) { type = Store.GetEnumerableGenericType(type); } ModelMetadata metadata = ModelMetadataProviders.Current.GetMetadataForType(null, type); if (metadata != null) { var storeModel = this.Model.Primary; if (storeModel == null) { storeModel = new Model(); this.Model.Add(storeModel); } ModelAttribute storeModelAttr = metadata.AdditionalValues.ContainsKey(ModelAttribute.KEY) ? (ModelAttribute)metadata.AdditionalValues[ModelAttribute.KEY] : null; if (storeModelAttr != null) { storeModelAttr.CopyTo(storeModel); } if (metadata.AdditionalValues.ContainsKey(AbstractValidationAttribute.KEY)) { storeModel.Validations.AddRange((ValidationCollection)metadata.AdditionalValues[AbstractValidationAttribute.KEY]); } if (metadata.AdditionalValues.ContainsKey(AbstractAssociationAttribute.KEY)) { storeModel.Associations.AddRange((AssociationCollection)metadata.AdditionalValues[AbstractAssociationAttribute.KEY]); } if (metadata.AdditionalValues.ContainsKey(ProxyAttribute.KEY)) { this.Proxy.Add((AbstractProxy)metadata.AdditionalValues[ProxyAttribute.KEY]); } if (metadata.AdditionalValues.ContainsKey(AbstractReaderAttribute.KEY)) { if (this.Proxy.Primary != null && this.Proxy.Primary is ServerProxy) { ((ServerProxy)this.Proxy.Primary).Reader.Add((AbstractReader)metadata.AdditionalValues[AbstractReaderAttribute.KEY]); } else { this.Reader.Add((AbstractReader)metadata.AdditionalValues[AbstractReaderAttribute.KEY]); } } if (metadata.AdditionalValues.ContainsKey(AbstractWriterAttribute.KEY)) { if (this.Proxy.Primary != null && this.Proxy.Primary is ServerProxy) { ((ServerProxy)this.Proxy.Primary).Writer.Add((AbstractWriter)metadata.AdditionalValues[AbstractWriterAttribute.KEY]); } else { this.Writer.Add((AbstractWriter)metadata.AdditionalValues[AbstractWriterAttribute.KEY]); } } var virtualFieldsAttr = metadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (List <VirtualModelField>)metadata.AdditionalValues[ModelFieldAttribute.KEY] : null; if (virtualFieldsAttr != null) { foreach (VirtualModelField attr in virtualFieldsAttr) { var modelField = new ModelField(); if (storeModel != null) { attr.CopyTo(modelField, storeModel); storeModel.Fields.Add(modelField); } } } var registrator = metadata.AdditionalValues.ContainsKey(ClientResourceAttribute.KEY) ? (ResourcesRegistrator)metadata.AdditionalValues[ClientResourceAttribute.KEY] : null; if (registrator != null) { this.Controls.Add(registrator); } foreach (ModelMetadata propertyMetadata in metadata.Properties) { var modelAttr = propertyMetadata.AdditionalValues.ContainsKey(ModelFieldAttribute.KEY) ? (ModelFieldAttribute)propertyMetadata.AdditionalValues[ModelFieldAttribute.KEY] : null; if (modelAttr == null && this.InitForModelOnly) { continue; } if (modelAttr != null && modelAttr.Ignore) { continue; } Store.CreateModelFieldFromMeta(propertyMetadata, storeModel, modelAttr, this.ControllerContext); } } }
private Store BuildStore(DataTable dt) { Store store = new Store(); Model md = new Model(); for (int i = 0; i < dt.Columns.Count; i++) { ModelField mdf = new ModelField(dt.Columns[i].ToString()); md.Fields.Add(mdf); } store.Model.Add(md); store.DataSource = dt; return store; }
private object GetFieldValue(AutoGeneratedFieldProperties property, object obj, ModelField field, System.Data.DataRow dataRow, out bool notFound) { notFound = false; if (field != null && field.ServerMapping.IsNotEmpty()) { string[] mapping = field.ServerMapping.Split('.'); if (mapping.Length > 1) { for (int i = 0; i < mapping.Length; i++) { if (dataRow != null && dataRow.IsNull(mapping[i])) { return null; } PropertyInfo p = obj.GetType().GetProperty(mapping[i]); if (p == null) { notFound = true; return null; } obj = p.GetValue(obj, null); if (obj == null) { return null; } } return obj; } } if (dataRow == null) { var prop = TypeDescriptor.GetProperties(obj).Find(property.DataField, true); if (prop != null) { return prop.GetValue(obj); } } else if(!dataRow.IsNull(property.DataField)) { return dataRow[property.DataField]; } notFound = true; return null; }
/// <summary> /// /// </summary> public virtual TBuilder AddField(ModelField field, int index, bool rebuildMeta) { this.ToComponent().AddField(field, index, rebuildMeta); return(this as TBuilder); }
private object GetFieldValue(PropertyDescriptorCollection props, object item, ModelField field) { if (field != null && field.ServerMapping.IsNotEmpty()) { string[] mapping = field.ServerMapping.Split('.'); if (mapping.Length > 1) { for (int i = 0; i < mapping.Length; i++) { PropertyInfo p = item.GetType().GetProperty(mapping[i]); item = p.GetValue(item, null); if (item == null) { return null; } } return item; } } var fieldName = string.IsNullOrEmpty(field.Mapping) ? field.Name : field.Mapping; var desc = props.Find(fieldName, true); return desc != null ? desc.GetValue(item) : null; }
private void InitModel() { if (this.ItemType != null) { Model item = new Model(); PropertyInfo[] infoArray2 = this.ItemType.GetProperties(); GridFilters filters = new GridFilters(); filters.MenuFilterText = "搜索"; filters.ID = "filters" + ID; for (int i = 0; i < infoArray2.Length; i++) { Func<ColumnBase, bool> predicate = null; PropertyInfo property = infoArray2[i]; ModelField field = new ModelField { Name = property.Name }; #region 映射字段类型 string fullName = property.PropertyType.FullName; if (fullName.Contains("System.Int")) { var enumDataTypeAttr = AttributeHelper.GetAttribute<EnumDataTypeAttribute>(property); if (enumDataTypeAttr == null) { field.Type = ModelFieldType.Int; } } else if (fullName.Contains("System.DateTime")) { field.Type = ModelFieldType.Date; } else if (fullName.Contains("System.Single")) { field.Type = ModelFieldType.Float; } else if (fullName.Contains("System.Boolean")) { field.Type = ModelFieldType.Boolean; } else if (fullName.Contains("System.String")) { field.Type = ModelFieldType.String; } else if (!property.PropertyType.IsPrimitive) { field.Type = ModelFieldType.Object; } else { field.Type = ModelFieldType.Auto; } #endregion item.Fields.Add(field); if (predicate == null) { predicate = x => x.DataIndex == property.Name; } ColumnBase column = this.ColumnModel.Columns.FirstOrDefault<ColumnBase>(predicate); if (column == null) { ColumnBase base2; DataGridColumnAttribute displayAttr = AttributeHelper.GetAttribute<DataGridColumnAttribute>(property); FilterAttribute filterAttr = AttributeHelper.GetAttribute<FilterAttribute>(property); bool simpleFilterable = filterAttr != null && filterAttr.Enabled && filterAttr.FilterType == null; GridFilter filter = null; #region 映射列类型 if (fullName.Contains("System.Int") || fullName.Contains("System.Single") || fullName.Contains("System.Decimal") || fullName.Contains("System.Double")) { NumberColumn column1 = new NumberColumn { Format = this.GetFormat(displayAttr, "0") }; base2 = column1; if (simpleFilterable && filterAttr.FilterType == null) { filter = new NumericFilter(); } } else if (fullName.Contains("System.DateTime")) { DateColumn column2 = new DateColumn { Format = this.GetFormat(displayAttr, "Y-m-d") }; base2 = column2; if (simpleFilterable) { filter = new DateFilter() { AfterText = "在这之后", BeforeText = "在这之前", OnText = "在这天" }; } } else if (fullName.Contains("System.Boolean")) { string[] strArray = this.GetFormat(displayAttr, "是|否").Split(new char[] { '|' }); BooleanColumn column4 = new BooleanColumn { TrueText = strArray[0], FalseText = strArray[1] }; base2 = column4; if (simpleFilterable) { filter = new BooleanFilter() { NoText = strArray[1], YesText = strArray[0] }; } } else if (fullName.Contains("System.String")) { base2 = new Column(); if (simpleFilterable) { filter = new StringFilter(); } } else if (!property.PropertyType.IsPrimitive) { base2 = new Column(); if (simpleFilterable) { filter = new StringFilter(); } } else { base2 = new Column(); if (simpleFilterable) { filter = new StringFilter(); } } #endregion #region 生成外键过滤器 if (filterAttr != null && filterAttr.Enabled && filterAttr.FilterType != null) { Store store = new Store(); store.ID = "filterStore" + property.Name; if (filterAttr.FilterType.IsEnum) { var valueType = filterAttr.FilterType; var values = Enum.GetValues(valueType); var names = Enum.GetNames(valueType); List<KeyValueModel> enumDict = new List<KeyValueModel>(); for (int it = 0; it < values.Length; it++) { var fieldInfo = valueType.GetField(names[it]); var descAttr = AttributeHelper.GetAttribute<DescriptionAttribute>(fieldInfo); if (descAttr == null) { throw new Exception("枚举必须要有Description"); } int key = (int)values.GetValue(it); enumDict.Add(new KeyValueModel() { Id = key, Name = descAttr.Description }); } Model model = new Model(); model.Fields.Add(new ModelField("Id", ModelFieldType.Int)); model.Fields.Add(new ModelField("Name", ModelFieldType.String)); store.Model.Add(model); store.DataSource = enumDict; store.DataBind(); filter = new ListFilter() { StoreID = store.ID, Single = true, IDField = "Id", LabelField = "Name" }; } else { var filterObject = Activator.CreateInstance(filterAttr.FilterType); var filterGen = filterObject as ForeignFilterBase; if (filterGen == null) { throw new ArgumentException("FilterAttribute中的FilterType的类型必须为ForeignFilterBase的子类或枚举类型"); } store.Model.Add(filterGen.GetModel()); store.DataSource = filterGen.GetData(); store.DataBind(); filter = new ListFilter() { StoreID = store.ID, Single = true, IDField = filterGen.IdField, LabelField = filterGen.LabelField }; } Bin.Add(store); } #endregion base2.Text = (displayAttr == null) ? property.Name : displayAttr.DisplayName; base2.DataIndex = property.Name; if (displayAttr != null && displayAttr.Width != -1) base2.Width = displayAttr.Width; if (displayAttr != null && displayAttr.ValueType != null) { Type valueType = displayAttr.ValueType; var values = Enum.GetValues(valueType); var names = Enum.GetNames(valueType); Dictionary<int, string> enumDict = new Dictionary<int, string>(); List<string> valueList = new List<string>(); for (int it = 0; it < values.Length; it++) { var fieldInfo = valueType.GetField(names[it]); var descAttr = AttributeHelper.GetAttribute<DescriptionAttribute>(fieldInfo); int key = (int)values.GetValue(it); enumDict.Add(key, descAttr.Description); valueList.Add("values[\"" + names[it] + "\"]=\"" + (descAttr != null ? descAttr.Description : names[it]) + "\";"); } base2.Renderer.Fn = "function(){var values=Array();" + string.Join(string.Empty, valueList) + "return values[arguments[0]];}"; } if (filter != null) { filter.DataIndex = property.Name; filters.Filters.Add(filter); } column = base2; this.ColumnModel.Columns.Add(base2); } column.TabIndex = (short)i; } Features.Add(filters); if (this.ColumnModel.Columns.Count >= 10) { this.AutoScroll = true; } var list = ColumnModel.Columns.OrderBy(x => x.TabIndex).ToList(); ColumnModel.Columns.Clear(); ColumnModel.Columns.AddRange(list); this._store.Model.Add(item); } }