Skip to content

Commit ce031e4

Browse files
authored
feat(DataTableDynamicContext): add UseCache parameter (#5389)
* feat: 增加 UseCache 参数 * refactor: 显示控制逻辑 * refactor: 重构代码 * test: 增加单元测试
1 parent 2b1ab85 commit ce031e4

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public class DataTableDynamicContext : DynamicObjectContext
2828

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

31+
/// <summary>
32+
/// 获得/设置 是否启用内部缓存 默认 true 启用
33+
/// </summary>
34+
public bool UseCache { get; set; } = true;
35+
3136
/// <summary>
3237
/// 负责将 DataRow 与 Items 关联起来方便查找提高效率
3338
/// </summary>
@@ -104,7 +109,14 @@ private static bool GetShownColumns(ITableColumn col, IEnumerable<string>? invis
104109
/// <returns></returns>
105110
public override IEnumerable<IDynamicObject> GetItems()
106111
{
107-
Items ??= BuildItems();
112+
if (UseCache)
113+
{
114+
Items ??= BuildItems();
115+
}
116+
else
117+
{
118+
Items = BuildItems();
119+
}
108120
return Items;
109121
}
110122

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the Apache 2.0 License
3+
// See the LICENSE file in the project root for more information.
4+
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone
5+
6+
using System.Data;
7+
8+
namespace UnitTest.Components;
9+
10+
public class DataTableDynamicContextTest : BootstrapBlazorTestBase
11+
{
12+
[Fact]
13+
public void UseCache_Ok()
14+
{
15+
var table = new DataTable();
16+
table.Columns.Add("Id", typeof(int));
17+
table.Rows.Add(1);
18+
table.AcceptChanges();
19+
20+
var context = new DataTableDynamicContext(table);
21+
Assert.True(context.UseCache);
22+
23+
var data = context.GetItems();
24+
Assert.Single(data);
25+
Assert.Equal(1, data.First().GetValue("Id"));
26+
27+
// 增加数据
28+
table.Rows.Add(2);
29+
var data2 = context.GetItems();
30+
Assert.Equal(data, data2);
31+
32+
// 关闭缓存
33+
context.UseCache = false;
34+
table.Rows.Add(3);
35+
data2 = context.GetItems();
36+
Assert.Equal(3, data2.Count());
37+
}
38+
}

0 commit comments

Comments
 (0)