Skip to content

Commit 148775c

Browse files
authored
feat(Table): add SearchFormLocalizerOptions parameter (#7769)
* doc: 代码格式化 * refactor: 移除 UseSearchForm 条件 * feat(Table): add SearchFormLocalizerOptions parameter * refactor: 增加高级搜索生效逻辑 * doc: 更新本地化资源文件 * chore: bump version 10.4.1-beta03 * chore: bump version 10.4.1 * doc: 移除注释信息 * refactor: 移除接口默认实现 * test: 增加单元测试 * test: 更新单元测试
1 parent 83ca545 commit 148775c

File tree

14 files changed

+164
-34
lines changed

14 files changed

+164
-34
lines changed

src/BootstrapBlazor.Server/Components/Samples/Table/TablesSearch.razor

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@
165165
<div>@((MarkupString)Localizer["SearchFormDesc"].Value)</div>
166166
</section>
167167
<Table TItem="Foo"
168-
IsPagination="true" PageItemsSource="@PageItemsSource" SearchMode="SearchMode.Top"
169-
IsStriped="true" IsBordered="true"
170-
ShowSearch="true" UseSearchForm="true"
171-
ShowToolbar="true" IsMultipleSelect="true" ShowExtendButtons="true"
172-
OnQueryAsync="@OnQueryAsync" OnAddAsync="@OnAddAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync">
168+
IsPagination="true" PageItemsSource="@PageItemsSource" SearchMode="SearchMode.Top"
169+
IsStriped="true" IsBordered="true"
170+
ShowSearch="true" UseSearchForm="true"
171+
ShowToolbar="true" IsMultipleSelect="true" ShowExtendButtons="true"
172+
OnQueryAsync="@OnQueryAsync" OnAddAsync="@OnAddAsync" OnSaveAsync="@OnSaveAsync" OnDeleteAsync="@OnDeleteAsync">
173173
<TableColumns>
174174
<TableColumn @bind-Field="@context.DateTime" Width="180" Searchable="true" />
175175
<TableColumn @bind-Field="@context.Name" Searchable="true" SearchFormItemMetaData="_nameSearchFormItemMetaData" />

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5063,6 +5063,9 @@
50635063
"DisplayText4": "Display Search",
50645064
"EditModelTitle": "Edit Test Data Window",
50655065
"NamePlaceholder": "Please enter your name within 50 characters",
5066+
"SearchFormDesc": "When <code>UseSearchForm</code> is enabled and <code>SearchItems</code> is not provided, it will default to using <code>TableColumn</code> with <code>Searchable=\"true\"</code>. You can customize the metadata through the <code>SearchFormItemMetaData</code> property in <code>TableColumn</code>",
5067+
"SearchFormIntro": "Enable the search form feature by setting <code>UseSearchForm=\"true\"</code>, and configure the search items within the form using <code>SearchItems</code>, suitable for scenarios with custom complex search conditions",
5068+
"SearchFormTitle": "Search Form",
50665069
"SearchTableGroupBoxText": "Search Criteria",
50675070
"SearchTableIntro": "Set <code>ShowSearch</code> to display the query component, customize the search UI by setting the <code>SearchTemplate</code> template",
50685071
"SearchTableLi1": "Enable no data display function by setting <code>ShowEmpty=\"true\"</code>",
@@ -5076,10 +5079,7 @@
50765079
"SelectedItemValue1": "Name1",
50775080
"SelectedItemValue2": "Name2",
50785081
"TablesSearchDesc": "Commonly used for single table maintenance, simple addition, deletion, modification, search, sorting, filtering, search and other common functions can be realized through attribute configuration, and very complex can be realized through the advanced usage of <code>Template</code> business needs functions",
5079-
"TablesSearchTitle": "Table Search",
5080-
"SearchFormTitle": "Search Form",
5081-
"SearchFormIntro": "Enable the search form feature by setting <code>UseSearchForm=\"true\"</code>, and configure the search items within the form using <code>SearchItems</code>, suitable for scenarios with custom complex search conditions",
5082-
"SearchFormDesc": "When <code>UseSearchForm</code> is enabled and <code>SearchItems</code> is not provided, it will default to using <code>TableColumn</code> with <code>Searchable=\"true\"</code>. You can customize the metadata through the <code>SearchFormItemMetaData</code> property in <code>TableColumn</code>"
5082+
"TablesSearchTitle": "Table Search"
50835083
},
50845084
"BootstrapBlazor.Server.Components.Samples.Table.TablesSelection": {
50855085
"TablesSelectionCountText": "Count:{0}",

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5063,6 +5063,9 @@
50635063
"DisplayText4": "显示搜索",
50645064
"EditModelTitle": "编辑测试数据窗口",
50655065
"NamePlaceholder": "请输入姓名,50字以内",
5066+
"SearchFormDesc": "使用 <code>UseSearchForm</code> 开启搜索表单时未提供 <code>SearchItems</code> 默认尝试使用设置 <code>Searchable=\"true\"</code> 的 <code>TableColumn</code> 进行构建,可以通过 <code>TableColumn</code> 中的 <code>SearchFormItemMetaData</code> 属性定制化元数据",
5067+
"SearchFormIntro": "通过设置 <code>UseSearchForm=\"true\"</code> 开启搜索表单功能,通过 <code>SearchItems</code> 配置搜索表单内搜索项,适用于自定义复杂搜索条件的场景",
5068+
"SearchFormTitle": "搜索表单",
50665069
"SearchTableGroupBoxText": "搜索条件",
50675070
"SearchTableIntro": "设置 <code>ShowSearch</code> 显示查询组件,通过设置 <code>SearchTemplate</code> 模板自定义搜索 UI",
50685071
"SearchTableLi1": "通过设置 <code>ShowEmpty=\"true\"</code> 开启无数据显示功能",
@@ -5076,10 +5079,7 @@
50765079
"SelectedItemValue1": "姓名1",
50775080
"SelectedItemValue2": "姓名2",
50785081
"TablesSearchDesc": "常用于单表维护,通过属性配置实现简单的增、删、改、查、排序、过滤、搜索等常用功能,通过 <code>Template</code> 的高级用法能实现非常复杂的业务需求功能",
5079-
"TablesSearchTitle": "Table 表格",
5080-
"SearchFormTitle": "搜索表单",
5081-
"SearchFormIntro": "通过设置 <code>UseSearchForm=\"true\"</code> 开启搜索表单功能,通过 <code>SearchItems</code> 配置搜索表单内搜索项,适用于自定义复杂搜索条件的场景",
5082-
"SearchFormDesc": "使用 <code>UseSearchForm</code> 开启搜索表单时未提供 <code>SearchItems</code> 默认尝试使用设置 <code>Searchable=\"true\"</code> 的 <code>TableColumn</code> 进行构建,可以通过 <code>TableColumn</code> 中的 <code>SearchFormItemMetaData</code> 属性定制化元数据"
5082+
"TablesSearchTitle": "Table 表格"
50835083
},
50845084
"BootstrapBlazor.Server.Components.Samples.Table.TablesSelection": {
50855085
"TablesSelectionCountText": "选中的行数:{0}",

src/BootstrapBlazor/BootstrapBlazor.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Razor">
22

33
<PropertyGroup>
4-
<Version>10.4.1-beta02</Version>
4+
<Version>10.4.1</Version>
55
</PropertyGroup>
66

77
<ItemGroup>

src/BootstrapBlazor/Components/SearchForm/ISearchItem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public interface ISearchItem
6868
/// <para lang="zh">获得/设置 搜索元数据</para>
6969
/// <para lang="en">Gets or sets the search metadata</para>
7070
/// </summary>
71-
public ISearchFormItemMetaData? MetaData { get; set; }
71+
ISearchFormItemMetaData? MetaData { get; set; }
7272

7373
/// <summary>
7474
/// <para lang="zh">获得 过滤器实例</para>
@@ -78,5 +78,5 @@ public interface ISearchItem
7878
/// <para lang="zh">过滤器实例</para>
7979
/// <para lang="en">Filter instance</para>
8080
/// </returns>
81-
public FilterKeyValueAction? GetFilter() => MetaData?.GetFilter(FieldName);
81+
FilterKeyValueAction? GetFilter();
8282
}

src/BootstrapBlazor/Components/SearchForm/SearchItem.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,10 @@ public class SearchItem(string fieldName, Type fieldType, string? fieldText = nu
6363
/// <inheritdoc cref="ISearchItem.MetaData"/>
6464
/// </summary>
6565
public ISearchFormItemMetaData? MetaData { get; set; }
66+
67+
/// <summary>
68+
/// <inheritdoc/>
69+
/// </summary>
70+
/// <returns></returns>
71+
public FilterKeyValueAction? GetFilter() => MetaData?.GetFilter(FieldName);
6672
}

src/BootstrapBlazor/Components/Table/Table.razor.Localization.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ public partial class Table<TItem>
416416
[NotNull]
417417
private IStringLocalizer<Table<TItem>>? Localizer { get; set; }
418418

419+
[Inject]
420+
[NotNull]
421+
private IStringLocalizer<SearchFormLocalizerOptions>? SearchFormLocalizer { get; set; }
422+
419423
private void OnInitLocalization()
420424
{
421425
AddButtonText ??= Localizer[nameof(AddButtonText)];

src/BootstrapBlazor/Components/Table/Table.razor.Search.cs

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ public partial class Table<TItem>
9494
public bool ShowSearchButton { get; set; } = true;
9595

9696
/// <summary>
97-
/// <para lang="zh">获得/设置 是否显示高级搜索按钮,默认值为 true</para>
98-
/// <para lang="en">Gets or sets Whether to show advanced search button. Default true. <see cref="ShowSearch" /></para>
97+
/// <para lang="zh">获得/设置 是否显示高级搜索按钮,默认值为 true 设置 <see cref="SearchMode.Popup" /> 时生效</para>
98+
/// <para lang="en">Gets or sets Whether to show advanced search button. Default true. Effective when <see cref="SearchMode"/> is set to Popup</para>
9999
/// </summary>
100100
[Parameter]
101101
public bool ShowAdvancedSearch { get; set; } = true;
@@ -128,6 +128,13 @@ public partial class Table<TItem>
128128
[Parameter]
129129
public IEnumerable<ISearchItem>? SearchItems { get; set; }
130130

131+
/// <summary>
132+
/// <para lang="zh">获得/设置 搜索表单本地化配置项</para>
133+
/// <para lang="en">Gets or sets Search Form Localization Options</para>
134+
/// </summary>
135+
[Parameter]
136+
public SearchFormLocalizerOptions? SearchFormLocalizerOptions { get; set; }
137+
131138
/// <summary>
132139
/// <para lang="zh">获得/设置 每行显示组件数量 默认为 2</para>
133140
/// <para lang="en">Gets or sets Items per row. Default 2</para>
@@ -163,8 +170,19 @@ private IEnumerable<ISearchItem> SearchFormItems
163170
{
164171
get
165172
{
166-
// TODO: 增加多语言支持
167-
_searchItems ??= SearchItems ?? GetSearchColumns().Select(i => i.ParseSearchItem()).ToList();
173+
if (SearchFormLocalizerOptions is null)
174+
{
175+
SearchFormLocalizerOptions = new SearchFormLocalizerOptions()
176+
{
177+
SelectAllText = SearchFormLocalizer[nameof(Components.SearchFormLocalizerOptions.SelectAllText)],
178+
BooleanAllText = SearchFormLocalizer[nameof(Components.SearchFormLocalizerOptions.BooleanAllText)],
179+
BooleanTrueText = SearchFormLocalizer[nameof(Components.SearchFormLocalizerOptions.BooleanTrueText)],
180+
BooleanFalseText = SearchFormLocalizer[nameof(Components.SearchFormLocalizerOptions.BooleanFalseText)],
181+
NumberStartValueLabelText = SearchFormLocalizer[nameof(Components.SearchFormLocalizerOptions.NumberStartValueLabelText)],
182+
NumberEndValueLabelText = SearchFormLocalizer[nameof(Components.SearchFormLocalizerOptions.NumberEndValueLabelText)]
183+
};
184+
}
185+
_searchItems ??= SearchItems ?? GetSearchColumns().Select(i => i.ParseSearchItem(SearchFormLocalizerOptions.Value)).ToList();
168186
return _searchItems;
169187
}
170188
}
@@ -315,7 +333,7 @@ protected IEnumerable<IFilterAction> GetCustomerSearches()
315333
protected List<IFilterAction> GetAdvanceSearches()
316334
{
317335
var searches = new List<IFilterAction>();
318-
if (ShowAdvancedSearch && CustomerSearchModel == null && UseSearchForm == false)
336+
if (ShowAdvancedSearch && SearchMode == SearchMode.Popup && CustomerSearchModel == null)
319337
{
320338
var callback = GetAdvancedSearchFilterCallback ?? new Func<PropertyInfo, TItem, List<SearchFilterAction>?>((p, model) =>
321339
{

src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ public static class IEditItemExtensions
1818
/// <para lang="en">Convert ITableColumn to ISearchItem</para>
1919
/// </summary>
2020
/// <param name="column"></param>
21+
/// <param name="options"></param>
2122
/// <returns></returns>
22-
public static ISearchItem ParseSearchItem(this ITableColumn column)
23+
public static ISearchItem ParseSearchItem(this ITableColumn column, SearchFormLocalizerOptions options)
2324
{
2425
var item = new SearchItem(column.GetFieldName(), column.PropertyType, column.GetDisplayName())
2526
{
@@ -28,13 +29,13 @@ public static ISearchItem ParseSearchItem(this ITableColumn column)
2829
GroupName = column.GroupName,
2930
GroupOrder = column.GroupOrder,
3031
Order = column.Order,
31-
MetaData = column.BuildSearchMetaData()
32+
MetaData = column.BuildSearchMetaData(options)
3233
};
3334

3435
return item;
3536
}
3637

37-
private static ISearchFormItemMetaData BuildSearchMetaData(this ITableColumn column)
38+
private static ISearchFormItemMetaData BuildSearchMetaData(this ITableColumn column, SearchFormLocalizerOptions options)
3839
{
3940
// 自定义搜索项逻辑
4041
if (column.SearchFormItemMetaData is not null)
@@ -61,38 +62,34 @@ private static ISearchFormItemMetaData BuildSearchMetaData(this ITableColumn col
6162
}
6263
else if (type.IsEnum)
6364
{
64-
// TODO: 缺少本地化工作
6565
metaData = new SelectSearchMetaData()
6666
{
67-
Items = type.ToSelectList(new SelectedItem() { Value = "", Text = "全选" }),
67+
Items = type.ToSelectList(new SelectedItem() { Value = "", Text = options.SelectAllText }),
6868
};
6969
}
7070
else if (fieldType.IsNumberWithDotSeparator())
7171
{
72-
// TODO: 缺少本地化工作
7372
metaData = new NumberSearchMetaData()
7473
{
75-
StartValueLabelText = "开始数量",
76-
EndValueLabelText = "截止数量",
74+
StartValueLabelText = options.NumberStartValueLabelText,
75+
EndValueLabelText = options.NumberEndValueLabelText,
7776
ValueType = type
7877
};
7978
}
8079
else if (fieldType.IsBoolean())
8180
{
82-
// TODO: 缺少本地化工作
8381
metaData = new SelectSearchMetaData()
8482
{
8583
Items = new List<SelectedItem>()
8684
{
87-
new SelectedItem() { Value = "", Text = "全部" },
88-
new SelectedItem() { Value = "True", Text = "完成" },
89-
new SelectedItem() { Value = "False", Text = "未完成" }
85+
new SelectedItem() { Value = "", Text = options.BooleanAllText },
86+
new SelectedItem() { Value = "True", Text = options.BooleanTrueText },
87+
new SelectedItem() { Value = "False", Text = options.BooleanFalseText }
9088
}
9189
};
9290
}
9391
else if (fieldType.IsDateTime())
9492
{
95-
// TODO: 缺少默认值设置
9693
metaData = new DateTimeRangeSearchMetaData();
9794
}
9895
else

src/BootstrapBlazor/Locales/en.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,5 +407,13 @@
407407
},
408408
"BootstrapBlazor.Components.CardUpload": {
409409
"DeleteConfirmContent": "Are you sure you want to delete the current data?"
410+
},
411+
"BootstrapBlazor.Components.SearchFormLocalizerOptions": {
412+
"SelectAllText": "All",
413+
"BooleanAllText": "All",
414+
"BooleanTrueText": "True",
415+
"BooleanFalseText": "False",
416+
"NumberStartValueLabelText": "Start",
417+
"NumberEndValueLabelText": "End"
410418
}
411419
}

0 commit comments

Comments
 (0)