Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ListView): support IsTriggerByPagination parameter #5513

Merged
merged 6 commits into from
Mar 4, 2025
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<DemoBlock Title="@Localizer["PaginationTitle"]"
Introduction="@Localizer["PaginationIntro"]"
Name="Pagination">
<ListView TItem="Product" Pageable="true" PageItems="4" OnQueryAsync="@OnQueryAsync" Height="620px">
<ListView TItem="Product" IsPagination="true" PageItems="4" OnQueryAsync="@OnQueryAsync" Height="620px">
<HeaderTemplate>
<div>@Localizer["ProductListText"]</div>
</HeaderTemplate>
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Components/ListView/ListView.razor
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,14 @@
@EmptyText
}
</div>
@if (FooterTemplate != null || Pageable)
@if (FooterTemplate != null || IsPagination)
{
<div class="listview-footer">
@if (FooterTemplate != null)
{
@FooterTemplate
}
else if (Pageable)
else if (IsPagination)
{
<Pagination PageCount="@PageCount" PageIndex="@_pageIndex" OnPageLinkClick="@OnPageLinkClick"></Pagination>
}
Expand Down
24 changes: 17 additions & 7 deletions src/BootstrapBlazor/Components/ListView/ListView.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public partial class ListView<TItem> : BootstrapComponentBase
public RenderFragment<TItem>? BodyTemplate { get; set; }

/// <summary>
/// 获得/设置 FooterTemplate 默认 null 未设置 设置值后 <see cref="Pageable"/> 参数不起作用,请自行实现分页功能
/// 获得/设置 FooterTemplate 默认 null 未设置 设置值后 <see cref="IsPagination"/> 参数不起作用,请自行实现分页功能
/// </summary>
[Parameter]
public RenderFragment? FooterTemplate { get; set; }
Expand All @@ -66,7 +66,15 @@ public partial class ListView<TItem> : BootstrapComponentBase
/// 获得/设置 是否分页 默认为 false 不分页 设置 <see cref="FooterTemplate"/> 时分页功能自动被禁用
/// </summary>
[Parameter]
public bool Pageable { get; set; }
[Obsolete("已弃用,请使用 IsPagination 代替。Deprecated, use IsPagination instead")]
[ExcludeFromCodeCoverage]
public bool Pageable { get => IsPagination; set => IsPagination = value; }

/// <summary>
/// 获得/设置 是否分页 默认为 false 不分页 设置 <see cref="FooterTemplate"/> 时分页功能自动被禁用
/// </summary>
[Parameter]
public bool IsPagination { get; set; }

/// <summary>
/// 获得/设置 分组 Lambda 表达式 默认 null
Expand Down Expand Up @@ -143,7 +151,7 @@ public partial class ListView<TItem> : BootstrapComponentBase
/// <summary>
/// 获得/设置 当前页码
/// </summary>
private int _pageIndex;
private int _pageIndex = 1;

/// <summary>
/// 获得/设置 数据总条目
Expand Down Expand Up @@ -177,29 +185,31 @@ protected override async Task OnParametersSetAsync()
/// 点击页码调用此方法
/// </summary>
/// <param name="pageIndex"></param>
protected Task OnPageLinkClick(int pageIndex) => QueryAsync(pageIndex);
protected Task OnPageLinkClick(int pageIndex) => QueryAsync(pageIndex, true);

/// <summary>
/// 查询按钮调用此方法
/// </summary>
/// <returns></returns>
public async Task QueryAsync(int pageIndex = 1)
public async Task QueryAsync(int pageIndex = 1, bool triggerByPagination = false)
{
_pageIndex = pageIndex;
await QueryData();
await QueryData(triggerByPagination);
StateHasChanged();
}

/// <summary>
/// 调用 OnQuery 回调方法获得数据源
/// </summary>
protected async Task QueryData()
protected async Task QueryData(bool triggerByPagination = false)
{
QueryData<TItem>? queryData = null;
if (OnQueryAsync != null)
{
queryData = await OnQueryAsync(new QueryPageOptions()
{
IsPage = IsPagination,
IsTriggerByPagination = triggerByPagination,
PageIndex = _pageIndex,
PageItems = PageItems,
});
Expand Down
41 changes: 26 additions & 15 deletions test/UnitTest/Components/ListViewTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public async Task ListView_Ok()
[Fact]
public async Task Pageable_Ok()
{
var triggerByPage = false;
var pageIndex = 0;
var items = Enumerable.Range(1, 6).Select(i => new Product()
{
ImageUrl = $"images/Pic{i}.jpg",
Expand All @@ -84,26 +86,33 @@ public async Task Pageable_Ok()
});
var cut = Context.RenderComponent<ListView<Product>>(pb =>
{
pb.Add(a => a.OnQueryAsync, Query);
pb.Add(a => a.Pageable, true);
pb.Add(a => a.OnQueryAsync, option =>
{
pageIndex = option.PageIndex;
triggerByPage = option.IsTriggerByPagination;
return Task.FromResult(new QueryData<Product>()
{
Items = items,
TotalCount = 6
});
});
pb.Add(a => a.IsPagination, true);
pb.Add(a => a.PageItems, 2);
});
Assert.False(triggerByPage);
Assert.Equal(1, pageIndex);

var pages = cut.FindAll(".page-link");
Assert.Equal(5, pages.Count);
await cut.InvokeAsync(() => pages[2].Click());

Task<QueryData<Product>> Query(QueryPageOptions option) => Task.FromResult(new QueryData<Product>()
{
Items = items,
TotalCount = 6
});
Assert.True(triggerByPage);
}

[Fact]
public void QueryAsync_Ok()
public async Task QueryAsync_Ok()
{
bool query = false;
bool page = false;
var items = Enumerable.Range(1, 6).Select(i => new Product()
{
ImageUrl = $"images/Pic{i}.jpg",
Expand All @@ -114,6 +123,7 @@ public void QueryAsync_Ok()
{
pb.Add(a => a.OnQueryAsync, option =>
{
page = option.IsPage;
query = true;
var ret = new QueryData<Product>()
{
Expand All @@ -122,11 +132,12 @@ public void QueryAsync_Ok()
};
return Task.FromResult(ret);
});
pb.Add(a => a.Pageable, true);
pb.Add(a => a.IsPagination, true);
pb.Add(a => a.PageItems, 2);
});
Assert.True(query);
cut.InvokeAsync(() => cut.Instance.QueryAsync());
Assert.True(page);
await cut.InvokeAsync(() => cut.Instance.QueryAsync());
}

[Fact]
Expand Down Expand Up @@ -159,7 +170,7 @@ public void Collapsible_Ok()
};
return Task.FromResult(ret);
});
pb.Add(a => a.Pageable, true);
pb.Add(a => a.IsPagination, true);
pb.Add(a => a.PageItems, 2);
pb.Add(a => a.OnListViewItemClick, p =>
{
Expand Down Expand Up @@ -209,7 +220,7 @@ public void IsAccordion_Ok()
};
return Task.FromResult(ret);
});
pb.Add(a => a.Pageable, true);
pb.Add(a => a.IsPagination, true);
pb.Add(a => a.PageItems, 2);
});
var collapse = cut.FindComponent<Collapse>();
Expand Down Expand Up @@ -246,7 +257,7 @@ public void CollapsedGroupCallback_Ok()
};
return Task.FromResult(ret);
});
pb.Add(a => a.Pageable, true);
pb.Add(a => a.IsPagination, true);
pb.Add(a => a.PageItems, 2);
});
Assert.True(callback);
Expand Down Expand Up @@ -281,7 +292,7 @@ public void OnCollapseChanged_Ok()
expect = item;
return Task.CompletedTask;
});
pb.Add(a => a.Pageable, true);
pb.Add(a => a.IsPagination, true);
pb.Add(a => a.PageItems, 2);
});

Expand Down