Skip to content

Commit 826819f

Browse files
authored
feat(Table): make OnAutoFitContentAsync work (#6856)
* refactor: 增加条件提高性能 * feat: 增加 OnAutoFitContentAsync 回调
1 parent cbb85fb commit 826819f

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ private async Task OnTableRenderAsync(bool firstRender)
10651065
await InvokeVoidAsync("init", Id, Interop, new
10661066
{
10671067
DragColumnCallback = nameof(DragColumnCallback),
1068-
AutoFitContentCallback = nameof(AutoFitContentCallback),
1068+
AutoFitContentCallback = OnAutoFitContentAsync == null ? null : nameof(AutoFitContentCallback),
10691069
ResizeColumnCallback = OnResizeColumnAsync != null ? nameof(ResizeColumnCallback) : null,
10701070
ColumnMinWidth = ColumnMinWidth ?? Options.CurrentValue.TableSettings.ColumnMinWidth,
10711071
ScrollWidth = ActualScrollWidth,

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,10 @@ const indexOfCol = col => {
714714

715715
const autoFitColumnWidth = async (table, col) => {
716716
const field = col.getAttribute('data-bb-field');
717-
const widthValue = await table.invoke.invokeMethodAsync(table.options.autoFitContentCallback, field);
717+
let widthValue = 0;
718+
if (table.options.autoFitContentCallback !== null) {
719+
widthValue = await table.invoke.invokeMethodAsync(table.options.autoFitContentCallback, field);
720+
}
718721

719722
const index = indexOfCol(col);
720723
let rows = null;
@@ -725,11 +728,13 @@ const autoFitColumnWidth = async (table, col) => {
725728
rows = table.tables[0].querySelectorAll('table > tbody > tr:not(.is-detail)');
726729
}
727730

728-
let maxWidth = 0;
729-
[...rows].forEach(row => {
730-
const cell = row.cells[index];
731-
maxWidth = Math.max(maxWidth, calcCellWidth(cell));
732-
});
731+
let maxWidth = widthValue;
732+
if (maxWidth === 0) {
733+
[...rows].forEach(row => {
734+
const cell = row.cells[index];
735+
maxWidth = Math.max(maxWidth, calcCellWidth(cell));
736+
});
737+
}
733738

734739
if (maxWidth > 0) {
735740
table.tables.forEach(table => {

0 commit comments

Comments
 (0)