From 7759b98d6e9f54ae99ad145c593640f42c7c0365 Mon Sep 17 00:00:00 2001 From: AiZhen Date: Thu, 8 May 2025 09:13:53 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E5=A2=9E=E5=BC=BA=E7=BC=96=E8=BE=91?= =?UTF-8?q?=E5=92=8C=E5=88=A0=E9=99=A4=E6=93=8D=E4=BD=9C=E7=9A=84=E6=8C=89?= =?UTF-8?q?=E9=92=AE=E7=8A=B6=E6=80=81=E6=A3=80=E6=9F=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加了对编辑按钮禁用状态的检查,确保在编辑时考虑行的可编辑性和按钮的可用性。同时,更新了删除操作的逻辑,增加了对删除按钮禁用状态的检查,以提高代码的健壮性。 --- .../Components/Table/Table.razor.Toolbar.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs index 8025456bf44..8abe600ef20 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs @@ -544,8 +544,11 @@ public async Task EditAsync() { if (SelectedRows.Count == 1) { - // 检查是否选中了不可编辑行(行内无编辑按钮) - if (ShowExtendEditButtonCallback != null && !ShowExtendEditButtonCallback(SelectedRows[0])) + // 检查是否选中了不可编辑行(行内无编辑按钮),同时检查按钮禁用状态(禁用时不可编辑) + if ((ShowExtendEditButtonCallback != null && !ShowExtendEditButtonCallback(SelectedRows[0])) + || !ShowExtendEditButton + || (DisableExtendEditButtonCallback != null && DisableExtendEditButtonCallback(SelectedRows[0])) + || DisableExtendEditButton) { // 提示不可编辑 await ShowToastAsync(EditButtonToastTitle, EditButtonToastReadonlyContent); @@ -992,7 +995,10 @@ protected async Task ConfirmDelete() { await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastContent); } - else if (ShowExtendDeleteButtonCallback != null && SelectedRows.Any(i => !ShowExtendDeleteButtonCallback(i))) + else if ((ShowExtendDeleteButtonCallback != null && SelectedRows.Any(i => !ShowExtendDeleteButtonCallback(i))) + || !ShowExtendDeleteButton + || (DisableExtendDeleteButtonCallback != null && SelectedRows.Any(x => DisableExtendDeleteButtonCallback(x))) + || DisableExtendDeleteButton) { await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastCanNotDeleteContent); } From 956ddb1f4cc2ecd9b7e446143420989662cc3131 Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 8 May 2025 12:41:43 +0800 Subject: [PATCH 2/4] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/Table/Table.razor.Toolbar.cs | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs index 8abe600ef20..e7c15a394a0 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs @@ -545,10 +545,7 @@ public async Task EditAsync() if (SelectedRows.Count == 1) { // 检查是否选中了不可编辑行(行内无编辑按钮),同时检查按钮禁用状态(禁用时不可编辑) - if ((ShowExtendEditButtonCallback != null && !ShowExtendEditButtonCallback(SelectedRows[0])) - || !ShowExtendEditButton - || (DisableExtendEditButtonCallback != null && DisableExtendEditButtonCallback(SelectedRows[0])) - || DisableExtendEditButton) + if (CanEdit()) { // 提示不可编辑 await ShowToastAsync(EditButtonToastTitle, EditButtonToastReadonlyContent); @@ -995,10 +992,7 @@ protected async Task ConfirmDelete() { await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastContent); } - else if ((ShowExtendDeleteButtonCallback != null && SelectedRows.Any(i => !ShowExtendDeleteButtonCallback(i))) - || !ShowExtendDeleteButton - || (DisableExtendDeleteButtonCallback != null && SelectedRows.Any(x => DisableExtendDeleteButtonCallback(x))) - || DisableExtendDeleteButton) + else if (CanDelete()) { await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastCanNotDeleteContent); } @@ -1009,6 +1003,16 @@ protected async Task ConfirmDelete() return ret; } + private bool CanEdit() => (ShowExtendEditButtonCallback != null && !ShowExtendEditButtonCallback(SelectedRows[0])) + || !ShowExtendEditButton + || (DisableExtendEditButtonCallback != null && DisableExtendEditButtonCallback(SelectedRows[0])) + || DisableExtendEditButton; + + private bool CanDelete() => (ShowExtendDeleteButtonCallback != null && SelectedRows.Any(i => !ShowExtendDeleteButtonCallback(i))) + || !ShowExtendDeleteButton + || (DisableExtendDeleteButtonCallback != null && SelectedRows.Any(x => DisableExtendDeleteButtonCallback(x))) + || DisableExtendDeleteButton; + /// /// 删除数据方法 /// From ab87080aac025c0538619e17dbcb0ad06be968be Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 8 May 2025 12:41:52 +0800 Subject: [PATCH 3/4] =?UTF-8?q?test:=20=E5=A2=9E=E5=8A=A0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TableTest.cs | 60 +++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/test/UnitTest/Components/TableTest.cs b/test/UnitTest/Components/TableTest.cs index fc437e11b90..f46f2b7a9c7 100644 --- a/test/UnitTest/Components/TableTest.cs +++ b/test/UnitTest/Components/TableTest.cs @@ -11,6 +11,7 @@ using System.ComponentModel.DataAnnotations; using System.Data; using System.Reflection; +using System.Runtime.CompilerServices; namespace UnitTest.Components; @@ -8635,6 +8636,65 @@ await Assert.ThrowsAsync(() => }); } + [Fact] + public void Modify_Ok() + { + var cut = Context.RenderComponent>(pb => + { + pb.Add(a => a.TableColumns, foo => builder => + { + builder.OpenComponent>(0); + builder.AddAttribute(1, "Field", "Name"); + builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string))); + builder.CloseComponent(); + }); + pb.Add(a => a.ShowExtendEditButton, false); + pb.Add(a => a.ShowExtendDeleteButton, false); + }); + Assert.True(CanEdit(cut.Instance)); + Assert.True(CanDelete(cut.Instance)); + + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.ShowExtendEditButton, true); + pb.Add(a => a.ShowExtendDeleteButton, true); + pb.Add(a => a.DisableExtendEditButton, true); + pb.Add(a => a.DisableExtendDeleteButton, true); + }); + Assert.True(CanEdit(cut.Instance)); + Assert.True(CanDelete(cut.Instance)); + } + + static bool CanEdit(Table @this) + { + var ret = false; + var methodInfo = @this.GetType().GetMethod("CanEdit", BindingFlags.Instance | BindingFlags.NonPublic); + if (methodInfo != null) + { + var result = methodInfo.Invoke(@this, null); + if (result is bool d) + { + ret = d; + } + } + return ret; + } + + static bool CanDelete(Table @this) + { + var ret = false; + var methodInfo = @this.GetType().GetMethod("CanDelete", BindingFlags.Instance | BindingFlags.NonPublic); + if (methodInfo != null) + { + var result = methodInfo.Invoke(@this, null); + if (result is bool d) + { + ret = d; + } + } + return ret; + } + class MockFoo(string name) { public string Name { get; set; } = name; From 96e8e684ebc7e0cd81d77d770eead872619725fa Mon Sep 17 00:00:00 2001 From: Argo Zhang Date: Thu, 8 May 2025 13:26:03 +0800 Subject: [PATCH 4/4] =?UTF-8?q?test:=20=E6=9B=B4=E6=96=B0=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/UnitTest/Components/TableTest.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/UnitTest/Components/TableTest.cs b/test/UnitTest/Components/TableTest.cs index f46f2b7a9c7..b0641475681 100644 --- a/test/UnitTest/Components/TableTest.cs +++ b/test/UnitTest/Components/TableTest.cs @@ -8663,6 +8663,25 @@ public void Modify_Ok() }); Assert.True(CanEdit(cut.Instance)); Assert.True(CanDelete(cut.Instance)); + + cut.SetParametersAndRender(pb => + { + pb.Add(a => a.ShowExtendEditButton, true); + pb.Add(a => a.ShowExtendDeleteButton, true); + pb.Add(a => a.DisableExtendEditButton, false); + pb.Add(a => a.DisableExtendDeleteButton, false); + pb.Add(a => a.SelectedRows, [new Foo()]); + pb.Add(a => a.DisableExtendEditButtonCallback, rows => + { + return true; + }); + pb.Add(a => a.DisableExtendDeleteButtonCallback, rows => + { + return true; + }); + }); + Assert.True(CanEdit(cut.Instance)); + Assert.True(CanDelete(cut.Instance)); } static bool CanEdit(Table @this)