diff --git a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs index 8025456bf44..e7c15a394a0 100644 --- a/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs +++ b/src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs @@ -544,8 +544,8 @@ public async Task EditAsync() { if (SelectedRows.Count == 1) { - // 检查是否选中了不可编辑行(行内无编辑按钮) - if (ShowExtendEditButtonCallback != null && !ShowExtendEditButtonCallback(SelectedRows[0])) + // 检查是否选中了不可编辑行(行内无编辑按钮),同时检查按钮禁用状态(禁用时不可编辑) + if (CanEdit()) { // 提示不可编辑 await ShowToastAsync(EditButtonToastTitle, EditButtonToastReadonlyContent); @@ -992,7 +992,7 @@ protected async Task ConfirmDelete() { await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastContent); } - else if (ShowExtendDeleteButtonCallback != null && SelectedRows.Any(i => !ShowExtendDeleteButtonCallback(i))) + else if (CanDelete()) { await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastCanNotDeleteContent); } @@ -1003,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; + /// /// 删除数据方法 /// diff --git a/test/UnitTest/Components/TableTest.cs b/test/UnitTest/Components/TableTest.cs index fc437e11b90..b0641475681 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,84 @@ 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)); + + 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) + { + 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;