Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -544,8 +544,8 @@ public async Task EditAsync()
{
if (SelectedRows.Count == 1)
{
// 检查是否选中了不可编辑行(行内无编辑按钮)
if (ShowExtendEditButtonCallback != null && !ShowExtendEditButtonCallback(SelectedRows[0]))
// 检查是否选中了不可编辑行(行内无编辑按钮),同时检查按钮禁用状态(禁用时不可编辑)
if (CanEdit())
{
// 提示不可编辑
await ShowToastAsync(EditButtonToastTitle, EditButtonToastReadonlyContent);
Expand Down Expand Up @@ -992,7 +992,7 @@ protected async Task<bool> ConfirmDelete()
{
await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastContent);
}
else if (ShowExtendDeleteButtonCallback != null && SelectedRows.Any(i => !ShowExtendDeleteButtonCallback(i)))
else if (CanDelete())
{
await ShowDeleteToastAsync(DeleteButtonToastTitle, DeleteButtonToastCanNotDeleteContent);
}
Expand All @@ -1003,6 +1003,16 @@ protected async Task<bool> 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;

/// <summary>
/// 删除数据方法
/// </summary>
Expand Down
79 changes: 79 additions & 0 deletions test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace UnitTest.Components;

Expand Down Expand Up @@ -8635,6 +8636,84 @@ await Assert.ThrowsAsync<InvalidOperationException>(() =>
});
}

[Fact]
public void Modify_Ok()
{
var cut = Context.RenderComponent<Table<Foo>>(pb =>
{
pb.Add(a => a.TableColumns, foo => builder =>
{
builder.OpenComponent<TableColumn<Foo, string>>(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<Foo> @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<Foo> @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;
Expand Down