Skip to content

Commit 929214d

Browse files
authored
feat(ListView): support IsTriggerByPagination parameter (#5513)
* refactor: Pageable 更改为 IsPagination * feat: 支持 IsTriggerByPagination 参数 * test: 更新单元测试 * doc: 更新文档注释 * refactor: 更新 PageIndex 默认值 * test: 更新单元测试
1 parent 91eebc2 commit 929214d

File tree

4 files changed

+46
-25
lines changed

4 files changed

+46
-25
lines changed

src/BootstrapBlazor.Server/Components/Samples/ListViews.razor

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
<DemoBlock Title="@Localizer["PaginationTitle"]"
3636
Introduction="@Localizer["PaginationIntro"]"
3737
Name="Pagination">
38-
<ListView TItem="Product" Pageable="true" PageItems="4" OnQueryAsync="@OnQueryAsync" Height="620px">
38+
<ListView TItem="Product" IsPagination="true" PageItems="4" OnQueryAsync="@OnQueryAsync" Height="620px">
3939
<HeaderTemplate>
4040
<div>@Localizer["ProductListText"]</div>
4141
</HeaderTemplate>

src/BootstrapBlazor/Components/ListView/ListView.razor

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,14 @@
5555
@EmptyText
5656
}
5757
</div>
58-
@if (FooterTemplate != null || Pageable)
58+
@if (FooterTemplate != null || IsPagination)
5959
{
6060
<div class="listview-footer">
6161
@if (FooterTemplate != null)
6262
{
6363
@FooterTemplate
6464
}
65-
else if (Pageable)
65+
else if (IsPagination)
6666
{
6767
<Pagination PageCount="@PageCount" PageIndex="@_pageIndex" OnPageLinkClick="@OnPageLinkClick"></Pagination>
6868
}

src/BootstrapBlazor/Components/ListView/ListView.razor.cs

+17-7
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public partial class ListView<TItem> : BootstrapComponentBase
5151
public RenderFragment<TItem>? BodyTemplate { get; set; }
5252

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

7179
/// <summary>
7280
/// 获得/设置 分组 Lambda 表达式 默认 null
@@ -143,7 +151,7 @@ public partial class ListView<TItem> : BootstrapComponentBase
143151
/// <summary>
144152
/// 获得/设置 当前页码
145153
/// </summary>
146-
private int _pageIndex;
154+
private int _pageIndex = 1;
147155

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

182190
/// <summary>
183191
/// 查询按钮调用此方法
184192
/// </summary>
185193
/// <returns></returns>
186-
public async Task QueryAsync(int pageIndex = 1)
194+
public async Task QueryAsync(int pageIndex = 1, bool triggerByPagination = false)
187195
{
188196
_pageIndex = pageIndex;
189-
await QueryData();
197+
await QueryData(triggerByPagination);
190198
StateHasChanged();
191199
}
192200

193201
/// <summary>
194202
/// 调用 OnQuery 回调方法获得数据源
195203
/// </summary>
196-
protected async Task QueryData()
204+
protected async Task QueryData(bool triggerByPagination = false)
197205
{
198206
QueryData<TItem>? queryData = null;
199207
if (OnQueryAsync != null)
200208
{
201209
queryData = await OnQueryAsync(new QueryPageOptions()
202210
{
211+
IsPage = IsPagination,
212+
IsTriggerByPagination = triggerByPagination,
203213
PageIndex = _pageIndex,
204214
PageItems = PageItems,
205215
});

test/UnitTest/Components/ListViewTest.cs

+26-15
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ public async Task ListView_Ok()
7676
[Fact]
7777
public async Task Pageable_Ok()
7878
{
79+
var triggerByPage = false;
80+
var pageIndex = 0;
7981
var items = Enumerable.Range(1, 6).Select(i => new Product()
8082
{
8183
ImageUrl = $"images/Pic{i}.jpg",
@@ -84,26 +86,33 @@ public async Task Pageable_Ok()
8486
});
8587
var cut = Context.RenderComponent<ListView<Product>>(pb =>
8688
{
87-
pb.Add(a => a.OnQueryAsync, Query);
88-
pb.Add(a => a.Pageable, true);
89+
pb.Add(a => a.OnQueryAsync, option =>
90+
{
91+
pageIndex = option.PageIndex;
92+
triggerByPage = option.IsTriggerByPagination;
93+
return Task.FromResult(new QueryData<Product>()
94+
{
95+
Items = items,
96+
TotalCount = 6
97+
});
98+
});
99+
pb.Add(a => a.IsPagination, true);
89100
pb.Add(a => a.PageItems, 2);
90101
});
102+
Assert.False(triggerByPage);
103+
Assert.Equal(1, pageIndex);
91104

92105
var pages = cut.FindAll(".page-link");
93106
Assert.Equal(5, pages.Count);
94107
await cut.InvokeAsync(() => pages[2].Click());
95-
96-
Task<QueryData<Product>> Query(QueryPageOptions option) => Task.FromResult(new QueryData<Product>()
97-
{
98-
Items = items,
99-
TotalCount = 6
100-
});
108+
Assert.True(triggerByPage);
101109
}
102110

103111
[Fact]
104-
public void QueryAsync_Ok()
112+
public async Task QueryAsync_Ok()
105113
{
106114
bool query = false;
115+
bool page = false;
107116
var items = Enumerable.Range(1, 6).Select(i => new Product()
108117
{
109118
ImageUrl = $"images/Pic{i}.jpg",
@@ -114,6 +123,7 @@ public void QueryAsync_Ok()
114123
{
115124
pb.Add(a => a.OnQueryAsync, option =>
116125
{
126+
page = option.IsPage;
117127
query = true;
118128
var ret = new QueryData<Product>()
119129
{
@@ -122,11 +132,12 @@ public void QueryAsync_Ok()
122132
};
123133
return Task.FromResult(ret);
124134
});
125-
pb.Add(a => a.Pageable, true);
135+
pb.Add(a => a.IsPagination, true);
126136
pb.Add(a => a.PageItems, 2);
127137
});
128138
Assert.True(query);
129-
cut.InvokeAsync(() => cut.Instance.QueryAsync());
139+
Assert.True(page);
140+
await cut.InvokeAsync(() => cut.Instance.QueryAsync());
130141
}
131142

132143
[Fact]
@@ -159,7 +170,7 @@ public void Collapsible_Ok()
159170
};
160171
return Task.FromResult(ret);
161172
});
162-
pb.Add(a => a.Pageable, true);
173+
pb.Add(a => a.IsPagination, true);
163174
pb.Add(a => a.PageItems, 2);
164175
pb.Add(a => a.OnListViewItemClick, p =>
165176
{
@@ -209,7 +220,7 @@ public void IsAccordion_Ok()
209220
};
210221
return Task.FromResult(ret);
211222
});
212-
pb.Add(a => a.Pageable, true);
223+
pb.Add(a => a.IsPagination, true);
213224
pb.Add(a => a.PageItems, 2);
214225
});
215226
var collapse = cut.FindComponent<Collapse>();
@@ -246,7 +257,7 @@ public void CollapsedGroupCallback_Ok()
246257
};
247258
return Task.FromResult(ret);
248259
});
249-
pb.Add(a => a.Pageable, true);
260+
pb.Add(a => a.IsPagination, true);
250261
pb.Add(a => a.PageItems, 2);
251262
});
252263
Assert.True(callback);
@@ -281,7 +292,7 @@ public void OnCollapseChanged_Ok()
281292
expect = item;
282293
return Task.CompletedTask;
283294
});
284-
pb.Add(a => a.Pageable, true);
295+
pb.Add(a => a.IsPagination, true);
285296
pb.Add(a => a.PageItems, 2);
286297
});
287298

0 commit comments

Comments
 (0)