Skip to content

Commit 512920f

Browse files
authored
feat(Table): use localstorage keep table column width (#2413)
* feat: 增加固定列宽功能 * refactor: 增加 tableName tableWidth 处理逻辑 * chore: bump version 8.0.3-beta02
1 parent a419f96 commit 512920f

File tree

5 files changed

+107
-8
lines changed

5 files changed

+107
-8
lines changed

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>8.0.3-beta01</Version>
4+
<Version>8.0.3-beta02</Version>
55
</PropertyGroup>
66

77
<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright (c) Argo Zhang ([email protected]). All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
// Website: https://www.blazor.zone or https://argozhang.github.io/
4+
5+
namespace BootstrapBlazor.Components;
6+
7+
/// <summary>
8+
/// 列宽设置类
9+
/// </summary>
10+
class ColumnWidth
11+
{
12+
/// <summary>
13+
/// 获得/设置 列名称
14+
/// </summary>
15+
public string? Name { get; set; }
16+
17+
/// <summary>
18+
/// 获得/设置 列宽
19+
/// </summary>
20+
public int Width { get; set; }
21+
}

src/BootstrapBlazor/Components/Table/Table.razor

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@
195195
if (IsFixedHeader)
196196
{
197197
<div class="table-fixed-header">
198-
<table class="@TableClassName">
198+
<table class="@TableClassName" style="@GetTableStyleString(true)" data-bb-name="@GetTableName(true)">
199199
@RenderColGroup(true)
200200
@RenderHeader(true)
201201
</table>
@@ -300,7 +300,7 @@
300300

301301
@code {
302302
RenderFragment<bool> RenderTable => hasHeader =>
303-
@<table class="@TableClassName">
303+
@<table class="@TableClassName" style="@GetTableStyleString(hasHeader)" data-bb-name="@GetTableName(hasHeader)">
304304
@RenderColGroup.Invoke(false)
305305
@if (hasHeader)
306306
{
@@ -430,7 +430,7 @@
430430
}
431431
@foreach (var col in GetVisibleColumns())
432432
{
433-
@if (CheckShownWithBreakpoint(col))
433+
if (CheckShownWithBreakpoint(col))
434434
{
435435
<col style="@GetColWidthString(col.Width)" />
436436
}
@@ -532,7 +532,7 @@
532532
</div>
533533
@if (!col.Fixed && AllowResizing)
534534
{
535-
<span class="col-resizer"></span>
535+
<span class="col-resizer" data-bb-field="@col.GetFieldName()"></span>
536536
}
537537
</DynamicElement>
538538
}
@@ -855,7 +855,7 @@
855855
@if (isInCell)
856856
{
857857
<Button Size="Size.ExtraSmall" OnClick="ClickUpdateButtonCallback" Color="Color.Success" Icon="@SaveButtonIcon" Text="@SaveButtonText"></Button>
858-
if (!IsTracking)
858+
@if (!IsTracking)
859859
{
860860
<Button Size="Size.ExtraSmall" OnClick="@CancelSave" Color="Color.Warning" Icon="@CancelButtonIcon" Text="@CancelButtonText"></Button>
861861
}

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

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.AspNetCore.Components.Web;
66
using Microsoft.AspNetCore.Components.Web.Virtualization;
77
using System.Reflection;
8+
using System.Text.Json;
89

910
namespace BootstrapBlazor.Components;
1011

@@ -574,6 +575,12 @@ public void ExpandDetailRow(TItem item)
574575
[Parameter]
575576
public Func<PropertyInfo, TItem, List<SearchFilterAction>?>? GetAdvancedSearchFilterCallback { get; set; }
576577

578+
/// <summary>
579+
/// 获得/设置 表格名称 默认 null 用于列宽持久化功能
580+
/// </summary>
581+
[Parameter]
582+
public string? TableName { get; set; }
583+
577584
[CascadingParameter]
578585
[NotNull]
579586
private ContextMenuZone? ContextMenuZone { get; set; }
@@ -823,6 +830,43 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
823830
}
824831
}
825832

833+
private int? _localStorageTableWidth;
834+
835+
private string? GetTableStyleString(bool hasHeader) => hasHeader && _localStorageTableWidth.HasValue
836+
? $"width: {_localStorageTableWidth.Value}px;"
837+
: null;
838+
839+
private string? GetTableName(bool hasHeader) => hasHeader ? TableName : null;
840+
841+
private async Task<IEnumerable<ColumnWidth>> ReloadColumnWidth()
842+
{
843+
IEnumerable<ColumnWidth>? ret = null;
844+
if (!string.IsNullOrEmpty(TableName) && AllowResizing)
845+
{
846+
var jsonData = await InvokeAsync<string>("reloadColumnWidth", Id, TableName);
847+
if (!string.IsNullOrEmpty(jsonData))
848+
{
849+
try
850+
{
851+
var doc = JsonDocument.Parse(jsonData);
852+
if (doc.RootElement.TryGetProperty("cols", out var element))
853+
{
854+
ret = element.Deserialize<IEnumerable<ColumnWidth>>(new JsonSerializerOptions()
855+
{
856+
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
857+
});
858+
}
859+
if (doc.RootElement.TryGetProperty("table", out var tableEl) && tableEl.TryGetInt32(out var tableWidth))
860+
{
861+
_localStorageTableWidth = tableWidth;
862+
}
863+
}
864+
catch { }
865+
}
866+
}
867+
return ret ?? Enumerable.Empty<ColumnWidth>();
868+
}
869+
826870
private async Task ProcessFirstRender()
827871
{
828872
IsLoading = true;
@@ -855,6 +899,20 @@ private async Task ProcessFirstRender()
855899

856900
InternalResetVisibleColumns(Columns.Select(i => new ColumnVisibleItem(i.GetFieldName(), i.Visible)));
857901

902+
// 查看是否开启列宽序列化
903+
var columnWidths = await ReloadColumnWidth();
904+
if (columnWidths != null)
905+
{
906+
foreach (var cw in columnWidths.Where(c => c.Width > 0))
907+
{
908+
var c = Columns.Find(c => c.GetFieldName() == cw.Name);
909+
if (c != null)
910+
{
911+
c.Width = cw.Width;
912+
}
913+
}
914+
}
915+
858916
// set default sortName
859917
var col = Columns.Find(i => i.Sortable && i.DefaultSort);
860918
if (col != null)
@@ -1186,13 +1244,13 @@ private async Task OnContextMenu(MouseEventArgs e, TItem item)
11861244
private string? DraggableString => AllowDragColumn ? "true" : null;
11871245

11881246
/// <summary>
1189-
/// 获得/设置 拖动列结束回调方法
1247+
/// 获得/设置 拖动列结束回调方法
11901248
/// </summary>
11911249
[Parameter]
11921250
public Func<string, IEnumerable<ITableColumn>, Task>? OnDragColumnEndAsync { get; set; }
11931251

11941252
/// <summary>
1195-
/// 获得/设置 设置列宽回调方法
1253+
/// 获得/设置 设置列宽回调方法
11961254
/// </summary>
11971255
[Parameter]
11981256
public Func<string, float, Task>? OnResizeColumnAsync { get; set; }

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,8 @@ const setResizeListener = table => {
283283
const currentIndex = [...table.tables[0].querySelectorAll('thead > tr > th > .col-resizer')].indexOf(col)
284284
table.invoke.invokeMethodAsync(table.callbacks.resizeColumnCallback, currentIndex, width)
285285
}
286+
287+
saveColumnWidth(table)
286288
}
287289
)
288290
})
@@ -459,6 +461,24 @@ export function init(id, invoke, callbacks) {
459461
reset(id)
460462
}
461463

464+
export function reloadColumnWidth(id, tableName) {
465+
const key = `bb-table-column-width-${tableName}`
466+
return localStorage.getItem(key);
467+
}
468+
469+
const saveColumnWidth = table => {
470+
const cols = table.columns
471+
const tableWidth = table.tables[0].offsetWidth
472+
const tableName = table.tables[0].getAttribute('data-bb-name')
473+
const key = `bb-table-column-width-${tableName}`
474+
localStorage.setItem(key, JSON.stringify({
475+
"cols": cols.map(col => {
476+
return { "width": col.closest('th').offsetWidth, "name": col.getAttribute('data-bb-field') }
477+
}),
478+
"table": tableWidth
479+
}));
480+
}
481+
462482
export function reset(id) {
463483
const table = Data.get(id)
464484

0 commit comments

Comments
 (0)