Skip to content

Commit 6baa850

Browse files
authored
doc(Table): update ClearTableColumnClientStatus sample code (#8007)
* refactor: 移除更改原始列宽逻辑 * refactor: 移除 AutoFitColumnWidthCallback 回调与列调整回调合并 * refactor: 循环 col 改用 _tableColumnStates 变量 * refactor: 移除客户端脚本调用逻辑 * doc: 更新示例 * refactor: 移除不使用的扩展方法 * doc: 更新示例 * test: 更新单元测试 * doc: 增加重置按钮本地化资源 * test: 补充单元测试 * chore: bump version 10.6.1-beta21
1 parent 64b5c86 commit 6baa850

11 files changed

Lines changed: 40 additions & 87 deletions

File tree

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

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,16 @@
88

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

11-
@* <Button Text="Reset" OnClickWithoutRender="Reset"></Button>
12-
*@
11+
1312
<DemoBlock Title="@Localizer["AllowDragOrderTitle"]" Introduction="@Localizer["AllowDragOrderIntro"]" Name="AllowDragColumn">
14-
<section ignore>@((MarkupString)Localizer["AllowDragOrderDesc"].Value)</section>
15-
<Table TItem="Foo" ClientTableName="table-test"
13+
<section ignore>
14+
<p>@((MarkupString)Localizer["AllowDragOrderDesc"].Value)</p>
15+
<p>@((MarkupString)Localizer["ClearTableColumnClientStatusDesc"].Value)</p>
16+
</section>
17+
<Table TItem="Foo" ClientTableName="table-column-drag"
1618
IsPagination="true" PageItemsSource="@PageItemsSource"
1719
AllowDragColumn="true" AllowResizing="true"
18-
ShowToolbar="true" ShowColumnList="true"
20+
ShowToolbar="true" ShowColumnList="true" @ref="_table"
1921
IsStriped="true" IsBordered="true" OnTableColumnClientStatusChanged="OnTableColumnClientStatusChanged"
2022
IsMultipleSelect="true" ShowExtendButtons="false"
2123
OnQueryAsync="@OnQueryAsync">
@@ -25,15 +27,9 @@
2527
<TableColumn @bind-Field="@context.Address" />
2628
<TableColumn @bind-Field="@context.Count" />
2729
</TableColumns>
30+
<TableToolbarTemplate>
31+
<TableToolbarButton Text="@Localizer["ResetButtonText"]" OnClickWithoutRender="Reset"></TableToolbarButton>
32+
</TableToolbarTemplate>
2833
</Table>
2934
<ConsoleLogger @ref="Logger" />
3035
</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.Server/Components/Samples/Table/TablesColumnDrag.razor.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public partial class TablesColumnDrag
1717
/// </summary>
1818
[NotNull]
1919
private List<Foo>? Items { get; set; }
20+
2021
private static IEnumerable<int> PageItemsSource => new int[]
2122
{
2223
5,
@@ -27,6 +28,8 @@ public partial class TablesColumnDrag
2728
[NotNull]
2829
private ConsoleLogger? Logger { get; set; }
2930

31+
private Table<Foo> _table = default!;
32+
3033
/// <summary>
3134
/// <inheritdoc/>
3235
/// </summary>
@@ -69,4 +72,9 @@ private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
6972
items = items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();
7073
return Task.FromResult(new QueryData<Foo>() { Items = items, TotalCount = total, IsSorted = isSorted, IsFiltered = isFiltered, IsSearch = true });
7174
}
75+
76+
private Task Reset()
77+
{
78+
return _table.ClearTableColumnClientStatus();
79+
}
7280
}

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4607,6 +4607,8 @@
46074607
},
46084608
"BootstrapBlazor.Server.Components.Samples.Table.TablesColumnDrag": {
46094609
"AllowDragOrderDesc": "<p>Pressing the mouse over a column heading and dragging it to another column heading position can adjust the column to be in front of the target column, but the built-in columns in the <code>Table</code> component, such as detail row, row number, selection, and operation columns, cannot be adjusted</p><p>This example enables local storage by setting the <code>ClientTableName</code> parameter. After dragging and adjusting the order, the page can be refreshed, and the column order remains in the previous state</p><p>After version <code>10.6.1</code>, use <code>OnTableColumnClientStatusChanged</code> to uniformly handle column dragging, column resizing, and auto-fit column width callbacks, replacing the original <code>OnDragColumnEndAsync</code>, <code>OnResizeColumnAsync</code>, and <code>OnAutoFitContentAsync</code> callbacks.</p><p><b>Notes:</b></p><p>Table state persistence has two modes. If the <code>ClientTableName</code> property is set, browser local storage is used to persist information. If it is not set, server-side storage persistence is implemented through callbacks. The persistence structure is <code>TableColumnClientStatus</code>, which consists of the column state collection and the table width parameter.</p><p>The table width parameter defaults to 0. When all columns have width values, the table width parameter is the actual width rendered on the client. A value of 0 means the table width is adaptive.</p><p>When using browser local storage for persistence, no additional settings are required. The table automatically loads the client state.</p><p>When using server-side storage for persistence, handle table state loading and saving logic in callbacks. In actual use, the state can be stored in a database or other storage as needed. Use the <code>OnTableColumnClientStatusChanged</code> callback to handle server-side state changes, and use the <code>OnLoadTableColumnClientStatus</code> callback to restore the table persistence state.</p>",
4610+
"ClearTableColumnClientStatusDesc": "By calling the <code>ClearTableColumnClientStatus</code> method of the table component instance, the current persistent settings are cleared, restoring the table to its default settings.",
4611+
"ResetButtonText": "Reset",
46104612
"AllowDragOrderIntro": "By specifying <code>AllowDragColumn</code>, set the table columns to allow dragging column headings to adjust the table column order",
46114613
"AllowDragOrderTitle": "Allow dragging column headings to adjust table column order",
46124614
"TablesColumnDescription": "Used to display multiple pieces of data with similar structures, data can be sorted, filtered, compared or other custom operations.",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4607,6 +4607,8 @@
46074607
},
46084608
"BootstrapBlazor.Server.Components.Samples.Table.TablesColumnDrag": {
46094609
"AllowDragOrderDesc": "<p>在列标题上按下鼠标拖动到其他列标题位置可将该列调整至目标列之前,但 <code>Table</code> 组件内置的列如明细行列、行号列、选择列、操作列等不可被调整</p><p>本示例通过设置 <code>ClientTableName</code> 参数开启了本地化存储,拖动调整顺序后,可刷新页面,列顺序是保持上次状态的</p><p><code>10.6.1</code> 版本后使用 <code>OnTableColumnClientStatusChanged</code> 统一处理列拖拽、列宽调整与自适应列宽回调,移除原 <code>OnDragColumnEndAsync</code> <code>OnResizeColumnAsync</code> 和 <code>OnAutoFitContentAsync</code> 回调。</p><p><b>注意事项:</b></p><p>表格状态持久化功能分两种情况,设置 <code>ClientTableName</code> 属性即使用浏览器本地存储持久化信息,如果未设置则通过回调使用服务器端存储持久化信息。持久化信息结构体为 <code>TableColumnClientStatus</code> 由列状态集合以及表格宽度参数组成。</p><p>表格宽度参数默认为 0 当所有列宽度均有值时,表格宽度参数值为表格在客户端渲染的实际宽度;为 0 表示表格宽度自适应</p><p>使用浏览器本地存储持久化信息时,无需任何设置,表格会自动加载客户端状态</p><p>使用服务器端存储持久化信息时,需在回调中处理表格状态加载与保存逻辑,实际使用中可根据需要使用数据库或其他方式进行存储;通过 <code>OnTableColumnClientStatusChanged</code> 回调处理服务器端状态,通过 <code>OnLoadTableColumnClientStatus</code> 回调方法对表格进行持久化状态恢复操作</p>",
4610+
"ClearTableColumnClientStatusDesc": "通过调用表格组件实例方法 <code>ClearTableColumnClientStatus</code> 清除当前持久化设置恢复表格为默认设置",
4611+
"ResetButtonText": "重置",
46104612
"AllowDragOrderIntro": "通过指定 <code>AllowDragColumn</code> 设置表格列允许拖动列标题调整表格列顺序",
46114613
"AllowDragOrderTitle": "允许拖动列标题调整表格列顺序",
46124614
"TablesColumnDescription": "用于展示多条结构类似的数据,可对数据进行排序、筛选、对比或其他自定义操作。",

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-beta20</Version>
4+
<Version>10.6.1-beta21</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/Table/Table.razor

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,12 @@
249249
{
250250
<col style="@GetColWidthString(ExtendButtonColumnWidth)" />
251251
}
252-
@foreach (var col in GetVisibleColumns())
252+
@foreach (var col in _tableColumnStates)
253253
{
254-
<col style="@GetColWidthString(col.GetColumnFixedWidth(DefaultFixedColumnWidth))" />
254+
if (col.Visible)
255+
{
256+
<col style="@GetColWidthString(col.Width)" />
257+
}
255258
}
256259
@if (ShowExtendButtons && !IsExtendButtonsInRowHeader)
257260
{

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -568,9 +568,6 @@ private void ResetVisibleColumnsCache()
568568
{
569569
item.DisplayName = col.GetDisplayName();
570570

571-
// 恢复宽度
572-
col.Width = item.Width;
573-
574571
// 增加到可见列缓存集合
575572
_visibleColumnsCache.Add(col);
576573
}

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

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,7 +1403,6 @@ private async Task OnTableRenderAsync(bool firstRender)
14031403
TableName = ClientTableName,
14041404
DragColumnCallback = nameof(DragColumnCallback),
14051405
FitColumnWidthIncludeHeader,
1406-
AutoFitColumnWidthCallback = nameof(AutoFitColumnWidthCallback),
14071406
ResizeColumnCallback = nameof(ResizeColumnCallback),
14081407
ColumnMinWidth = ColumnMinWidth ?? Options.CurrentValue.TableSettings.ColumnMinWidth,
14091408
ColumnStates = _tableColumnStates,
@@ -1894,8 +1893,6 @@ public async Task ClearTableColumnClientStatus()
18941893
// 清除缓存的列状态
18951894
_tableColumnStateCache.Clear();
18961895

1897-
_resetColumns = true;
1898-
_invoke = true;
18991896
StateHasChanged();
19001897
}
19011898

@@ -1946,21 +1943,8 @@ public async Task ResizeColumnCallback(string name, TableColumnClientStatus colu
19461943
{
19471944
await OnTableColumnClientStatusChanged(name, _tableColumnStateCache);
19481945
}
1949-
}
19501946

1951-
/// <summary>
1952-
/// <para lang="zh">列宽自适应回调方法 由 JavaScript 脚本调用</para>
1953-
/// <para lang="en">Auto Fit Column Width Callback called by JavaScript</para>
1954-
/// </summary>
1955-
[JSInvokable]
1956-
public async Task AutoFitColumnWidthCallback(string fieldName, TableColumnClientStatus columnState)
1957-
{
1958-
UpdateTableColumnState(columnState);
1959-
1960-
if (OnTableColumnClientStatusChanged != null)
1961-
{
1962-
await OnTableColumnClientStatusChanged(fieldName, _tableColumnStateCache);
1963-
}
1947+
StateHasChanged();
19641948
}
19651949

19661950
private void UpdateTableColumnState(TableColumnClientStatus columnState)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,7 @@ const autoFitColumnWidth = async (table, col) => {
623623
const state = getColumnStateObject(table);
624624
saveColumnStateToLocalstorage(table, state);
625625

626-
await table.invoke.invokeMethodAsync(table.options.autoFitColumnWidthCallback, field, state);
626+
await table.invoke.invokeMethodAsync(table.options.resizeColumnCallback, field, state);
627627
}
628628
}
629629

src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,4 @@ private static RenderFragment RenderContent(this ITableColumn col, string? text)
443443
internal static bool GetShowTips(this ITableColumn col) => col.ShowTips ?? false;
444444

445445
internal static Alignment GetAlign(this ITableColumn col) => col.Align ?? Alignment.None;
446-
447-
internal static int? GetColumnFixedWidth(this ITableColumn col, int width) => col.Fixed ? col.Width ?? width : col.Width;
448446
}

0 commit comments

Comments
 (0)