Skip to content

Commit 64b5c86

Browse files
authored
feat(Table): add ClearTableColumnClientStatus instance method (#8005)
* feat: add clearColumnStates method * doc: 增加版本信息 * feat: 增加 ClearTableColumnClientStatus 方法 * test: 补充单元测试 * refactor: 调整获得 DisplayName 逻辑 * refactor: 移除 key 关键字 * doc: 移除注释 * refactor: 增加重置逻辑 * refactor: 撤销示例更改 * chore: bump version 10.6.1-beta20
1 parent 9649910 commit 64b5c86

8 files changed

Lines changed: 116 additions & 6 deletions

File tree

src/BootstrapBlazor.Server/Components/Samples/Table/TablesColumnDrag.razor

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
<h4>@Localizer["TablesColumnDescription"]</h4>
1010

11+
@* <Button Text="Reset" OnClickWithoutRender="Reset"></Button>
12+
*@
1113
<DemoBlock Title="@Localizer["AllowDragOrderTitle"]" Introduction="@Localizer["AllowDragOrderIntro"]" Name="AllowDragColumn">
1214
<section ignore>@((MarkupString)Localizer["AllowDragOrderDesc"].Value)</section>
1315
<Table TItem="Foo" ClientTableName="table-test"
@@ -26,3 +28,12 @@
2628
</Table>
2729
<ConsoleLogger @ref="Logger" />
2830
</DemoBlock>
31+
32+
@code {
33+
// private Table<Foo> Table { get; set; }
34+
35+
// private async Task Reset()
36+
// {
37+
// await Table.ClearTableColumnClientStatus();
38+
// }
39+
}

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.6.1-beta19</Version>
4+
<Version>10.6.1-beta20</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Table/Table.razor

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@
251251
}
252252
@foreach (var col in GetVisibleColumns())
253253
{
254-
<col @key="col" style="@GetColWidthString(col.GetColumnFixedWidth(DefaultFixedColumnWidth))" />
254+
<col style="@GetColWidthString(col.GetColumnFixedWidth(DefaultFixedColumnWidth))" />
255255
}
256256
@if (ShowExtendButtons && !IsExtendButtonsInRowHeader)
257257
{

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,8 @@ private void ResetVisibleColumnsCache()
566566
var col = Columns.Find(c => c.GetFieldName() == item.Name);
567567
if (col != null)
568568
{
569+
item.DisplayName = col.GetDisplayName();
570+
569571
// 恢复宽度
570572
col.Width = item.Width;
571573

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1372,8 +1372,6 @@ private void ResetTableColumns()
13721372
continue;
13731373
}
13741374

1375-
item.DisplayName = col.GetDisplayName();
1376-
13771375
if (!ShowColumnList)
13781376
{
13791377
item.Visible = col.GetVisible(_screenSize);
@@ -1391,7 +1389,6 @@ private void ResetTableColumns()
13911389

13921390
private TableColumnState CreateTableColumnState(ITableColumn col) => new TableColumnState()
13931391
{
1394-
DisplayName = col.GetDisplayName(),
13951392
Name = col.GetFieldName(),
13961393
Width = col.Width,
13971394
Visible = col.GetVisible(_screenSize)
@@ -1860,6 +1857,7 @@ private async Task OnContextMenu(MouseEventArgs e, TItem item)
18601857
/// <summary>
18611858
/// <para lang="zh">获得/设置 加载表格客户端状态回调方法,组件会在首次渲染时调用该方法获取列状态用于初始化表格显示状态</para>
18621859
/// <para lang="en">Gets or sets Load Table Column Client Status Callback. The component will call this method on the first render to get the column status for initializing the table display state.</para>
1860+
/// <para>v<version>10.6.1</version></para>
18631861
/// </summary>
18641862
[Parameter]
18651863
public Func<Task<TableColumnClientStatus>>? OnLoadTableColumnClientStatus { get; set; }
@@ -1880,6 +1878,27 @@ public async Task FitAllColumnWidth()
18801878
await InvokeVoidAsync("fitAllColumnWidth", Id);
18811879
}
18821880

1881+
/// <summary>
1882+
/// <para lang="zh">清除表格列客户端状态实例方法</para>
1883+
/// <para lang="en">clear table column client status instance method</para>
1884+
/// <para>v<version>10.6.1</version></para>
1885+
/// </summary>
1886+
public async Task ClearTableColumnClientStatus()
1887+
{
1888+
if (!string.IsNullOrEmpty(ClientTableName))
1889+
{
1890+
// 如果启用了 ClientTableName 则清除浏览器持久化列状态
1891+
await InvokeVoidAsync("clearColumnStates", ClientTableName);
1892+
}
1893+
1894+
// 清除缓存的列状态
1895+
_tableColumnStateCache.Clear();
1896+
1897+
_resetColumns = true;
1898+
_invoke = true;
1899+
StateHasChanged();
1900+
}
1901+
18831902
/// <summary>
18841903
/// <para lang="zh">重置列方法 由 JavaScript 脚本调用</para>
18851904
/// <para lang="en">Reset Column Method called by JavaScript</para>
@@ -2026,7 +2045,7 @@ private Task OnTouchEnd(TouchEventArgs e)
20262045
return Task.CompletedTask;
20272046
}
20282047

2029-
private object? GetKeyByITem(TItem item) => SortableList != null ? item : null; //OnGetRowKey?.Invoke(item);
2048+
private object? GetKeyByITem(TItem item) => SortableList != null ? item : null;
20302049

20312050
private RenderFragment RenderRowCell(TItem item) => builder =>
20322051
{

src/BootstrapBlazor/Components/Table/Table.razor.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -869,6 +869,11 @@ const removeColumnWidthState = tableName => {
869869
localStorage.removeItem(columnWidthKey);
870870
}
871871

872+
export function clearColumnStates(tableName) {
873+
const columnStateKey = `bb-table-${tableName}`;
874+
localStorage.removeItem(columnStateKey);
875+
}
876+
872877
const getColumnStateFromLocalstorage = tableName => {
873878
const columnStateKey = `bb-table-${tableName}`;
874879
return getLocalStorageValue(columnStateKey);
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
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(argo@live.ca) Website: https://www.blazor.zone
5+
6+
namespace BootstrapBlazor.Components;
7+
8+
/// <summary>
9+
/// <para lang="zh">TableColumnClientStatus 扩展方法</para>
10+
/// <para lang="en">TableColumnClientStatus extensions</para>
11+
/// </summary>
12+
public static class TableColumnClientStatusExtensions
13+
{
14+
extension(TableColumnClientStatus status)
15+
{
16+
/// <summary>
17+
/// <para lang="zh">清除方法</para>
18+
/// <para lang="en">Clear method</para>
19+
/// </summary>
20+
public void Clear()
21+
{
22+
status.Columns.Clear();
23+
status.TableWidth = 0;
24+
}
25+
}
26+
}

test/UnitTest/Components/TableTest.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8897,6 +8897,53 @@ public async Task OnTableColumnClientStatusChanged_ResizeColumn_Ok()
88978897
Assert.NotNull(clientState);
88988898
}
88998899

8900+
[Fact]
8901+
public async Task ClearTableColumnClientStatus_Ok()
8902+
{
8903+
var state = new TableColumnClientStatus();
8904+
state.TableWidth = 220;
8905+
state.Columns.Add(new TableColumnState() { Name = nameof(Foo.Name), Visible = true, Width = 100 });
8906+
state.Columns.Add(new TableColumnState() { Name = nameof(Foo.Address), Visible = true, Width = 120 });
8907+
8908+
Context.JSInterop.Setup<TableColumnClientStatus>("getColumnStates", "test_clear").SetResult(state);
8909+
var invoker = Context.JSInterop.SetupVoid("clearColumnStates", "test_clear");
8910+
invoker.SetVoidResult();
8911+
8912+
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
8913+
var cut = Context.Render<BootstrapBlazorRoot>(pb =>
8914+
{
8915+
pb.AddChildContent<Table<Foo>>(pb =>
8916+
{
8917+
pb.Add(a => a.ClientTableName, "test_clear");
8918+
pb.Add(a => a.RenderMode, TableRenderMode.Table);
8919+
pb.Add(a => a.AllowResizing, true);
8920+
pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer));
8921+
pb.Add(a => a.TableColumns, foo => builder =>
8922+
{
8923+
builder.OpenComponent<TableColumn<Foo, string>>(0);
8924+
builder.AddAttribute(1, "Field", "Name");
8925+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
8926+
builder.AddAttribute(3, "Width", 80);
8927+
builder.CloseComponent();
8928+
8929+
builder.OpenComponent<TableColumn<Foo, string>>(0);
8930+
builder.AddAttribute(3, "Field", "Address");
8931+
builder.AddAttribute(4, "FieldExpression", Utility.GenerateValueExpression(foo, "Address", typeof(string)));
8932+
builder.CloseComponent();
8933+
});
8934+
});
8935+
});
8936+
8937+
// 由于启用了客户端持久化 Name 列宽使用 100 而非 80
8938+
var table = cut.FindComponent<Table<Foo>>();
8939+
Assert.Equal(100, table.Instance.Columns[0].Width);
8940+
8941+
// 清除客户端状态
8942+
await cut.InvokeAsync(() => table.Instance.ClearTableColumnClientStatus());
8943+
invoker.VerifyInvoke("clearColumnStates");
8944+
Assert.Equal(80, table.Instance.Columns[0].Width);
8945+
}
8946+
89008947
[Theory]
89018948
[InlineData(true)]
89028949
[InlineData(false)]

0 commit comments

Comments
 (0)