Skip to content

Commit 951e97a

Browse files
authored
feat(Table): add FitAllColumnWidth instance method (#6857)
* feat(Table): add FitAllColumnWidth instance method * refactor: 增加 border 宽度 * chore: bump version 9.11.1 * test: 增加单元测试
1 parent 826819f commit 951e97a

File tree

4 files changed

+53
-3
lines changed

4 files changed

+53
-3
lines changed

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>9.11.1-beta06</Version>
4+
<Version>9.11.1</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,6 +1590,14 @@ private async Task OnContextMenu(MouseEventArgs e, TItem item)
15901590
[Parameter]
15911591
public Func<string, Task<float>>? OnAutoFitContentAsync { get; set; }
15921592

1593+
/// <summary>
1594+
/// 列宽自适应方法
1595+
/// </summary>
1596+
public async Task FitAllColumnWidth()
1597+
{
1598+
await InvokeVoidAsync("fitAllColumnWidth", Id);
1599+
}
1600+
15931601
/// <summary>
15941602
/// 重置列方法 由 JavaScript 脚本调用
15951603
/// </summary>

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,18 @@ export function reset(id) {
114114
observeHeight(table)
115115
}
116116

117+
export function fitAllColumnWidth(id) {
118+
const table = Data.get(id)
119+
if (table === null) {
120+
return;
121+
}
122+
123+
const columns = [...table.tables[0].querySelectorAll('.col-resizer')];
124+
columns.forEach(async col => {
125+
await autoFitColumnWidth(table, col);
126+
});
127+
}
128+
117129
const observeHeight = table => {
118130
setBodyHeight(table);
119131

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

775787
const cellStyle = getComputedStyle(cell);
776-
const width = div.offsetWidth + parseFloat(cellStyle.getPropertyValue('padding-left')) + parseFloat(cellStyle.getPropertyValue('padding-right'));
777-
div.remove();
788+
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;
778789
return width;
779790
}
780791

test/UnitTest/Components/TableTest.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6030,6 +6030,37 @@ public void ReloadColumnWidth_Columns_Invalid()
60306030
Assert.DoesNotContain("<col style=\"width: 20px;\" />", table.Markup);
60316031
}
60326032

6033+
[Fact]
6034+
public async Task FitAllColumnWidth_Ok()
6035+
{
6036+
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
6037+
var items = Foo.GenerateFoo(localizer, 2);
6038+
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
6039+
{
6040+
pb.AddChildContent<Table<Foo>>(pb =>
6041+
{
6042+
pb.Add(a => a.RenderMode, TableRenderMode.Table);
6043+
pb.Add(a => a.AllowResizing, true);
6044+
pb.Add(a => a.Items, items);
6045+
pb.Add(a => a.TableColumns, foo => builder =>
6046+
{
6047+
builder.OpenComponent<TableColumn<Foo, string>>(0);
6048+
builder.AddAttribute(1, "Field", "Name");
6049+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
6050+
builder.CloseComponent();
6051+
6052+
builder.OpenComponent<TableColumn<Foo, string>>(0);
6053+
builder.AddAttribute(1, "Field", "Address");
6054+
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Address", typeof(string)));
6055+
builder.CloseComponent();
6056+
});
6057+
});
6058+
});
6059+
var table = cut.FindComponent<Table<Foo>>();
6060+
Assert.NotNull(table);
6061+
await cut.InvokeAsync(() => table.Instance.FitAllColumnWidth());
6062+
}
6063+
60336064
[Fact]
60346065
public async Task Refresh_Ok()
60356066
{

0 commit comments

Comments
 (0)