diff --git a/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs b/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs index 187674547f9..ec98af44d11 100644 --- a/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs +++ b/src/BootstrapBlazor/Dynamic/DataTableDynamicContext.cs @@ -28,6 +28,11 @@ public class DataTableDynamicContext : DynamicObjectContext private Action? AddAttributesCallback { get; set; } + /// + /// 获得/设置 是否启用内部缓存 默认 true 启用 + /// + public bool UseCache { get; set; } = true; + /// /// 负责将 DataRow 与 Items 关联起来方便查找提高效率 /// @@ -104,7 +109,14 @@ private static bool GetShownColumns(ITableColumn col, IEnumerable? invis /// public override IEnumerable GetItems() { - Items ??= BuildItems(); + if (UseCache) + { + Items ??= BuildItems(); + } + else + { + Items = BuildItems(); + } return Items; } diff --git a/test/UnitTest/Components/DataTableDynamicContextTest.cs b/test/UnitTest/Components/DataTableDynamicContextTest.cs new file mode 100644 index 00000000000..78b9d7a6f93 --- /dev/null +++ b/test/UnitTest/Components/DataTableDynamicContextTest.cs @@ -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(argo@live.ca) 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()); + } +}