/// <summary> /// 通过指定 Model 获得 IEditorItem 集合方法 /// </summary> /// <param name="source"></param> /// <param name="predicate"></param> /// <returns></returns> public static IEnumerable <IEditorItem> GenerateColumns <TModel>(this TModel source, Func <IEditorItem, bool>?predicate = null) where TModel : class { if (predicate == null) { predicate = p => true; } return(InternalTableColumn.GetProperties <TModel>().Where(predicate)); }
public static IEnumerable <ITableColumn> GetProperties <TModel>(IEnumerable <ITableColumn>?source = null) { var cols = new List <ITableColumn>(50); var type = typeof(TModel); var attrModel = type.GetCustomAttribute <AutoGenerateClassAttribute>(); var props = type.GetProperties(); foreach (var prop in props) { ITableColumn?tc; var attr = prop.GetCustomAttribute <AutoGenerateColumnAttribute>(); // Issue: 增加定义设置标签 AutoGenerateClassAttribute // https://gitee.com/LongbowEnterprise/BootstrapBlazor/issues/I381ED var displayName = attr?.Text ?? Utility.GetDisplayName(type, prop.Name); if (attr == null) { tc = new InternalTableColumn(prop.Name, prop.PropertyType, displayName); if (attrModel != null) { InheritValue(attrModel, tc); } } else { if (attr.Ignore) { continue; } attr.Text = displayName; attr.FieldName = prop.Name; attr.PropertyType = prop.PropertyType; if (attrModel != null) { InheritValue(attrModel, attr); } tc = attr; } // 替换属性 手写优先 var col = source?.FirstOrDefault(c => c.GetFieldName() == tc.GetFieldName()); if (col != null) { CopyValue(col, tc); } cols.Add(tc); } return(cols.Where(a => a.Order > 0).OrderBy(a => a.Order) .Concat(cols.Where(a => a.Order == 0)) .Concat(cols.Where(a => a.Order < 0).OrderBy(a => a.Order))); }
/// <summary> /// OnAfterRenderAsync 方法 /// </summary> /// <param name="firstRender"></param> /// <returns></returns> protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); if (firstRender) { FirstRender = false; if (CascadeEditorItems != null) { // 通过级联参数渲染组件 FormItems.AddRange(CascadeEditorItems); } else { // 如果 EditorItems 有值表示 用户自定义列 if (AutoGenerateAllItem) { // 获取绑定模型所有属性 var items = InternalTableColumn.GetProperties <TModel>().ToList(); // 通过设定的 FieldItems 模板获取项进行渲染 foreach (var el in EditorItems) { var item = items.FirstOrDefault(i => i.GetFieldName() == el.GetFieldName()); if (item != null) { // 过滤掉不编辑的列 if (!el.Editable) { items.Remove(item); } else { // 设置只读属性与列模板 item.Readonly = el.Readonly; item.EditTemplate = el.EditTemplate; item.Text = el.Text; item.Data = el.Data; } } } FormItems.AddRange(items); } else { FormItems.AddRange(EditorItems); } } StateHasChanged(); } }
public static IEnumerable <ITableColumn> GetProperties <TModel>(IEnumerable <ITableColumn>?source = null) { var cols = new List <ITableColumn>(50); var type = typeof(TModel); var props = type.GetProperties(); foreach (var prop in props) { ITableColumn?tc; var attr = prop.GetCustomAttribute <AutoGenerateColumnAttribute>(); if (attr == null) { tc = new InternalTableColumn(prop.Name, prop.PropertyType, type.GetDisplayName(prop.Name)); } else { if (attr.Ignore) { continue; } attr.Text = type.GetDisplayName(prop.Name); attr.FieldName = prop.Name; attr.PropertyType = prop.PropertyType; tc = attr; } // 替换属性 手写优先 var col = source?.FirstOrDefault(c => c.GetFieldName() == tc.GetFieldName()); if (col != null) { CopyValue(col, tc); } cols.Add(tc); } return(cols.Where(a => a.Order > 0).OrderBy(a => a.Order) .Concat(cols.Where(a => a.Order == 0)) .Concat(cols.Where(a => a.Order < 0).OrderBy(a => a.Order))); }
public static void GetProperties <TModel>(List <ITableColumn> columns) { var cols = new List <ITableColumn>(50); var type = typeof(TModel); var props = type.GetProperties(); foreach (var prop in props) { ITableColumn?tc; var attr = prop.GetCustomAttribute <AutoGenerateColumnAttribute>(); if (attr == null) { tc = new InternalTableColumn(prop.Name, prop.PropertyType, type.GetDisplayName(prop.Name)); } else { if (attr.Ignore) { continue; } attr.Text = type.GetDisplayName(prop.Name); attr.FieldName = prop.Name; attr.PropertyType = prop.PropertyType; tc = attr; } // 替换属性 手写优先 var col = columns.FirstOrDefault(c => c.GetFieldName() == tc.GetFieldName()); if (col != null) { CopyValue(col, tc); } cols.Add(tc); } columns.Clear(); columns.AddRange(cols.OrderBy(c => c.Order)); }
/// <summary> /// OnAfterRenderAsync 方法 /// </summary> /// <param name="firstRender"></param> protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); if (firstRender) { FirstRender = false; methodName = Height.HasValue ? "fixTableHeader" : "init"; ScreenSize = await RetrieveWidth(); // 初始化列 if (AutoGenerateColumns) { InternalTableColumn.GetProperties <TItem>(Columns); } ColumnVisibles = Columns.Select(i => new ColumnVisibleItem { FieldName = i.GetFieldName(), Visible = i.Visible }).ToList(); // set default sortName var col = Columns.FirstOrDefault(i => i.Sortable && i.DefaultSort); if (col != null) { SortName = col.GetFieldName(); SortOrder = col.DefaultSortOrder; } await QueryAsync(); } if (!firstRender) { IsRendered = true; } if (IsRendered) { if (!string.IsNullOrEmpty(methodName)) { // 固定表头脚本关联 await JSRuntime.InvokeVoidAsync(TableElement, "bb_table", methodName); methodName = null; } if (IsAutoRefresh && AutoRefreshInterval > 500 && AutoRefreshCancelTokenSource == null) { AutoRefreshCancelTokenSource = new CancellationTokenSource(); // 自动刷新功能 _ = Task.Run(async() => { while (!AutoRefreshCancelTokenSource.IsCancellationRequested) { await InvokeAsync(QueryAsync); await Task.Delay(AutoRefreshInterval, AutoRefreshCancelTokenSource.Token); } }); } } }
/// <summary> /// OnAfterRenderAsync 方法 /// </summary> /// <param name="firstRender"></param> protected override async Task OnAfterRenderAsync(bool firstRender) { await base.OnAfterRenderAsync(firstRender); if (firstRender) { if (ShowSearch) { // 注册 SeachBox 回调事件 Interop = new JSInterop <Table <TItem> >(JSRuntime); await Interop.Invoke(this, TableElement, "bb_table_search", nameof(OnSearch), nameof(OnClearSearch)); } FirstRender = false; methodName = Height.HasValue ? "fixTableHeader" : "init"; ScreenSize = await RetrieveWidth(); // 初始化列 if (AutoGenerateColumns) { var cols = InternalTableColumn.GetProperties <TItem>(Columns); Columns.Clear(); Columns.AddRange(cols); } ColumnVisibles = Columns.Select(i => new ColumnVisibleItem { FieldName = i.GetFieldName(), Visible = i.Visible }).ToList(); // set default sortName var col = Columns.FirstOrDefault(i => i.Sortable && i.DefaultSort); if (col != null) { SortName = col.GetFieldName(); SortOrder = col.DefaultSortOrder; } await QueryAsync(); } if (!firstRender) { IsRendered = true; } if (IsRendered) { if (IsLoading) { IsLoading = false; var _ = JSRuntime.InvokeVoidAsync(TableElement, "bb_table_load", "hide"); } // fix: https://gitee.com/LongbowEnterprise/BootstrapBlazor/issues/I2AYEH // PR: https://gitee.com/LongbowEnterprise/BootstrapBlazor/pulls/818 if (Columns.Any(col => col.ShowTips) && string.IsNullOrEmpty(methodName)) { methodName = "tooltip"; } if (!string.IsNullOrEmpty(methodName)) { await JSRuntime.InvokeVoidAsync(TableElement, "bb_table", methodName); methodName = null; } if (IsAutoRefresh && AutoRefreshInterval > 500 && AutoRefreshCancelTokenSource == null) { AutoRefreshCancelTokenSource = new CancellationTokenSource(); // 自动刷新功能 _ = Task.Run(async() => { try { while (!(AutoRefreshCancelTokenSource?.IsCancellationRequested ?? true)) { await InvokeAsync(QueryAsync); await Task.Delay(AutoRefreshInterval, AutoRefreshCancelTokenSource?.Token ?? new CancellationToken(true)); } } catch (TaskCanceledException) { } }); } } }
/// <summary> /// 获得指定泛型的 IEditorItem 集合 /// </summary> /// <returns></returns> public static IEnumerable <IEditorItem> GenerateEditorItems() => InternalTableColumn.GetProperties <TValue>();