Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions src/BootstrapBlazor/Components/Table/Table.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1588,7 +1588,7 @@ private async Task OnContextMenu(MouseEventArgs e, TItem item)
/// 获得/设置 自动调整列宽回调方法
/// </summary>
[Parameter]
public Func<string, Task<float>>? OnAutoFitContentAsync { get; set; }
public Func<string, float, Task<float>>? OnAutoFitContentAsync { get; set; }

/// <summary>
/// 列宽自适应方法
Expand Down Expand Up @@ -1648,14 +1648,15 @@ public async Task ResizeColumnCallback(int index, float width)
/// 列宽自适应回调方法 由 JavaScript 脚本调用
/// </summary>
/// <param name="fieldName">当前列名称</param>
/// <param name="calcWidth">当前列宽</param>
/// <returns></returns>
[JSInvokable]
public async Task<float> AutoFitContentCallback(string fieldName)
public async Task<float> AutoFitContentCallback(string fieldName, float calcWidth)
{
float ret = 0;
if (OnAutoFitContentAsync != null)
{
ret = await OnAutoFitContentAsync(fieldName);
ret = await OnAutoFitContentAsync(fieldName, calcWidth);
}
return ret;
}
Expand Down
27 changes: 15 additions & 12 deletions src/BootstrapBlazor/Components/Table/Table.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -726,26 +726,29 @@ const indexOfCol = col => {

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

const index = indexOfCol(col);
let rows = null;
if (table.thead) {
rows = table.body.querySelectorAll('table > tbody > tr:not(.is-detail)');
// https://github.com/dotnetcore/BootstrapBlazor/issues/6864
rows = table.el.querySelectorAll('table > tbody > tr:not(.is-detail), table> thead > tr');
}
else {
rows = table.tables[0].querySelectorAll('table > tbody > tr:not(.is-detail)');
// https://github.com/dotnetcore/BootstrapBlazor/issues/6864
rows = table.tables[0].querySelectorAll('table > tbody > tr:not(.is-detail), table> thead > tr');
}

let maxWidth = widthValue;
if (maxWidth === 0) {
[...rows].forEach(row => {
const cell = row.cells[index];
maxWidth = Math.max(maxWidth, calcCellWidth(cell));
});
let maxWidth = 0;
[...rows].forEach(row => {
const cell = row.cells[index];
maxWidth = Math.max(maxWidth, calcCellWidth(cell));
});

if (table.options.autoFitContentCallback !== null) {
const widthValue = await table.invoke.invokeMethodAsync(table.options.autoFitContentCallback, field, maxWidth);
if (widthValue > 0) {
maxWidth = widthValue;
}
}

if (maxWidth > 0) {
Expand Down
Loading