Skip to content

Commit

Permalink
fix(Table): scroll to top when click row on VirtualScroll mode (#3794)
Browse files Browse the repository at this point in the history
* refactor: 更新注释文档

* fix: 增加 FilterTrigger 变量过滤时滚动条置顶

* feat: 表格虚拟滚动时内部禁止分页

* test: 更新单元测试

* chore: bump version 8.7.1-beta06
  • Loading branch information
ArgoZhang authored Jul 6, 2024
1 parent b89b03c commit ef99307
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
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>8.7.1-beta05</Version>
<Version>8.7.1-beta06</Version>
</PropertyGroup>

<ItemGroup Condition="'$(TargetFramework)' == 'net5.0'">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public partial class TableFilter : IFilter
public string? FilterButtonText { get; set; }

/// <summary>
/// 获得/设置 Table Header 实例
/// 获得/设置 ITable 实例
/// </summary>
[Parameter]
public ITable? Table { get; set; }
Expand Down
12 changes: 10 additions & 2 deletions src/BootstrapBlazor/Components/Table/Table.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,8 @@ public async Task ExpandDetailRow(TItem item)

private bool UpdateSortTooltip { get; set; }

private bool _isFilterTrigger;

/// <summary>
/// OnInitialized 方法
/// </summary>
Expand Down Expand Up @@ -695,6 +697,10 @@ protected override void OnInitialized()
{
PageIndex = 1;
TotalCount = 0;
if (ScrollMode == ScrollMode.Virtual)
{
_isFilterTrigger = true;
}
return QueryAsync();
};
}
Expand Down Expand Up @@ -817,6 +823,7 @@ protected override void OnParametersSet()
{
IsFixedHeader = true;
RenderMode = TableRenderMode.Table;
IsPagination = false;
}

RowsCache = null;
Expand Down Expand Up @@ -880,8 +887,9 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
await InvokeVoidAsync("scroll", Id, AutoScrollVerticalAlign.ToDescriptionString());
}

if (ScrollMode == ScrollMode.Virtual)
if (_isFilterTrigger)
{
_isFilterTrigger = false;
await InvokeVoidAsync("scrollTo", Id);
}

Expand Down Expand Up @@ -1267,7 +1275,7 @@ void SetEditTemplate()

private async ValueTask<ItemsProviderResult<TItem>> LoadItems(ItemsProviderRequest request)
{
StartIndex = request.StartIndex;
StartIndex = _isFilterTrigger ? 0 : request.StartIndex;
PageItems = TotalCount > 0 ? Math.Min(request.Count, TotalCount - request.StartIndex) : request.Count;
await QueryData();
return new ItemsProviderResult<TItem>(QueryItems, TotalCount);
Expand Down
41 changes: 41 additions & 0 deletions test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using Microsoft.AspNetCore.Components.Forms;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.Web.Virtualization;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Localization;
using Microsoft.Extensions.Options;
Expand Down Expand Up @@ -2313,6 +2314,44 @@ public void Filterable_Ok()
cut.Contains("card filter-item");
}

[Fact]
public async Task Filterable_Virtualize()
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var cut = Context.RenderComponent<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.ScrollMode, ScrollMode.Virtual);
pb.Add(a => a.ShowFilterHeader, false);
pb.Add(a => a.IsPagination, true);
pb.Add(a => a.OnQueryAsync, option =>
{
var items = Foo.GenerateFoo(localizer, 5);
var ret = new QueryData<Foo>()
{
Items = items,
TotalCount = 5
};
return Task.FromResult(ret);
});
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, "Filterable", true);
builder.CloseComponent();
});
});
});
var table = cut.FindComponent<Table<Foo>>();
Assert.False(table.Instance.IsPagination);

Assert.NotNull(table.Instance.OnFilterAsync);
await cut.InvokeAsync(() => table.Instance.OnFilterAsync());
}

[Fact]
public async Task CustomerToolbarPopConfirmButton_Ok()
{
Expand Down Expand Up @@ -2660,6 +2699,8 @@ public void ScrollMode_Ok()
});
});
});
var virtualComponent = cut.FindComponent<Virtualize<Foo>>();
Assert.NotNull(virtualComponent);
}

[Fact]
Expand Down

0 comments on commit ef99307

Please sign in to comment.