Skip to content

Commit e5fc452

Browse files
AApuciljmay168
andcommitted
Update auto-fit column width callback to include width
Modified the OnAutoFitContentAsync callback to accept the calculated column width as a parameter in both C# and JavaScript. This allows custom logic to consider the current width when determining the final column width, improving flexibility for auto-fit scenarios. Co-Authored-By: ljmay168 <[email protected]>
1 parent b79bf37 commit e5fc452

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,7 +1588,7 @@ private async Task OnContextMenu(MouseEventArgs e, TItem item)
15881588
/// 获得/设置 自动调整列宽回调方法
15891589
/// </summary>
15901590
[Parameter]
1591-
public Func<string, Task<float>>? OnAutoFitContentAsync { get; set; }
1591+
public Func<string, float, Task<float>>? OnAutoFitContentAsync { get; set; }
15921592

15931593
/// <summary>
15941594
/// 列宽自适应方法
@@ -1648,14 +1648,15 @@ public async Task ResizeColumnCallback(int index, float width)
16481648
/// 列宽自适应回调方法 由 JavaScript 脚本调用
16491649
/// </summary>
16501650
/// <param name="fieldName">当前列名称</param>
1651+
/// <param name="calcWidth">当前列宽</param>
16511652
/// <returns></returns>
16521653
[JSInvokable]
1653-
public async Task<float> AutoFitContentCallback(string fieldName)
1654+
public async Task<float> AutoFitContentCallback(string fieldName, float calcWidth)
16541655
{
16551656
float ret = 0;
16561657
if (OnAutoFitContentAsync != null)
16571658
{
1658-
ret = await OnAutoFitContentAsync(fieldName);
1659+
ret = await OnAutoFitContentAsync(fieldName, calcWidth);
16591660
}
16601661
return ret;
16611662
}

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

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -726,26 +726,29 @@ const indexOfCol = col => {
726726

727727
const autoFitColumnWidth = async (table, col) => {
728728
const field = col.getAttribute('data-bb-field');
729-
let widthValue = 0;
730-
if (table.options.autoFitContentCallback !== null) {
731-
widthValue = await table.invoke.invokeMethodAsync(table.options.autoFitContentCallback, field);
732-
}
733729

734730
const index = indexOfCol(col);
735731
let rows = null;
736732
if (table.thead) {
737-
rows = table.body.querySelectorAll('table > tbody > tr:not(.is-detail)');
733+
// https://github.com/dotnetcore/BootstrapBlazor/issues/6864
734+
rows = table.el.querySelectorAll('table > tbody > tr:not(.is-detail), table> thead > tr');
738735
}
739736
else {
740-
rows = table.tables[0].querySelectorAll('table > tbody > tr:not(.is-detail)');
737+
// https://github.com/dotnetcore/BootstrapBlazor/issues/6864
738+
rows = table.tables[0].querySelectorAll('table > tbody > tr:not(.is-detail), table> thead > tr');
741739
}
742740

743-
let maxWidth = widthValue;
744-
if (maxWidth === 0) {
745-
[...rows].forEach(row => {
746-
const cell = row.cells[index];
747-
maxWidth = Math.max(maxWidth, calcCellWidth(cell));
748-
});
741+
let maxWidth = 0;
742+
[...rows].forEach(row => {
743+
const cell = row.cells[index];
744+
maxWidth = Math.max(maxWidth, calcCellWidth(cell));
745+
});
746+
747+
if (table.options.autoFitContentCallback !== null) {
748+
const widthValue = await table.invoke.invokeMethodAsync(table.options.autoFitContentCallback, field, maxWidth);
749+
if (widthValue > 0) {
750+
maxWidth = widthValue;
751+
}
749752
}
750753

751754
if (maxWidth > 0) {

0 commit comments

Comments
 (0)