Skip to content

Commit 24729ef

Browse files
authored
doc(Select): add DefaultVirtualizeItemText parameter sample code (#7294)
* doc: 更新多语言键值拼写错误 * perf: 改用 Find 提高性能 * doc: 更新示例文档 * refactor: 更改绑定值为可为空
1 parent 76809d2 commit 24729ef

5 files changed

Lines changed: 23 additions & 19 deletions

File tree

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
@page "/select"
1+
@page "/select"
22
@inject DialogService Dialog
33
@inject IStringLocalizer<Selects> Localizer
44
@inject IOptions<WebsiteOptions> WebsiteOption
@@ -366,8 +366,8 @@
366366
Name="ConfirmSelect">
367367
<section ignore>
368368
<ul class="ul-demo">
369-
<li>@((MarkupString)Localizer["SelectConfifrmSelectDesc1"].Value)</li>
370-
<li>@((MarkupString)Localizer["SelectConfifrmSelectDesc2"].Value)</li>
369+
<li>@((MarkupString)Localizer["SelectConfirmSelectDesc1"].Value)</li>
370+
<li>@((MarkupString)Localizer["SelectConfirmSelectDesc2"].Value)</li>
371371
</ul>
372372
</section>
373373
<div class="row">
@@ -452,22 +452,24 @@
452452
<p class="code-label">1. 使用 OnQueryAsync 作为数据源</p>
453453
<div class="row mb-3">
454454
<div class="col-6">
455-
<Select IsVirtualize="true" OnQueryAsync="OnQueryAsync" @bind-Value="VirtualItem1"
455+
<Select IsVirtualize="true" OnQueryAsync="OnQueryAsync" @bind-Value="_virtualItem1"
456+
DefaultVirtualizeItemText="@VirtualItemText1"
456457
ShowSearch="_showSearch" IsClearable="_isClearable"></Select>
457458
</div>
458459
<div class="col-6">
459-
<Display TValue="string" Value="@VirtualItem1?.Text"></Display>
460+
<Display TValue="int?" Value="@_virtualItem1"></Display>
460461
</div>
461462
</div>
462463

463464
<p class="code-label">2. 使用 Items 作为数据源</p>
464465
<div class="row">
465466
<div class="col-6">
466-
<Select IsVirtualize="true" Items="VirtualItems" @bind-Value="VirtualItem2"
467+
<Select IsVirtualize="true" Items="VirtualItems" @bind-Value="_virtualItem2"
468+
DefaultVirtualizeItemText="@VirtualItemText2"
467469
ShowSearch="_showSearch" IsClearable="_isClearable"></Select>
468470
</div>
469471
<div class="col-6">
470-
<Display TValue="string" Value="@VirtualItem2?.Text"></Display>
472+
<Display TValue="int?" Value="@_virtualItem2"></Display>
471473
</div>
472474
</div>
473475
</DemoBlock>

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -28,11 +28,13 @@ public sealed partial class Selects
2828
new SelectedItem ("Shanghai", "上海")
2929
};
3030

31-
private IEnumerable<SelectedItem> VirtualItems => Foos.Select(i => new SelectedItem(i.Name!, i.Name!)).ToList();
31+
private IEnumerable<SelectedItem> VirtualItems => Foos.Select(i => new SelectedItem(i.Id.ToString(), i.Name!)).ToList();
3232

33-
private SelectedItem? VirtualItem1 { get; set; }
33+
private int? _virtualItem1 = 2;
34+
private string? VirtualItemText1 => Foos.FirstOrDefault(i => i.Id == 2)?.Name;
3435

35-
private SelectedItem? VirtualItem2 { get; set; }
36+
private int? _virtualItem2 = 3;
37+
private string? VirtualItemText2 => Foos.FirstOrDefault(i => i.Id == 3)?.Name;
3638

3739
[NotNull]
3840
private List<Foo>? Foos { get; set; }
@@ -76,7 +78,7 @@ private async Task<QueryData<SelectedItem>> OnQueryAsync(VirtualizeQueryOption o
7678
}
7779
return new QueryData<SelectedItem>
7880
{
79-
Items = items.Skip(option.StartIndex).Take(option.Count).Select(i => new SelectedItem(i.Name!, i.Name!)),
81+
Items = items.Skip(option.StartIndex).Take(option.Count).Select(i => new SelectedItem(i.Id.ToString(), i.Name!)),
8082
TotalCount = items.Count
8183
};
8284
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,8 +3030,8 @@
30303030
"SelectsShowSearchIntro": "Controls whether the search box is displayed by setting the <code>ShowSearch</code> property, which is not displayed by default <b>false</b>. You can set the <code>IsAutoClearSearchTextWhenCollapsed</code> parameter to control whether the text in the search box is automatically cleared after the drop-down box is collapsed. The default value is <b>false</b>.",
30313031
"SelectsConfirmSelectTitle": "Drop-down box with confirmation",
30323032
"SelectsConfirmSelectIntro": "Prevent the current value from changing by setting the <code>OnBeforeSelectedItemChange</code> delegate or setting the <code>ShowSwal</code> parameter to <code>true</code>.",
3033-
"SelectConfifrmSelectDesc1": "Set the <code>OnBeforeSelectedItemChange</code> callback method, and pop up a window in the callback method to confirm whether to change the value. If it returns <code>true</code>, the value will be changed, otherwise it will not be changed.",
3034-
"SelectConfifrmSelectDesc2": "Set <code>ShowSwal=\"true\"</code> and then confirm the value of the <code>SwalTitle</code> <code>SwalContent</code> parameter using the built-in popup window. In the callback method, you can confirm whether to change the value.",
3033+
"SelectConfirmSelectDesc1": "Set the <code>OnBeforeSelectedItemChange</code> callback method, and pop up a window in the callback method to confirm whether to change the value. If it returns <code>true</code>, the value will be changed, otherwise it will not be changed.",
3034+
"SelectConfirmSelectDesc2": "Set <code>ShowSwal=\"true\"</code> and then confirm the value of the <code>SwalTitle</code> <code>SwalContent</code> parameter using the built-in popup window. In the callback method, you can confirm whether to change the value.",
30353035
"SelectsTimeZoneTitle": "Timezone",
30363036
"SelectsTimeZoneIntro": "Display data of Timezone",
30373037
"SwalTitle": "The drop-down box value changes",

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3030,8 +3030,8 @@
30303030
"SelectsShowSearchIntro": "通过设置 <code>ShowSearch</code> 属性控制是否显示搜索框,默认为 <b>false</b> 不显示搜索框,可以通过设置 <code>IsAutoClearSearchTextWhenCollapsed</code> 参数控制下拉框收起后是否自动清空搜索框内文字,默认值为 <b>false</b> 不清空",
30313031
"SelectsConfirmSelectTitle": "带确认的下拉框",
30323032
"SelectsConfirmSelectIntro": "通过设置 <code>OnBeforeSelectedItemChange</code> 委托或者设置 <code>ShowSwal</code> 参数值为 <code>true</code>,阻止当前值的改变。",
3033-
"SelectConfifrmSelectDesc1": "设置 <code>OnBeforeSelectedItemChange</code> 回调方法,在回调方法内自己弹窗确认是否更改值,返回 <code>true</code> 时更改,否则不更改",
3034-
"SelectConfifrmSelectDesc2": "设置 <code>ShowSwal=\"true\"</code> 然后通过设置 <code>SwalTitle</code> <code>SwalContent</code> 参数值使用内置弹窗进行确认即可,在回调方法内自己弹窗确认是否更改值",
3033+
"SelectConfirmSelectDesc1": "设置 <code>OnBeforeSelectedItemChange</code> 回调方法,在回调方法内自己弹窗确认是否更改值,返回 <code>true</code> 时更改,否则不更改",
3034+
"SelectConfirmSelectDesc2": "设置 <code>ShowSwal=\"true\"</code> 然后通过设置 <code>SwalTitle</code> <code>SwalContent</code> 参数值使用内置弹窗进行确认即可,在回调方法内自己弹窗确认是否更改值",
30353035
"SelectsTimeZoneTitle": "时区下拉框",
30363036
"SelectsTimeZoneIntro": "下拉框展现时区数据",
30373037
"SwalTitle": "下拉框值变更",
@@ -3064,7 +3064,7 @@
30643064
"SelectsIsEditableDesc": "开启可编辑功能后,输入值如果候选项中没有时,可以通过 <code>TextConvertToValueCallback</code> 回调方法返回新值,可以通过 <code>OnInputChangedCallback</code> 回调对 <code>Items</code> 数据源进行更新,防止页面刷新后输入值丢失",
30653065
"SelectsVirtualizeTitle": "虚拟滚动",
30663066
"SelectsVirtualizeIntro": "通过设置 <code>IsVirtualize</code> 参数开启组件虚拟功能特性",
3067-
"SelectsVirtualizeDescription": "组件虚拟滚动支持两种形式通过 <code>Items</code> 或者 <code>OnQueryAsync</code> 回调方法提供数据",
3067+
"SelectsVirtualizeDescription": "组件虚拟滚动支持两种形式通过 <code>Items</code> 或者 <code>OnQueryAsync</code> 回调方法提供数据。如果数据源使用 <code>OnQueryAsync</code> 回调获得时只有当下拉框展开时才会触发,,如果数据源使用 <code>Items</code> 时,由于性能问题(有些开发会把几百万条数据给 Items)内部并没有进行查找选中项,所以需要设置 <code>DefaultVirtualizeItemText</code> 值应对首次加载时不知道如何显示问题",
30683068
"SelectsGenericTitle": "泛型支持",
30693069
"SelectsGenericIntro": "数据源 <code>Items</code> 使用 <code>SelectedItem&lt;TValue&gt;</code> 时即可支持泛型",
30703070
"SelectsGenericDesc": "<p>请参考 <a href=\"https://github.com/dotnetcore/BootstrapBlazor/issues/4497?wt.mc_id=DT-MVP-5004174\" target=\"_blank\">设计思路</a> 理解此功能。本例中通过选择下拉框选项,得到的值为 <code>Foo</code> 实例,右侧文本框内显示值为 <code>Foo</code> 属性 <code>Address</code> 值</p><p>本例中设置 <code>IsEditable=\"true\"</code> 以及 <code>TextConvertToValueCallback</code> 参数,录入原数据源中不存在的 <code>Foo</code> 时,在 <code></code> 回调方法中添加新 <code>Foo</code> 实例到数据源中</p>",

src/BootstrapBlazor/Components/Select/Select.razor.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the Apache 2.0 License
33
// See the LICENSE file in the project root for more information.
44
// Maintainer: Argo Zhang(argo@live.ca) Website: https://www.blazor.zone
@@ -500,7 +500,7 @@ private async Task OnChange(ChangeEventArgs args)
500500
var item = GetItemWithEnumValue()
501501
?? Rows.Find(i => i.Value == CurrentValueAsString)
502502
?? Rows.Find(i => i.Active)
503-
?? Rows.FirstOrDefault(i => !i.IsDisabled);
503+
?? Rows.Find(i => !i.IsDisabled);
504504
return item;
505505
}
506506
}

0 commit comments

Comments
 (0)