Skip to content

Commit 6569ed7

Browse files
committed
doc: 增加示例
1 parent 09eb49f commit 6569ed7

File tree

2 files changed

+100
-1
lines changed

2 files changed

+100
-1
lines changed

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

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,77 @@
55
<h3>@Localizer["Title"]</h3>
66
<h4>@Localizer["Description"]</h4>
77

8+
<DemoBlock Title="@Localizer["IsVirtualizeTitle"]" Introduction="@Localizer["IsVirtualizeIntro"]" Name="IsVirtualize">
9+
<section ignore>
10+
@((MarkupString)@Localizer["IsVirtualizeDesc"].Value)
11+
</section>
12+
<section ignore>
13+
<p>@((MarkupString)Localizer["SelectsVirtualizeDescription"].Value)</p>
14+
<div class="row g-3">
15+
<div class="col-12 col-sm-6">
16+
<BootstrapInputGroup>
17+
<BootstrapInputGroupLabel DisplayText="IsClearable" />
18+
<Checkbox @bind-Value="@_isClearable" />
19+
</BootstrapInputGroup>
20+
</div>
21+
</div>
22+
</section>
23+
24+
<p class="code-label">1. 使用 OnQueryAsync 作为数据源</p>
25+
<div class="row mb-3">
26+
<div class="col-12">
27+
<AutoFill @bind-Value="Model4" OnQueryAsync="OnQueryAsync" OnGetDisplayText="OnGetDisplayText" class="mb-3"
28+
IsSelectAllTextOnFocus="true" OnCustomFilter="OnCustomVirtulizeFilter"
29+
IsVirtualize="true" RowHeight="58f" IsClearable="_isClearable">
30+
<ItemTemplate>
31+
<div class="d-flex">
32+
<div>
33+
<img src="@WebsiteOption.CurrentValue.GetAvatarUrl(context.Id)" class="bb-avatar" />
34+
</div>
35+
<div class="ps-2">
36+
<div>@context.Name</div>
37+
<div class="bb-sub">@Foo.GetTitle(context.Id)</div>
38+
</div>
39+
</div>
40+
</ItemTemplate>
41+
</AutoFill>
42+
<section ignore>
43+
<EditorForm Model="@Model4" RowType="RowType.Inline" ItemsPerRow="2" />
44+
</section>
45+
</div>
46+
</div>
47+
48+
<p class="code-label">2. 使用 Items 作为数据源</p>
49+
<div class="row">
50+
<div class="col-12">
51+
<AutoFill @bind-Value="Model4" Items="Items4" OnGetDisplayText="OnGetDisplayText" class="mb-3"
52+
IsSelectAllTextOnFocus="true" OnCustomFilter="OnCustomVirtulizeFilter"
53+
IsVirtualize="true" RowHeight="58f" IsClearable="_isClearable">
54+
<ItemTemplate>
55+
<div class="d-flex">
56+
<div>
57+
<img src="@WebsiteOption.CurrentValue.GetAvatarUrl(context.Id)" class="bb-avatar" />
58+
</div>
59+
<div class="ps-2">
60+
<div>@context.Name</div>
61+
<div class="bb-sub">@Foo.GetTitle(context.Id)</div>
62+
</div>
63+
</div>
64+
</ItemTemplate>
65+
</AutoFill>
66+
<section ignore>
67+
<EditorForm Model="@Model4" RowType="RowType.Inline" ItemsPerRow="2" />
68+
</section>
69+
</div>
70+
</div>
71+
</DemoBlock>
72+
873
<DemoBlock Title="@Localizer["NormalTitle"]" Introduction="@Localizer["NormalIntro"]" Name="Normal">
974
<section ignore>
1075
@((MarkupString)@Localizer["NormalDesc"].Value)
1176
</section>
12-
<AutoFill @bind-Value="Model1" Items="Items1" IsLikeMatch="true" OnGetDisplayText="OnGetDisplayText" class="mb-3" IsSelectAllTextOnFocus="true">
77+
<AutoFill @bind-Value="Model1" Items="Items1" class="mb-3"
78+
IsLikeMatch="true" OnGetDisplayText="OnGetDisplayText" IsSelectAllTextOnFocus="true">
1379
<ItemTemplate>
1480
<div class="d-flex">
1581
<div>

src/BootstrapBlazor.Server/Components/Samples/AutoFills.razor.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@ partial class AutoFills
1515

1616
[NotNull]
1717
private Foo Model2 { get; set; } = new();
18+
1819
[NotNull]
1920
private Foo Model3 { get; set; } = new();
2021

22+
[NotNull]
23+
private Foo Model4 { get; set; } = new();
24+
2125
private static string? OnGetDisplayText(Foo? foo) => foo?.Name;
2226

2327
[NotNull]
@@ -29,10 +33,15 @@ partial class AutoFills
2933
[NotNull]
3034
private IEnumerable<Foo>? Items3 { get; set; }
3135

36+
[NotNull]
37+
private IEnumerable<Foo>? Items4 { get; set; }
38+
3239
[Inject]
3340
[NotNull]
3441
private IStringLocalizer<Foo>? LocalizerFoo { get; set; }
3542

43+
private bool _isClearable = true;
44+
3645
/// <inheritdoc/>
3746
protected override void OnInitialized()
3847
{
@@ -46,6 +55,9 @@ protected override void OnInitialized()
4655

4756
Items3 = Foo.GenerateFoo(LocalizerFoo);
4857
Model3 = Items3.First();
58+
59+
Items4 = Foo.GenerateFoo(LocalizerFoo);
60+
Model4 = Items3.First();
4961
}
5062

5163
private Task<IEnumerable<Foo>> OnCustomFilter(string searchText)
@@ -54,6 +66,27 @@ private Task<IEnumerable<Foo>> OnCustomFilter(string searchText)
5466
return Task.FromResult(items);
5567
}
5668

69+
private Task<IEnumerable<Foo>> OnCustomVirtulizeFilter(string searchText)
70+
{
71+
var items = string.IsNullOrEmpty(searchText) ? Items4 : Items4.Where(i => i.Name!.Contains(searchText));
72+
return Task.FromResult(items);
73+
}
74+
75+
private async Task<QueryData<Foo>> OnQueryAsync(VirtualizeQueryOption option)
76+
{
77+
await Task.Delay(200);
78+
var items = Foo.GenerateFoo(LocalizerFoo);
79+
if (!string.IsNullOrEmpty(option.SearchText))
80+
{
81+
items = [.. items.Where(i => i.Name!.Contains(option.SearchText, StringComparison.OrdinalIgnoreCase))];
82+
}
83+
return new QueryData<Foo>
84+
{
85+
Items = items.Skip(option.StartIndex).Take(option.Count),
86+
TotalCount = items.Count
87+
};
88+
}
89+
5790
/// <summary>
5891
/// Get property method
5992
/// </summary>

0 commit comments

Comments
 (0)