From c31ab7e2eef49d57115c5763dd307fb6c7686edf Mon Sep 17 00:00:00 2001 From: Argo-Tianyi Date: Tue, 8 Mar 2022 15:53:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E7=B2=BE=E7=AE=80=E5=88=A4?= =?UTF-8?q?=E6=96=AD=E6=A8=A1=E5=9E=8B=E6=98=AF=E5=90=A6=E6=9C=89=20Key=20?= =?UTF-8?q?=E6=A0=87=E7=AD=BE=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Table/Table.razor.Edit.cs | 6 ++--- .../Components/Table/Table.razor.cs | 9 +++++++ .../Components/Table/TableTreeNode.cs | 24 +------------------ 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs index 5a0a503e6..33a30570c 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Edit.cs @@ -4,8 +4,6 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Forms; -using System.ComponentModel.DataAnnotations; -using System.Reflection; namespace BootstrapBlazor.Components; @@ -485,7 +483,7 @@ public partial class Table void ProcessSelectedRows() { // 判断模型是否有 [Key] Id 等可识别字段尝试重构 - if (typeof(TItem).GetRuntimeProperties().Any(p => p.IsDefined(typeof(KeyAttribute)))) + if (HasKeyAttribute) { var rows = new List(); // 更新选中行逻辑 @@ -556,7 +554,7 @@ public partial class Table async Task ProcessTreeData() { KeySet.Clear(); - if (TableTreeNode.HasKey) + if (HasKeyAttribute) { CheckExpandKeys(TreeRows); } diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index a4b2bab9d..f80bbdbe4 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -5,7 +5,9 @@ using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web.Virtualization; using Microsoft.Extensions.Options; +using System.ComponentModel.DataAnnotations; using System.Globalization; +using System.Reflection; namespace BootstrapBlazor.Components; @@ -646,6 +648,11 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable /// 回调配合 private bool OnAfterRenderIsTriggered { get; set; } + /// + /// 获得/设置 模型是否有 [KeyAttribute] 标签 + /// + protected bool HasKeyAttribute { get; set; } + /// /// OnInitialized 方法 /// @@ -679,6 +686,8 @@ public partial class Table : BootstrapComponentBase, IDisposable, ITable await QueryAsync(); }; + HasKeyAttribute = typeof(TItem).GetRuntimeProperties().Any(p => p.IsDefined(typeof(KeyAttribute))); + if (IsTree) { var rows = Items ?? QueryItems ?? Enumerable.Empty(); diff --git a/src/BootstrapBlazor/Components/Table/TableTreeNode.cs b/src/BootstrapBlazor/Components/Table/TableTreeNode.cs index 806206e79..b9d3a3983 100644 --- a/src/BootstrapBlazor/Components/Table/TableTreeNode.cs +++ b/src/BootstrapBlazor/Components/Table/TableTreeNode.cs @@ -2,10 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. // Website: https://www.blazor.zone or https://argozhang.github.io/ -using System.ComponentModel.DataAnnotations; -using System.Linq.Expressions; -using System.Reflection; - namespace BootstrapBlazor.Components; /// @@ -14,24 +10,6 @@ namespace BootstrapBlazor.Components; /// public class TableTreeNode where TItem : class { - private static readonly Lazy> GetKeyFunc = new(() => - { - var property = typeof(TItem).GetProperties().FirstOrDefault(p => p.IsDefined(typeof(KeyAttribute))); - if (property == null) - { - return _ => null; - } - HasKey = true; - var param = Expression.Parameter(typeof(TItem)); - var body = Expression.Property(param, property); - return Expression.Lambda>(Expression.Convert(body, typeof(object)), param).Compile(); - }); - - /// - /// 获取 TItem 是否存在唯一标识 - /// - public static bool HasKey { get; private set; } - /// /// 获得/设置 当前节点值 /// @@ -68,6 +46,6 @@ public class TableTreeNode where TItem : class public TableTreeNode(TItem item) { Value = item; - Key = GetKeyFunc.Value(item); + Key = Utility.GetKeyValue(item); } } -- Gitee