Skip to content
Open
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
31 changes: 30 additions & 1 deletion src/BootstrapBlazor/Components/Table/Table.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1435,6 +1435,7 @@ private void ResetTableColumns()
{
var item = _tableColumnStates[index];
var col = columnMap[item.Name];
col.Fixed = !string.IsNullOrEmpty(item.Name) && stateMap.TryGetValue(item.Name, out var state) ? stateMap[item.Name].Fixed : false;
if (item.Visible)
Comment on lines 1436 to 1439
{
// 增加到可见列缓存集合
Expand All @@ -1449,7 +1450,8 @@ private void ResetTableColumns()
DisplayName = col.GetDisplayName(),
Name = col.GetFieldName(),
Width = col.Fixed && !col.Width.HasValue ? DefaultFixedColumnWidth : col.Width,
Visible = col.GetVisible(_screenSize)
Visible = col.GetVisible(_screenSize),
Fixed = col.Fixed
};

private async Task OnTableRenderAsync(bool firstRender)
Expand Down Expand Up @@ -1940,6 +1942,33 @@ public async Task FitAllColumnWidth()
await InvokeVoidAsync("fitAllColumnWidth", Id);
}

/// <summary>
/// <para lang="zh">更新表格列客户端状态方法</para>
/// <para lang="en">Update Table Column Client Status Method</para>
/// </summary>
/// <returns></returns>
public async Task<TableColumnClientStatus> UpdateTableColumnClientStatus()
{
if (Columns.Count != 0)
{
// 用户在外面变更了列状态后,为避免用户变更状态丢失,须将变更后的状态同步到缓存中
foreach (var item in Columns)
{
var columnState = _tableColumnStates.Find(x => x.Name == item.GetFieldName());
if (columnState != null)
{
columnState.Fixed = item.Fixed;
columnState.Width = item.Fixed && !item.Width.HasValue ? DefaultFixedColumnWidth : item.Width;
}
}
StateHasChanged();
}
// 如果启用了 ClientTableName 则更新浏览器持久化列状态
await InvokeVoidAsync("updateColumnStates", Id);

return _tableColumnStateCache;
}
Comment on lines +1950 to +1970

/// <summary>
/// <para lang="zh">清除表格列客户端状态实例方法</para>
/// <para lang="en">clear table column client status instance method</para>
Expand Down
19 changes: 16 additions & 3 deletions src/BootstrapBlazor/Components/Table/Table.razor.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,7 @@ const autoFitColumnWidth = async (table, col) => {
const index = indexOfCol(col);
let rows = null;
let maxWidth = getColumnMaxCellWidth(table, index);

if (table.options.fitColumnWidthIncludeHeader) {
const th = getColumnHeader(col);
maxWidth = Math.max(maxWidth, getCellWidth(th));
Expand Down Expand Up @@ -936,6 +936,17 @@ const removeColumnWidthState = tableName => {
localStorage.removeItem(columnWidthKey);
}

export function updateColumnStates(id) {
const el = document.getElementById(id)
if (el === null) {
return
}
let table = Data.get(id)
table.el = el;
Data.set(id, table)
const state = getColumnStateObject(table);
saveColumnStateToLocalstorage(table, state);
}
Comment on lines +939 to +949
export function clearColumnStates(tableName) {
const columnStateKey = `bb-table-${tableName}`;
localStorage.removeItem(columnStateKey);
Expand Down Expand Up @@ -976,7 +987,8 @@ const getColumnStateObject = table => {
return {
name: col.name,
width: getColumnWidth(col, table.columns),
visible: col.visible
visible: col.visible,
fixed: col.fixed
}
}),
table: getTableWidth(table.tables[0])
Expand All @@ -988,7 +1000,8 @@ const getColumnStateObject = table => {
return {
name: getColumnName(col),
width: getResizableColumnWidth(col),
visible: true
visible: true,
fixed: getColumnHeader(col).classList.contains('fixed')
}
}),
table: getTableWidth(table.tables[0])
Expand Down
6 changes: 6 additions & 0 deletions src/BootstrapBlazor/Components/Table/TableColumnState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,10 @@ public class TableColumnState
/// <para lang="en">Gets or sets column width</para>
/// </summary>
public int? Width { get; set; }

/// <summary>
/// <para lang="zh">获得/设置 列固定</para>
/// <para lang="en">Gets or sets column fixed</para>
/// </summary>
public bool Fixed { get; set; }
}
56 changes: 56 additions & 0 deletions test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9018,6 +9018,62 @@
Assert.NotNull(clientState);
}

[Fact]
public async Task UpdateTableColumnClientStatus_Ok()
{
var state = new TableColumnClientStatus();
state.TableWidth = 220;
state.Columns.Add(new TableColumnState() { Name = nameof(Foo.Name), Visible = true, Fixed = true });
state.Columns.Add(new TableColumnState() { Name = nameof(Foo.Address), Visible = true, Width = 120, Fixed = false });

Context.JSInterop.Setup<TableColumnClientStatus>("getColumnStates", "test_update").SetResult(state);
var invoker = Context.JSInterop.SetupVoid("updateColumnStates", "test_update");
invoker.SetVoidResult();

var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var cut = Context.Render<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.ClientTableName, "test_update");
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.AllowResizing, true);
pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer));
Comment on lines +9036 to +9041
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.AddAttribute(3, "Fixed", true);
builder.CloseComponent();

builder.OpenComponent<TableColumn<Foo, string>>(0);
builder.AddAttribute(3, "Field", "Address");
builder.AddAttribute(4, "FieldExpression", Utility.GenerateValueExpression(foo, "Address", typeof(string)));
builder.CloseComponent();
});
});
});

var table = cut.FindComponent<Table<Foo>>();
var colGroup = table.Find("colgroup");
Assert.Contains("style=\"width: 120px;\"", colGroup.ToMarkup());

var status = await cut.InvokeAsync(() => table.Instance.UpdateTableColumnClientStatus());
Assert.Equal(true, status.Columns[0].Fixed);

Check warning on line 9063 in test/UnitTest/Components/TableTest.cs

View workflow job for this annotation

GitHub Actions / run test

Do not use Assert.Equal() to check for boolean conditions. Use Assert.True instead. (https://xunit.net/xunit.analyzers/rules/xUnit2004)
Assert.Equal(state.Columns.Count, status.Columns.Count);

table = cut.FindComponent<Table<Foo>>();
var columns = cut.FindAll("th");
colGroup = table.Find("colgroup");
Assert.Contains("style=\"width: 200px;\"", colGroup.ToMarkup());
if (columns[0].ClassName.Contains("fixed"))

Check warning on line 9070 in test/UnitTest/Components/TableTest.cs

View workflow job for this annotation

GitHub Actions / run test

Dereference of a possibly null reference.
{
var fixedWidth = cut.FindAll("col")[0].OuterHtml.Contains("width: 200px");
Assert.Equal("fixedWidth:True", $"fixedWidth:{fixedWidth}");
}
}

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