-
-
Notifications
You must be signed in to change notification settings - Fork 382
feat(Table): update reset column width/order method #7850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -1091,11 +1091,7 @@ private void OnInitParameters() | |||||||||||||||||||||||||||||||
| SearchModel ??= CreateSearchModel(); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||
| /// <para lang="zh">获得/设置 是否为第一次 Render</para> | ||||||||||||||||||||||||||||||||
| /// <para lang="en">Gets or sets Whether it is the first Render</para> | ||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||
| protected bool FirstRender { get; set; } = true; | ||||||||||||||||||||||||||||||||
| private bool _firstRender = true; | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||
| /// <para lang="zh">获得/设置 自动刷新 CancellationTokenSource 实例</para> | ||||||||||||||||||||||||||||||||
|
|
@@ -1140,7 +1136,7 @@ protected override void OnParametersSet() | |||||||||||||||||||||||||||||||
| IsTree = false; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (!FirstRender) | ||||||||||||||||||||||||||||||||
| if (!_firstRender) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| // 动态列模式 | ||||||||||||||||||||||||||||||||
| ResetDynamicContext(); | ||||||||||||||||||||||||||||||||
|
|
@@ -1153,6 +1149,21 @@ protected override void OnParametersSet() | |||||||||||||||||||||||||||||||
| _searchItems = null; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||
| /// <inheritdoc/> | ||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||
| /// <returns></returns> | ||||||||||||||||||||||||||||||||
| protected override async Task OnParametersSetAsync() | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| await base.OnParametersSetAsync(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (!_firstRender) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| // 重新读取浏览器设置 | ||||||||||||||||||||||||||||||||
| await OnTableColumnReset(); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
Comment on lines
+1156
to
+1164
|
||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||||||||
| /// <inheritdoc/> | ||||||||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||||||||
|
|
@@ -1237,6 +1248,54 @@ protected override async Task OnAfterRenderAsync(bool firstRender) | |||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| private async Task OnTableColumnReset() | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| // 动态列模式 | ||||||||||||||||||||||||||||||||
| var cols = new List<ITableColumn>(); | ||||||||||||||||||||||||||||||||
| if (DynamicContext != null && typeof(TItem).IsAssignableTo(typeof(IDynamicObject))) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| cols.AddRange(DynamicContext.GetColumns()); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| else if (AutoGenerateColumns) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| cols.AddRange(Utility.GetTableColumns<TItem>(Columns)); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
| else | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| cols.AddRange(Columns); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (ColumnOrderCallback != null) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| cols = [.. ColumnOrderCallback(cols)]; | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| await ReloadColumnOrdersFromBrowserAsync(cols); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // 查看是否开启列宽序列化 | ||||||||||||||||||||||||||||||||
| await ReloadColumnWidthFromBrowserAsync(cols); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| if (OnColumnCreating != null) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| await OnColumnCreating(cols); | ||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| InternalResetVisibleColumns(cols); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| await ReloadColumnVisibleFromBrowserAsync(); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| Columns.Clear(); | ||||||||||||||||||||||||||||||||
| Columns.AddRange(cols.OrderFunc()); | ||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||
| // set default sortName | ||||||||||||||||||||||||||||||||
| var col = Columns.Find(i => i is { Sortable: true, DefaultSort: true }); | ||||||||||||||||||||||||||||||||
| if (col != null) | ||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||
| SortName = col.GetFieldName(); | ||||||||||||||||||||||||||||||||
| SortOrder = col.DefaultSortOrder; | ||||||||||||||||||||||||||||||||
|
Comment on lines
+1290
to
+1295
|
||||||||||||||||||||||||||||||||
| // set default sortName | |
| var col = Columns.Find(i => i is { Sortable: true, DefaultSort: true }); | |
| if (col != null) | |
| { | |
| SortName = col.GetFieldName(); | |
| SortOrder = col.DefaultSortOrder; | |
| // set default sortName only when sort is not already initialized | |
| if (string.IsNullOrEmpty(SortName) && SortOrder == SortOrder.Unset) | |
| { | |
| var col = Columns.Find(i => i is { Sortable: true, DefaultSort: true }); | |
| if (col != null) | |
| { | |
| SortName = col.GetFieldName(); | |
| SortOrder = col.DefaultSortOrder; | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -536,41 +536,24 @@ const setExcelKeyboardListener = table => { | |
| } | ||
|
|
||
| const resetTableWidth = table => { | ||
| const { options: { scrollWidth } } = table; | ||
| table.tables.forEach(t => { | ||
| const group = [...t.children].find(i => i.nodeName === 'COLGROUP') | ||
| if (group) { | ||
| const colgroup = getLastColgroup(t, group); | ||
| if (colgroup) { | ||
| colgroup.style = null; | ||
| } | ||
| const width = getTableWidthByColumnGroup(t, 100); | ||
| if (width >= t.parentElement.offsetWidth + scrollWidth) { | ||
| t.style.width = `${width}px`; | ||
| } | ||
| else { | ||
| t.style.width = null; | ||
| } | ||
| let width = 0; | ||
| [...group.children].forEach(col => { | ||
| let colWidth = parseInt(col.style.width); | ||
| if (isNaN(colWidth)) { | ||
| colWidth = 100; | ||
| } | ||
| width += colWidth; | ||
| }) | ||
| t.style.width = `${width}px`; | ||
|
|
||
| saveColumnWidth(table); | ||
|
Comment on lines
538
to
552
|
||
| } | ||
| }) | ||
| } | ||
|
|
||
| const getLastColgroup = (table, group) => { | ||
| const p = table.parentElement; | ||
| if (p) { | ||
| const length = group.children.length; | ||
| if (p.classList.contains("table-fixed-header")) { | ||
| return group.children[length - 2]; | ||
| } | ||
| else { | ||
| return group.children[length - 1]; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| const setResizeListener = table => { | ||
| if (table.options.showColumnWidthTooltip) { | ||
| table.handlers.setResizeHandler = e => { | ||
|
|
@@ -1064,33 +1047,15 @@ const saveColumnWidth = table => { | |
| })); | ||
| } | ||
|
|
||
| const getTableWidthByColumnGroup = (table, defaultWidth) => { | ||
| return [...table.querySelectorAll('colgroup col')] | ||
| .map((col, index) => getColumnRenderWidth(table, col, index, defaultWidth)) | ||
| .reduce((accumulator, val) => accumulator + val, 0); | ||
| } | ||
|
|
||
| const getColumnRenderWidth = (table, col, index, defaultWidth) => { | ||
| let width = parseFloat(col.style.width); | ||
| if (!isNaN(width) && width > 0) { | ||
| return width; | ||
| } | ||
|
|
||
| const header = table.querySelectorAll('thead > tr:last-child > th').item(index); | ||
| width = header?.offsetWidth ?? 0; | ||
| if (width > 0) { | ||
| return width; | ||
| } | ||
|
|
||
| const row = [...table.querySelectorAll('tbody > tr')].find(i => !i.classList.contains('is-detail')); | ||
| width = row?.children.item(index)?.offsetWidth ?? 0; | ||
| return width > 0 ? width : defaultWidth; | ||
| } | ||
|
|
||
| const setTableDefaultWidth = table => { | ||
| if (table.tables.length > 0 && isVisible(table.tables[0])) { | ||
| const { scrollWidth, columnMinWidth } = table.options; | ||
| const tableWidth = getTableWidthByColumnGroup(table.tables[0], columnMinWidth); | ||
| const tableWidth = [...table.tables[0].querySelectorAll('col')] | ||
| .map(i => { | ||
| const colWidth = parseFloat(i.style.width); | ||
| return isNaN(colWidth) ? columnMinWidth : colWidth; | ||
| }) | ||
| .reduce((accumulator, val) => accumulator + val, 0); | ||
|
Comment on lines
1051
to
+1058
|
||
|
|
||
| if (tableWidth > table.el.offsetWidth) { | ||
| table.tables[0].style.setProperty('width', `${tableWidth}px`); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The protected FirstRender property was removed and replaced with a private field. Since Table is a public component, removing a protected member is a breaking change for anyone deriving from Table and using FirstRender in subclasses. Consider keeping the protected property (possibly forwarding to _firstRender and marking it [Obsolete]) to preserve backward compatibility.