Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion src/BootstrapBlazor/BootstrapBlazor.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<Version>9.11.1-beta06</Version>
<Version>9.11.1</Version>
</PropertyGroup>

<ItemGroup>
Expand Down
8 changes: 8 additions & 0 deletions src/BootstrapBlazor/Components/Table/Table.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,14 @@ private async Task OnContextMenu(MouseEventArgs e, TItem item)
[Parameter]
public Func<string, Task<float>>? OnAutoFitContentAsync { get; set; }

/// <summary>
/// 列宽自适应方法
/// </summary>
public async Task FitAllColumnWidth()
{
await InvokeVoidAsync("fitAllColumnWidth", Id);
}

/// <summary>
/// 重置列方法 由 JavaScript 脚本调用
/// </summary>
Expand Down
15 changes: 13 additions & 2 deletions src/BootstrapBlazor/Components/Table/Table.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,18 @@ export function reset(id) {
observeHeight(table)
}

export function fitAllColumnWidth(id) {
const table = Data.get(id)
if (table === null) {
return;
}

const columns = [...table.tables[0].querySelectorAll('.col-resizer')];
columns.forEach(async col => {
await autoFitColumnWidth(table, col);
});
}

const observeHeight = table => {
setBodyHeight(table);

Expand Down Expand Up @@ -773,8 +785,7 @@ const calcCellWidth = cell => {
document.body.appendChild(div);

const cellStyle = getComputedStyle(cell);
const width = div.offsetWidth + parseFloat(cellStyle.getPropertyValue('padding-left')) + parseFloat(cellStyle.getPropertyValue('padding-right'));
div.remove();
const width = div.offsetWidth + parseFloat(cellStyle.getPropertyValue('padding-left')) + parseFloat(cellStyle.getPropertyValue('padding-right')) + parseFloat(cellStyle.getPropertyValue('border-left-width')) + parseFloat(cellStyle.getPropertyValue('border-right-width')) + 1;
return width;
}

Expand Down
31 changes: 31 additions & 0 deletions test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6030,6 +6030,37 @@ public void ReloadColumnWidth_Columns_Invalid()
Assert.DoesNotContain("<col style=\"width: 20px;\" />", table.Markup);
}

[Fact]
public async Task FitAllColumnWidth_Ok()
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var items = Foo.GenerateFoo(localizer, 2);
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.AllowResizing, true);
pb.Add(a => a.Items, items);
pb.Add(a => a.TableColumns, foo => builder =>
{
builder.OpenComponent<TableColumn<Foo, string>>(0);
builder.AddAttribute(1, "Field", "Name");
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
builder.CloseComponent();

builder.OpenComponent<TableColumn<Foo, string>>(0);
builder.AddAttribute(1, "Field", "Address");
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Address", typeof(string)));
builder.CloseComponent();
});
});
});
var table = cut.FindComponent<Table<Foo>>();
Assert.NotNull(table);
await cut.InvokeAsync(() => table.Instance.FitAllColumnWidth());
}

[Fact]
public async Task Refresh_Ok()
{
Expand Down
Loading