Skip to content

Commit 786e73c

Browse files
ArgoZhangcairabbit
andauthored
feat(ListView): add EmptyText parameter (#5287)
* refactor: 过滤掉超时异常 * refactor: 更新代码 * test: 更新单元测试 * refactor: 增加 LoadModuleByName 扩展方法 * refactor: 使用新的扩展方法减少代码 * feat: 增加 EmptyTemplate 参数 * test: 增加单元测试 * refactor: 删除合并导致的冗余代码 --------- Co-authored-by: caics <[email protected]>
1 parent 350b0e3 commit 786e73c

File tree

3 files changed

+79
-30
lines changed

3 files changed

+79
-30
lines changed

src/BootstrapBlazor/Components/ListView/ListView.razor

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,43 +10,54 @@
1010
</div>
1111
}
1212
<div class="@BodyClassString">
13-
@if (BodyTemplate != null)
13+
@if (Rows.Count > 0)
1414
{
15-
if (GroupName == null)
15+
if (BodyTemplate != null)
1616
{
17-
foreach (var item in Rows)
17+
if (GroupName == null)
1818
{
19-
<div class="listview-item" @onclick="@(e => OnClick(item))">
20-
@BodyTemplate(item)
21-
</div>
19+
foreach (var item in Rows)
20+
{
21+
<div class="listview-item" @onclick="@(e => OnClick(item))">
22+
@BodyTemplate(item)
23+
</div>
24+
}
2225
}
23-
}
24-
else if (Collapsible)
25-
{
26-
<Collapse IsAccordion="IsAccordion" OnCollapseChanged="OnCollapseChanged!">
27-
<CollapseItems>
28-
@RenderCollapsibleItems(GroupName)
29-
</CollapseItems>
30-
</Collapse>
31-
}
32-
else
33-
{
34-
foreach (var key in GetGroupItems(GroupName))
26+
else if (Collapsible)
3527
{
36-
<div @key="@key.GroupName" class="accordion-item">
37-
<div class="accordion-header">@key.GroupName</div>
38-
<div class="accordion-body">
39-
@foreach (var item in key.Items)
40-
{
41-
<div class="listview-item" @onclick="@(e => OnClick(item))">
42-
@BodyTemplate(item)
43-
</div>
44-
}
28+
<Collapse IsAccordion="IsAccordion" OnCollapseChanged="OnCollapseChanged!">
29+
<CollapseItems>
30+
@RenderCollapsibleItems(GroupName)
31+
</CollapseItems>
32+
</Collapse>
33+
}
34+
else
35+
{
36+
foreach (var key in GetGroupItems(GroupName))
37+
{
38+
<div @key="@key.GroupName" class="accordion-item">
39+
<div class="accordion-header">@key.GroupName</div>
40+
<div class="accordion-body">
41+
@foreach (var item in key.Items)
42+
{
43+
<div class="listview-item" @onclick="@(e => OnClick(item))">
44+
@BodyTemplate(item)
45+
</div>
46+
}
47+
</div>
4548
</div>
46-
</div>
49+
}
4750
}
4851
}
4952
}
53+
else if (EmptyTemplate != null)
54+
{
55+
@EmptyTemplate
56+
}
57+
else
58+
{
59+
@EmptyText
60+
}
5061
</div>
5162
@if (FooterTemplate != null || Pageable)
5263
{

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,18 @@ public partial class ListView<TItem> : BootstrapComponentBase
128128
[Parameter]
129129
public string? Height { get; set; }
130130

131+
/// <summary>
132+
/// 获得/设置 无数据时模板 默认 null 未设置
133+
/// </summary>
134+
[Parameter]
135+
public RenderFragment? EmptyTemplate { get; set; }
136+
137+
/// <summary>
138+
/// 获得/设置 无数据时显示文字 默认 null 未设置使用资源文件设置文字
139+
/// </summary>
140+
[Parameter]
141+
public string? EmptyText { get; set; }
142+
131143
/// <summary>
132144
/// 获得/设置 当前页码
133145
/// </summary>

test/UnitTest/Components/ListViewTest.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Items_Ok()
1414
{
1515
pb.Add(a => a.BodyTemplate, p => builder => builder.AddContent(0, $"{p.ImageUrl}-{p.Description}-{p.Category}"));
1616
});
17-
cut.Markup.Contains("listview-body");
17+
cut.Contains("listview-body");
1818
}
1919

2020
[Fact]
@@ -24,7 +24,7 @@ public void Height_Ok()
2424
{
2525
pb.Add(a => a.Height, "50vh");
2626
});
27-
cut.Markup.Contains("style=\"height: 50vh;\"");
27+
cut.Contains("style=\"height: 50vh;\"");
2828
}
2929

3030
[Fact]
@@ -293,6 +293,32 @@ public void OnCollapseChanged_Ok()
293293
});
294294
}
295295

296+
[Fact]
297+
public void EmptyTemplate_Ok()
298+
{
299+
var cut = Context.RenderComponent<ListView<Product>>(pb =>
300+
{
301+
pb.Add(a => a.OnQueryAsync, option =>
302+
{
303+
var ret = new QueryData<Product>()
304+
{
305+
Items = [],
306+
TotalCount = 0
307+
};
308+
return Task.FromResult(ret);
309+
});
310+
pb.Add(a => a.EmptyTemplate, builder => builder.AddContent(0, "empty-template"));
311+
});
312+
cut.Contains("empty-template");
313+
314+
cut.SetParametersAndRender(pb =>
315+
{
316+
pb.Add<RenderFragment?>(a => a.EmptyTemplate, null);
317+
pb.Add(a => a.EmptyText, "text-empty");
318+
});
319+
cut.Contains("text-empty");
320+
}
321+
296322
private class Product
297323
{
298324
public string? ImageUrl { get; set; }

0 commit comments

Comments
 (0)