Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
IsPagination="true" PageItemsSource="@PageItemsSource"
IsStriped="true" IsBordered="true" IsMultipleSelect="true"
ShowToolbar="true" ShowAddButton="false" ShowEditButton="false" ShowDeleteButton="false"
ShowExtendButtons="false" ShowColumnList="true"
ShowExtendButtons="false" ShowColumnList="true" ClientTableName="testtable"
OnQueryAsync="@OnQueryAsync">
<TableColumns>
<TableColumn @bind-Field="@context.DateTime" Width="180" />
Expand Down
7 changes: 6 additions & 1 deletion src/BootstrapBlazor/Components/Table/Table.razor.Checkbox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
// See the LICENSE file in the project root for more information.
// Maintainer: Argo Zhang([email protected]) Website: https://www.blazor.zone

using System.Text.Json;

namespace BootstrapBlazor.Components;

public partial class Table<TItem>
Expand Down Expand Up @@ -155,7 +157,10 @@ private async Task OnToggleColumnVisible(string columnName, bool visible)
{
_resetColumns = true;
}

if(ClientTableName != null && ShowColumnList)
{
await JSRuntime.InvokeVoidAsync("localStorage.setItem", "bb-table-column-visiable-"+ClientTableName, JsonSerializer.Serialize(_visibleColumns) ?? "");
}
if (OnColumnVisibleChanged != null)
{
await OnColumnVisibleChanged(columnName, visible);
Expand Down
8 changes: 4 additions & 4 deletions src/BootstrapBlazor/Components/Table/Table.razor.Toolbar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ public async Task AddAsync()
{
// 数据源为 DataTable 新建后重建行与列
await DynamicContext.AddAsync(SelectedRows.OfType<IDynamicObject>());
ResetDynamicContext();
await ResetDynamicContext();

if (!IsKeepSelectedRowAfterAdd)
{
Expand Down Expand Up @@ -1030,7 +1030,7 @@ protected async Task DeleteAsync()
if (DynamicContext != null)
{
await DynamicContext.DeleteAsync(SelectedRows.OfType<IDynamicObject>());
ResetDynamicContext();
await ResetDynamicContext();
SelectedRows.Clear();
await OnSelectedRowsChanged();
}
Expand Down Expand Up @@ -1098,7 +1098,7 @@ async Task<bool> DeleteItemsAsync()
}
}

private void ResetDynamicContext()
private async Task ResetDynamicContext()
{
if (DynamicContext != null)
{
Expand All @@ -1112,7 +1112,7 @@ private void ResetDynamicContext()
FirstFixedColumnCache.Clear();
LastFixedColumnCache.Clear();

InternalResetVisibleColumns(Columns);
await InternalResetVisibleColumns(Columns);

var queryOption = BuildQueryPageOptions();
// 设置是否为首次查询
Expand Down
32 changes: 28 additions & 4 deletions src/BootstrapBlazor/Components/Table/Table.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -981,7 +981,7 @@
if (!FirstRender)
{
// 动态列模式
ResetDynamicContext();

Check warning on line 984 in src/BootstrapBlazor/Components/Table/Table.razor.cs

View workflow job for this annotation

GitHub Actions / run test

Because this call is not awaited, execution of the current method continues before the call is completed. Consider applying the 'await' operator to the result of the call.

// resize column width;
ResetColumnWidth(Columns);
Expand Down Expand Up @@ -1207,7 +1207,7 @@
await OnColumnCreating(cols);
}

InternalResetVisibleColumns(cols);
await InternalResetVisibleColumns(cols);

Columns.Clear();
Columns.AddRange(cols.OrderFunc());
Expand Down Expand Up @@ -1258,9 +1258,33 @@
}
}

private void InternalResetVisibleColumns(List<ITableColumn> columns, IEnumerable<ColumnVisibleItem>? items = null)
private async Task InternalResetVisibleColumns(List<ITableColumn> columns, IEnumerable<ColumnVisibleItem>? items = null)
{
var cols = columns.Select(i => new ColumnVisibleItem(i.GetFieldName(), i.GetVisible()) { DisplayName = i.GetDisplayName() }).ToList();
List<ColumnVisibleItem>? ret = null;
if (ClientTableName != null && ShowColumnList)
{
var jsonData = await JSRuntime.InvokeAsync<string>("localStorage.getItem", "bb-table-column-visiable-" + ClientTableName);
if (!string.IsNullOrEmpty(jsonData))
{
try
{
ret = JsonSerializer.Deserialize<List<ColumnVisibleItem>>(jsonData, _serializerOption);
}
catch { }
if(ret != null)
{
foreach (var i in ret)
{
var col = cols.FirstOrDefault(d => d.Name == i.Name && d.DisplayName == i.DisplayName);
if (col != null)
{
col.Visible = i.Visible;
}
}
}
}
}
if (items != null)
{
foreach (var column in cols)
Expand All @@ -1284,15 +1308,15 @@
/// 设置 列可见方法
/// </summary>
/// <param name="columns"></param>
public void ResetVisibleColumns(IEnumerable<ColumnVisibleItem> columns)
public async Task ResetVisibleColumns(IEnumerable<ColumnVisibleItem> columns)
{
// https://github.com/dotnetcore/BootstrapBlazor/issues/6823
if (AllowResizing)
{
_resetColumns = true;
}

InternalResetVisibleColumns(Columns, columns);
await InternalResetVisibleColumns(Columns, columns);
StateHasChanged();
}

Expand Down
Loading