From b7c40407fc321bc54babc792f8c8f0c9bca3f120 Mon Sep 17 00:00:00 2001 From: Argo Date: Wed, 22 Sep 2021 06:06:25 +0000 Subject: [PATCH] =?UTF-8?q?!1884=20feat(#I4BB1T):=20add=20IsEditable=20ext?= =?UTF-8?q?ension=20method=20for=20IEditorItem=20*=20feat:=20=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=89=A9=E5=B1=95=E6=96=B9=E6=B3=95=20IsEditable=20?= =?UTF-8?q?=E7=BB=9F=E4=B8=80=E7=BB=84=E4=BB=B6=E6=B8=B2=E6=9F=93=E4=B8=8D?= =?UTF-8?q?=E5=8F=AF=E7=BC=96=E8=BE=91=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/EditorForm/EditorForm.razor.cs | 2 +- .../Components/EditorForm/IEditorItem.cs | 2 +- .../Components/Table/Table.razor.cs | 14 ++++++++------ src/BootstrapBlazor/Extensions/ObjectExtensions.cs | 7 +++++++ src/BootstrapBlazor/Utils/Utility.cs | 6 +++++- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs b/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs index fc5292e84..508849a9b 100644 --- a/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs +++ b/src/BootstrapBlazor/Components/EditorForm/EditorForm.razor.cs @@ -229,7 +229,7 @@ namespace BootstrapBlazor.Components private RenderFragment AutoGenerateTemplate(IEditorItem item) => builder => { - if (IsDisplay || item.Readonly || !item.Editable) + if (IsDisplay || !item.IsEditable()) { builder.CreateDisplayByFieldType(this, item, Model, ShowLabel); } diff --git a/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs b/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs index 5a9a3199f..a73538f8a 100644 --- a/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs +++ b/src/BootstrapBlazor/Components/EditorForm/IEditorItem.cs @@ -24,7 +24,7 @@ namespace BootstrapBlazor.Components bool Editable { get; set; } /// - /// 获得/设置 当前列编辑时是否只读 默认为 false + /// 获得/设置 当前列编辑时是否只读 默认为 false 自动生成 UI 为不可编辑 div /// bool Readonly { get; set; } diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.cs b/src/BootstrapBlazor/Components/Table/Table.razor.cs index 135c1b771..23b8e9260 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.cs @@ -846,16 +846,18 @@ namespace BootstrapBlazor.Components private static ConcurrentDictionary<(Type Type, string PropertyName), Func> GetPropertyCache { get; } = new(); #endregion - private RenderFragment RenderCell(ITableColumn col, TItem item) => col.EditTemplate == null - ? (col.Readonly || !col.Editable - ? builder => builder.CreateDisplayByFieldType(this, col, item, false) - : builder => builder.CreateComponentByFieldType(this, col, item, false)) - : col.EditTemplate.Invoke(item); + private RenderFragment RenderCell(ITableColumn col, TItem item) => col.IsEditable() + ? (col.EditTemplate == null + ? builder => builder.CreateComponentByFieldType(this, col, item, false) + : col.EditTemplate(item)) + : builder => builder.CreateDisplayByFieldType(this, col, item, false); private RenderFragment RenderExcelCell(ITableColumn col, TItem item) { col.PlaceHolder ??= ""; - if (col.EditTemplate == null) + + // 可编辑列未设置模板 + if (col.IsEditable() && col.EditTemplate == null) { if (DynamicContext != null) { diff --git a/src/BootstrapBlazor/Extensions/ObjectExtensions.cs b/src/BootstrapBlazor/Extensions/ObjectExtensions.cs index e39f6864e..437a876ca 100644 --- a/src/BootstrapBlazor/Extensions/ObjectExtensions.cs +++ b/src/BootstrapBlazor/Extensions/ObjectExtensions.cs @@ -179,5 +179,12 @@ namespace BootstrapBlazor.Components >= 1024 * 1024 * 1024 => $"{Math.Round(fileSize / 1024 / 1024 / 1024D, 0, MidpointRounding.AwayFromZero)} GB", _ => $"{fileSize} B" }; + + /// + /// 判断当前 IEditorItem 实例是否可以编辑 + /// + /// + /// + public static bool IsEditable(this IEditorItem item) => !item.Readonly && item.Editable; } } diff --git a/src/BootstrapBlazor/Utils/Utility.cs b/src/BootstrapBlazor/Utils/Utility.cs index a270014eb..454cf88d5 100644 --- a/src/BootstrapBlazor/Utils/Utility.cs +++ b/src/BootstrapBlazor/Utils/Utility.cs @@ -325,7 +325,11 @@ namespace BootstrapBlazor.Components builder.AddAttribute(2, nameof(ValidateBase.Value), fieldValue); builder.AddAttribute(3, nameof(ValidateBase.ValueChanged), fieldValueChanged); builder.AddAttribute(4, nameof(ValidateBase.ValueExpression), valueExpression); - builder.AddAttribute(5, nameof(ValidateBase.IsDisabled), item.Readonly); + + if (!item.IsEditable()) + { + builder.AddAttribute(5, nameof(ValidateBase.IsDisabled), true); + } } if (IsCheckboxList(fieldType) && item.Items != null)