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
14 changes: 13 additions & 1 deletion src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,9 @@ public async Task EditAsync()
else
{
await ToggleLoading(true);
EditModel = (IsTracking || DynamicContext != null) ? SelectedRows[0] : Utility.Clone(SelectedRows[0]);

// 复制对象给编辑模型
EditModel = GetEditModel(SelectedRows[0]);
if (OnEditAsync != null)
{
await OnEditAsync(EditModel);
Expand Down Expand Up @@ -696,6 +698,16 @@ public async Task EditAsync()
}
}

private TItem GetEditModel(TItem item)
{
if (IsTracking)
{
return item;
}

return DynamicContext is DataTableDynamicContext ? Utility.Clone(item) : item;
}

private async Task ShowToastAsync(string title, string content, ToastCategory category = ToastCategory.Information)
{
var option = GetToastOption(title);
Expand Down
3 changes: 3 additions & 0 deletions src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ private Task OnCellValueChanged(IDynamicObject item, ITableColumn column, object
// 更新内部 DataRow
if (_dataCache.TryGetValue(item.DynamicObjectPrimaryKey, out var cacheItem))
{
// 更新动态类型数据
Utility.SetPropertyValue<object, object?>(cacheItem, column.GetFieldName(), val);

// 更新原始 DataTable
if (cacheItem.Row != null)
Comment thread
ArgoZhang marked this conversation as resolved.
{
Expand Down
5 changes: 1 addition & 4 deletions src/BootstrapBlazor/Extensions/ObjectExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,10 +220,7 @@ internal static void Clone<TModel>(this TModel source, TModel item)
{
if (item != null)
{
var type = typeof(TModel);

// <para lang="zh">20200608 tian_teng@outlook.com 支持字段和只读属性</para>
// <para lang="en">20200608 tian_teng@outlook.com Support fields and read-only properties</para>
var type = item.GetType();
foreach (var f in type.GetFields())
{
var v = f.GetValue(item);
Expand Down
31 changes: 31 additions & 0 deletions test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6686,6 +6686,37 @@ public async Task DynamicContext_Edit()
Assert.True(saved);
}

[Fact]
public async Task DynamicContext_InCell_Ok()
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var items = Foo.GenerateFoo(localizer, 2);
var cut = Context.Render<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<DynamicObject>>(pb =>
{
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.IsMultipleSelect, true);
pb.Add(a => a.EditMode, EditMode.InCell);
pb.Add(a => a.ShowToolbar, true);
pb.Add(a => a.ShowExtendButtons, true);
pb.Add(a => a.DynamicContext, CreateDynamicContext(localizer));
});
});

// 选中行
var input = cut.FindComponents<Checkbox<Guid>>()[1];
await cut.InvokeAsync(input.Instance.OnToggleClick);

// 点击编辑按钮
var editButton = cut.FindComponents<TableToolbarButton<DynamicObject>>()[1];
await cut.InvokeAsync(() => editButton.Instance.OnClick.InvokeAsync());

// 点击取消按钮
var cancelButton = cut.FindComponents<Button>().First(i => i.Instance.Text == "取消");
await cut.InvokeAsync(() => cancelButton.Instance.OnClick.InvokeAsync());
}

[Fact]
public void DynamicContext_Pagination()
{
Expand Down
Loading