From 83a4815ebe1b53cf31e3976751799078c22e8f5d Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 22 Oct 2021 13:45:09 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E5=AF=B9?= =?UTF-8?q?=E5=8A=A8=E6=80=81=E7=B1=BB=E5=9E=8B=E6=96=B0=E5=BB=BA=E6=94=AF?= =?UTF-8?q?=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs index 3f021d1a2..e1aeb054c 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs @@ -228,7 +228,7 @@ namespace BootstrapBlazor.Components /// public async Task AddAsync() { - if (IsExcel) + if (IsExcel || DynamicContext != null) { await AddDynamicOjbectExcelModelAsync(); } -- Gitee From f084c809bb7ec74011b96d81fd874582ef1d35d4 Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 22 Oct 2021 15:10:29 +0800 Subject: [PATCH 2/5] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E6=89=A9?= =?UTF-8?q?=E5=B1=95=E6=96=B9=E6=B3=95=20SetValue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DynamicObjectContextExtensions.cs | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs b/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs index 673fa2be7..0d84b7f66 100644 --- a/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs +++ b/src/BootstrapBlazor/Extensions/DynamicObjectContextExtensions.cs @@ -6,7 +6,9 @@ using System; using System.Collections.Generic; using System.ComponentModel; using System.ComponentModel.DataAnnotations; +using System.Linq; using System.Reflection; +using System.Threading.Tasks; namespace BootstrapBlazor.Components { @@ -88,5 +90,25 @@ namespace BootstrapBlazor.Components var type = typeof(DescriptionAttribute); context.AddAttribute(columnName, type, new Type[] { typeof(string) }, new object?[] { description }); } + + /// + /// 扩展方法将指定模型赋值给 context 实例 + /// + /// DynamicObjectContext 实例 + /// 模型实例 + public static async Task SetValue(this IDynamicObjectContext context, object model) + { + if (model is IDynamicObject v) + { + var item = context.GetItems().FirstOrDefault(i => i.DynamicObjectPrimaryKey == v.DynamicObjectPrimaryKey); + if (item != null && context.OnValueChanged != null) + { + foreach (var col in context.GetColumns()) + { + await context.OnValueChanged(item, col, v.GetValue(col.GetFieldName())); + } + } + } + } } } -- Gitee From 5b69fe1fa1239dcaf440e8e5585f0c036ca4d77b Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 22 Oct 2021 15:33:23 +0800 Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20=E5=85=BC=E5=AE=B9=E5=8A=A8?= =?UTF-8?q?=E6=80=81=E7=A8=8B=E5=BA=8F=E9=9B=86=E9=98=B2=E6=AD=A2=E5=86=85?= =?UTF-8?q?=E5=AD=98=E6=BA=A2=E5=87=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Extensions/LambdaExtensions.cs | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/BootstrapBlazor/Extensions/LambdaExtensions.cs b/src/BootstrapBlazor/Extensions/LambdaExtensions.cs index 727a3c7de..588ec2281 100644 --- a/src/BootstrapBlazor/Extensions/LambdaExtensions.cs +++ b/src/BootstrapBlazor/Extensions/LambdaExtensions.cs @@ -382,7 +382,7 @@ namespace System.Linq return Expression.Lambda>(body, param_p1, param_p2); } - private static readonly ConcurrentDictionary<(Type ModelType, string FieldName), Func> PropertyValueInvokerCache = new(); + private static readonly ConcurrentDictionary<(Type ModelType, string FieldName), Func> PropertyValueInvokerCache = new(); /// /// 获取 指定对象的属性值 @@ -390,11 +390,27 @@ namespace System.Linq /// /// /// - public static object GetPropertyValue(object model, string fieldName) + public static object? GetPropertyValue(object model, string fieldName) { - var cacheKey = (model.GetType(), fieldName); - var invoker = PropertyValueInvokerCache.GetOrAdd(cacheKey, key => GetPropertyValueLambda(model, key.FieldName).Compile()); - return invoker.Invoke(model); + return model.GetType().Assembly.IsDynamic ? ReflectionInvoke() : LambdaInvoke(); + + object? ReflectionInvoke() + { + object? ret = null; + var propertyInfo = model.GetType().GetProperties().FirstOrDefault(i => i.Name == fieldName); + if (propertyInfo != null) + { + ret = propertyInfo.GetValue(model); + } + return ret; + } + + object? LambdaInvoke() + { + var cacheKey = (model.GetType(), fieldName); + var invoker = PropertyValueInvokerCache.GetOrAdd(cacheKey, key => GetPropertyValueLambda(model, key.FieldName).Compile()); + return invoker.Invoke(model); + } } #region TryParse -- Gitee From 3de5bab01dd9c646d8e71f398b2901776e2a984c Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 22 Oct 2021 15:33:49 +0800 Subject: [PATCH 4/5] =?UTF-8?q?feat:=20Table=20=E7=BB=84=E4=BB=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E5=8A=A8=E6=80=81=E7=B1=BB=E5=9E=8B=E7=9A=84=E6=96=B0?= =?UTF-8?q?=E5=BB=BA=E4=B8=8E=E7=BC=96=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Table/Dynamic/DataTableDynamicContext.cs | 1 + .../Table/Dynamic/DataTableDynamicObject.cs | 3 ++- .../Components/Table/Table.razor.Toolbar.cs | 12 +++++++++--- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicContext.cs b/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicContext.cs index 0057821ff..12e0aeab0 100644 --- a/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicContext.cs +++ b/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicContext.cs @@ -286,6 +286,7 @@ namespace BootstrapBlazor.Components if (Caches.TryGetValue(item.DynamicObjectPrimaryKey, out var cacheItem)) { cacheItem.Row[column.GetFieldName()] = val; + Items = null; } return Task.CompletedTask; } diff --git a/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicObject.cs b/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicObject.cs index ce6da45b2..1ab1908b9 100644 --- a/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicObject.cs +++ b/src/BootstrapBlazor/Components/Table/Dynamic/DataTableDynamicObject.cs @@ -3,6 +3,7 @@ // Website: https://www.blazor.zone or https://argozhang.github.io/ using System.Data; +using System.Linq; namespace BootstrapBlazor.Components { @@ -28,7 +29,7 @@ namespace BootstrapBlazor.Components { ret = Row[propertyName]; } - return ret; + return ret ?? LambdaExtensions.GetPropertyValue(this, propertyName); } /// diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs index e1aeb054c..972469ebe 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs @@ -333,7 +333,7 @@ namespace BootstrapBlazor.Components /// public async Task EditAsync() { - if (UseInjectDataService || IsTracking || OnSaveAsync != null) + if (UseInjectDataService || IsTracking || OnSaveAsync != null || DynamicContext != null) { if (SelectedItems.Count == 1) { @@ -425,7 +425,13 @@ namespace BootstrapBlazor.Components protected async Task SaveModelAsync(EditContext context, ItemChangedType changedType) { var valid = false; - if (OnSaveAsync != null) + if (DynamicContext != null) + { + await DynamicContext.SetValue(context.Model); + RowItemsCache = null; + valid = true; + } + else if (OnSaveAsync != null) { valid = await OnSaveAsync((TItem)context.Model, changedType); } @@ -455,7 +461,7 @@ namespace BootstrapBlazor.Components /// protected async Task SaveAsync(EditContext context, ItemChangedType changedType) { - if (UseInjectDataService || OnSaveAsync != null) + if (UseInjectDataService || OnSaveAsync != null || DynamicContext != null) { await ToggleLoading(true); if (await SaveModelAsync(context, changedType)) -- Gitee From 90c89df54aad0e9ddda25ae904e98b1696f6f6dd Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Fri, 22 Oct 2021 15:35:54 +0800 Subject: [PATCH 5/5] chore: bump version to 5.16.2 --- src/BootstrapBlazor/BootstrapBlazor.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/BootstrapBlazor/BootstrapBlazor.csproj b/src/BootstrapBlazor/BootstrapBlazor.csproj index 7cd72f5e4..b9c0d8d28 100644 --- a/src/BootstrapBlazor/BootstrapBlazor.csproj +++ b/src/BootstrapBlazor/BootstrapBlazor.csproj @@ -1,7 +1,7 @@ - 5.16.1 + 5.16.2 -- Gitee