/// <summary> /// /// </summary> public GridFilters.Builder GridFilters(GridFilters component) { #if MVC component.ViewContext = this.HtmlHelper != null ? this.HtmlHelper.ViewContext : null; #endif return(new GridFilters.Builder(component)); }
/// <summary> /// /// </summary> public GridFilters.Builder GridFilters(GridFilters component) { return(new GridFilters.Builder(component)); }
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); } }