Skip to content

Commit 36a0211

Browse files
authored
feat(Table): add IsAutoScrollTopWhenClickPage parameter (#6077)
* refactor: 更新键值 * feat: 增加 IsAutoScrollTopWhenClickPage 参数 * doc: 更新示例 * chore: bump vesion 9.6.4 * refactor: 更新单元测试 * refactor: 重构代码 * refactor: 合并脚本 * test: 更新单元测试
1 parent 10a6739 commit 36a0211

File tree

8 files changed

+37
-11
lines changed

8 files changed

+37
-11
lines changed

src/BootstrapBlazor.Server/Components/Samples/Table/TablesPages.razor

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
<DemoBlock Title="@Localizer["TablesPageShowTopPaginationTitle"]"
2323
Introduction="@Localizer["TablesPageShowTopPaginationIntro"]"
2424
Name="ShowTopPagination">
25-
<Table TItem="Foo"
26-
IsPagination="true" ShowTopPagination="true" PageItemsSource="@PageItemsSource" _pageItems="10" OnQueryAsync="@OnQueryAsync">
25+
<Table TItem="Foo" IsAutoScrollTopWhenClickPage="true" IsFixedHeader="true" Height="200"
26+
IsPagination="true" ShowTopPagination="true" PageItemsSource="@PageItemsSource" OnQueryAsync="@OnQueryAsync">
2727
<TableColumns>
2828
<TableColumn @bind-Field="@context.DateTime" Width="180" />
2929
<TableColumn @bind-Field="@context.Name" />

src/BootstrapBlazor.Server/Locales/en-US.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"TablesPagePaginationTitle": "Pagination table",
7979
"TablesPagePaginationIntro": "Set <code>IsPagination</code> to display the pagination component",
8080
"TablesPageShowTopPaginationTitle": "Show on top",
81-
"TablesPageShowTopPaginationIntro": "Set <code>ShowTopPagination</code> to <code>true</code> is the top display pagination component"
81+
"TablesPageShowTopPaginationIntro": "Set <code>ShowTopPagination</code> to <code>true</code> is the top display pagination component. You can use <code>IsAutoScrollTopWhenClickPage</code> to control whether to automatically scroll to the top after turning the page. The default value is <code>false</code> to keep the scroll bar position"
8282
},
8383
"BootstrapBlazor.Server.Components.Samples.Table.TablesFixedColumn": {
8484
"TablesFixedColumnTitle": "Fixed column",

src/BootstrapBlazor.Server/Locales/zh-CN.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
"TablesPagePaginationTitle": "分页表格",
7979
"TablesPagePaginationIntro": "设置 <code>IsPagination</code> 显示分页组件",
8080
"TablesPageShowTopPaginationTitle": "显示在顶端",
81-
"TablesPageShowTopPaginationIntro": "设置 <code>ShowTopPagination</code> 为 <code>true</code> 是顶端显示分页组件"
81+
"TablesPageShowTopPaginationIntro": "设置 <code>ShowTopPagination</code> 为 <code>true</code> 是顶端显示分页组件,可通过 <code>IsAutoScrollTopWhenClickPage</code> 控制是否翻页后自动滚动到顶端,默认值为 <code>false</code> 保持滚动条位置"
8282
},
8383
"BootstrapBlazor.Server.Components.Samples.Table.TablesFixedColumn": {
8484
"TablesFixedColumnTitle": "固定列功能",

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.6.4-beta06</Version>
4+
<Version>9.6.4</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public partial class Table<TItem>
148148
}
149149
};
150150

151+
private bool _shouldScrollTop = false;
151152
/// <summary>
152153
/// 点击页码调用此方法
153154
/// </summary>
@@ -169,6 +170,12 @@ protected async Task OnPageLinkClick(int pageIndex)
169170

170171
// 通知 SelectedRow 双向绑定集合改变
171172
await OnSelectedRowsChanged();
173+
174+
// 通知 UI 滚动到顶端
175+
if (IsAutoScrollTopWhenClickPage)
176+
{
177+
_shouldScrollTop = true;
178+
}
172179
}
173180
}
174181

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,12 @@ public async Task ExpandDetailRow(TItem item)
759759
[Parameter]
760760
public Func<List<TItem>, bool>? DisableEditButtonCallback { get; set; }
761761

762+
/// <summary>
763+
/// 获得/设置 翻页时是否自动滚动到顶部 默认 false
764+
/// </summary>
765+
[Parameter]
766+
public bool IsAutoScrollTopWhenClickPage { get; set; }
767+
762768
[CascadingParameter]
763769
private ContextMenuZone? ContextMenuZone { get; set; }
764770

@@ -1012,6 +1018,13 @@ protected override async Task OnAfterRenderAsync(bool firstRender)
10121018
if (_isFilterTrigger)
10131019
{
10141020
_isFilterTrigger = false;
1021+
_shouldScrollTop = false;
1022+
await InvokeVoidAsync("scrollTo", Id);
1023+
}
1024+
1025+
if(_shouldScrollTop)
1026+
{
1027+
_shouldScrollTop = false;
10151028
await InvokeVoidAsync("scrollTo", Id);
10161029
}
10171030

@@ -1055,7 +1068,7 @@ private async Task OnTableRenderAsync(bool firstRender)
10551068
},
10561069
new
10571070
{
1058-
Key = "align-left",
1071+
Key = "align-right",
10591072
Icon = "fa-solid fa-align-right",
10601073
Text = Localizer["AlignRightText"].Value,
10611074
Tooltip = Localizer["AlignRightTooltipText"].Value

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,12 +199,16 @@ export function scroll(id, align, options = { behavior: 'smooth' }) {
199199
}
200200
}
201201

202-
export function scrollTo(id, x = 0, y = 0, options = { behavior: 'smooth' }) {
202+
export function scrollTo(id) {
203203
const element = document.getElementById(id);
204204
if (element) {
205205
const scroll = element.querySelector('.scroll');
206206
if (scroll) {
207-
scroll.scrollTo(x, y, options);
207+
scroll.scrollTo({
208+
top: 0,
209+
left: 0,
210+
behavior: "smooth",
211+
});
208212
}
209213
}
210214
}

test/UnitTest/Components/TableTest.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,6 +980,7 @@ public async Task PageItemsSource_Ok()
980980
pb.Add(a => a.RenderMode, TableRenderMode.Table);
981981
pb.Add(a => a.PageItemsSource, [2, 4, 8]);
982982
pb.Add(a => a.IsPagination, true);
983+
pb.Add(a => a.IsAutoScrollTopWhenClickPage, true);
983984
pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer));
984985
pb.Add(a => a.TableColumns, foo => builder =>
985986
{
@@ -991,8 +992,9 @@ public async Task PageItemsSource_Ok()
991992
});
992993
});
993994

994-
var pager = cut.FindComponent<Pagination>();
995-
await cut.InvokeAsync(() => pager.Instance.OnPageLinkClick!.Invoke(2));
995+
var items = cut.FindAll(".page-link");
996+
await cut.InvokeAsync(() => items[2].Click());
997+
996998
var activePage = cut.Find(".page-item.active");
997999
Assert.Equal("2", activePage.TextContent);
9981000
}
@@ -1058,7 +1060,7 @@ Task<QueryData<Foo>> MockOnQueryAsync(QueryPageOptions options)
10581060
return Task.FromResult(new QueryData<Foo>()
10591061
{
10601062
Items = items,
1061-
TotalCount = items.Count(),
1063+
TotalCount = 80,
10621064
IsAdvanceSearch = true,
10631065
IsFiltered = true,
10641066
IsSearch = true,

0 commit comments

Comments
 (0)