Skip to content

Commit bfbafc6

Browse files
authored
feat(Table): support Cancel use DataTableDynamicObject model (#7917)
* feat: DataTable 动态类型编辑时使用副本 * refactor: 更新值时增加对象值更新逻辑 * refactor: 克隆副本方法支持动态数据类型 * refactor: 重构代码 * test: 增加单元测试
1 parent d22ede2 commit bfbafc6

4 files changed

Lines changed: 48 additions & 5 deletions

File tree

src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,9 @@ public async Task EditAsync()
651651
else
652652
{
653653
await ToggleLoading(true);
654-
EditModel = (IsTracking || DynamicContext != null) ? SelectedRows[0] : Utility.Clone(SelectedRows[0]);
654+
655+
// 复制对象给编辑模型
656+
EditModel = GetEditModel(SelectedRows[0]);
655657
if (OnEditAsync != null)
656658
{
657659
await OnEditAsync(EditModel);
@@ -696,6 +698,16 @@ public async Task EditAsync()
696698
}
697699
}
698700

701+
private TItem GetEditModel(TItem item)
702+
{
703+
if (IsTracking)
704+
{
705+
return item;
706+
}
707+
708+
return DynamicContext is DataTableDynamicContext ? Utility.Clone(item) : item;
709+
}
710+
699711
private async Task ShowToastAsync(string title, string content, ToastCategory category = ToastCategory.Information)
700712
{
701713
var option = GetToastOption(title);

src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,9 @@ private Task OnCellValueChanged(IDynamicObject item, ITableColumn column, object
290290
// 更新内部 DataRow
291291
if (_dataCache.TryGetValue(item.DynamicObjectPrimaryKey, out var cacheItem))
292292
{
293+
// 更新动态类型数据
294+
Utility.SetPropertyValue<object, object?>(cacheItem, column.GetFieldName(), val);
295+
293296
// 更新原始 DataTable
294297
if (cacheItem.Row != null)
295298
{

src/BootstrapBlazor/Extensions/ObjectExtensions.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -220,10 +220,7 @@ internal static void Clone<TModel>(this TModel source, TModel item)
220220
{
221221
if (item != null)
222222
{
223-
var type = typeof(TModel);
224-
225-
// <para lang="zh">20200608 tian_teng@outlook.com 支持字段和只读属性</para>
226-
// <para lang="en">20200608 tian_teng@outlook.com Support fields and read-only properties</para>
223+
var type = item.GetType();
227224
foreach (var f in type.GetFields())
228225
{
229226
var v = f.GetValue(item);

test/UnitTest/Components/TableTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6686,6 +6686,37 @@ public async Task DynamicContext_Edit()
66866686
Assert.True(saved);
66876687
}
66886688

6689+
[Fact]
6690+
public async Task DynamicContext_InCell_Ok()
6691+
{
6692+
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
6693+
var items = Foo.GenerateFoo(localizer, 2);
6694+
var cut = Context.Render<BootstrapBlazorRoot>(pb =>
6695+
{
6696+
pb.AddChildContent<Table<DynamicObject>>(pb =>
6697+
{
6698+
pb.Add(a => a.RenderMode, TableRenderMode.Table);
6699+
pb.Add(a => a.IsMultipleSelect, true);
6700+
pb.Add(a => a.EditMode, EditMode.InCell);
6701+
pb.Add(a => a.ShowToolbar, true);
6702+
pb.Add(a => a.ShowExtendButtons, true);
6703+
pb.Add(a => a.DynamicContext, CreateDynamicContext(localizer));
6704+
});
6705+
});
6706+
6707+
// 选中行
6708+
var input = cut.FindComponents<Checkbox<Guid>>()[1];
6709+
await cut.InvokeAsync(input.Instance.OnToggleClick);
6710+
6711+
// 点击编辑按钮
6712+
var editButton = cut.FindComponents<TableToolbarButton<DynamicObject>>()[1];
6713+
await cut.InvokeAsync(() => editButton.Instance.OnClick.InvokeAsync());
6714+
6715+
// 点击取消按钮
6716+
var cancelButton = cut.FindComponents<Button>().First(i => i.Instance.Text == "取消");
6717+
await cut.InvokeAsync(() => cancelButton.Instance.OnClick.InvokeAsync());
6718+
}
6719+
66896720
[Fact]
66906721
public void DynamicContext_Pagination()
66916722
{

0 commit comments

Comments
 (0)