|
5 | 5 | using Microsoft.AspNetCore.Components.Web; |
6 | 6 | using Microsoft.AspNetCore.Components.Web.Virtualization; |
7 | 7 | using System.Reflection; |
| 8 | +using System.Text.Json; |
8 | 9 |
|
9 | 10 | namespace BootstrapBlazor.Components; |
10 | 11 |
|
@@ -574,6 +575,12 @@ public void ExpandDetailRow(TItem item) |
574 | 575 | [Parameter] |
575 | 576 | public Func<PropertyInfo, TItem, List<SearchFilterAction>?>? GetAdvancedSearchFilterCallback { get; set; } |
576 | 577 |
|
| 578 | + /// <summary> |
| 579 | + /// 获得/设置 表格名称 默认 null 用于列宽持久化功能 |
| 580 | + /// </summary> |
| 581 | + [Parameter] |
| 582 | + public string? TableName { get; set; } |
| 583 | + |
577 | 584 | [CascadingParameter] |
578 | 585 | [NotNull] |
579 | 586 | private ContextMenuZone? ContextMenuZone { get; set; } |
@@ -823,6 +830,43 @@ protected override async Task OnAfterRenderAsync(bool firstRender) |
823 | 830 | } |
824 | 831 | } |
825 | 832 |
|
| 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 | + |
826 | 870 | private async Task ProcessFirstRender() |
827 | 871 | { |
828 | 872 | IsLoading = true; |
@@ -855,6 +899,20 @@ private async Task ProcessFirstRender() |
855 | 899 |
|
856 | 900 | InternalResetVisibleColumns(Columns.Select(i => new ColumnVisibleItem(i.GetFieldName(), i.Visible))); |
857 | 901 |
|
| 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 | + |
858 | 916 | // set default sortName |
859 | 917 | var col = Columns.Find(i => i.Sortable && i.DefaultSort); |
860 | 918 | if (col != null) |
@@ -1186,13 +1244,13 @@ private async Task OnContextMenu(MouseEventArgs e, TItem item) |
1186 | 1244 | private string? DraggableString => AllowDragColumn ? "true" : null; |
1187 | 1245 |
|
1188 | 1246 | /// <summary> |
1189 | | - /// 获得/设置 拖动列结束回调方法 |
| 1247 | + /// 获得/设置 拖动列结束回调方法 |
1190 | 1248 | /// </summary> |
1191 | 1249 | [Parameter] |
1192 | 1250 | public Func<string, IEnumerable<ITableColumn>, Task>? OnDragColumnEndAsync { get; set; } |
1193 | 1251 |
|
1194 | 1252 | /// <summary> |
1195 | | - /// 获得/设置 设置列宽回调方法 |
| 1253 | + /// 获得/设置 设置列宽回调方法 |
1196 | 1254 | /// </summary> |
1197 | 1255 | [Parameter] |
1198 | 1256 | public Func<string, float, Task>? OnResizeColumnAsync { get; set; } |
|
0 commit comments