/// <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)); }
/// <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(); } }
/// <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>();