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/Dynamic/DataTableDynamicContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ public class DataTableDynamicContext : DynamicObjectContext

private Action<DataTableDynamicContext, ITableColumn>? AddAttributesCallback { get; set; }

/// <summary>
/// 获得/设置 是否启用内部缓存 默认 true 启用
/// </summary>
public bool UseCache { get; set; } = true;

/// <summary>
/// 负责将 DataRow 与 Items 关联起来方便查找提高效率
/// </summary>
Expand Down Expand Up @@ -104,7 +109,14 @@ private static bool GetShownColumns(ITableColumn col, IEnumerable<string>? invis
/// <returns></returns>
public override IEnumerable<IDynamicObject> GetItems()
{
Items ??= BuildItems();
if (UseCache)
{
Items ??= BuildItems();
}
else
{
Items = BuildItems();
}
return Items;
}

Expand Down
38 changes: 38 additions & 0 deletions test/UnitTest/Components/DataTableDynamicContextTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the Apache 2.0 License
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using System.Data;

namespace UnitTest.Components;

public class DataTableDynamicContextTest : BootstrapBlazorTestBase
{
[Fact]
public void UseCache_Ok()
{
var table = new DataTable();
table.Columns.Add("Id", typeof(int));
table.Rows.Add(1);
table.AcceptChanges();

var context = new DataTableDynamicContext(table);
Assert.True(context.UseCache);

var data = context.GetItems();
Assert.Single(data);
Assert.Equal(1, data.First().GetValue("Id"));

// 增加数据
table.Rows.Add(2);
var data2 = context.GetItems();
Assert.Equal(data, data2);

// 关闭缓存
context.UseCache = false;
table.Rows.Add(3);
data2 = context.GetItems();
Assert.Equal(3, data2.Count());
}
}