Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,12 @@ private Task<QueryData<Foo>> OnSearchModelQueryAsync(QueryPageOptions options)
private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
{
// 使用内置扩展方法 ToFilter 获得过滤条件
// 目前 ToFilterFunc 无法解决大小写敏感问题
// 解决大小写敏感问题使用参数 StringComparison.OrdinalIgnoreCase
// 注意 EFCore 不支持 StringComparison.OrdinalIgnoreCase 需要使用 EF.Functions.Like 进行模糊搜索
var items = Items.Where(options.ToFilterFunc<Foo>());
if (!string.IsNullOrEmpty(options.SearchText))
{
// 使用 Linq 处理
// 使用 Linq 处理 处理模糊搜索 处理大小写敏感问题使用参数 StringComparison.OrdinalIgnoreCase
items = Items.Where(i =>
(!string.IsNullOrEmpty(i.Name) && i.Name.Contains(options.SearchText, StringComparison.OrdinalIgnoreCase))
|| (!string.IsNullOrEmpty(i.Address) && i.Address.Contains(options.SearchText, StringComparison.OrdinalIgnoreCase)));
Expand Down
4 changes: 2 additions & 2 deletions src/BootstrapBlazor/Components/SearchForm/SearchForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ public partial class SearchForm : IShowLabel
.AddClass($"--bb-row-label-width: {LabelWidth}px;", LabelWidth.HasValue)
.Build();

private IEnumerable<ISearchItem> UnsetGroupItems => Items.Where(i => string.IsNullOrEmpty(i.GroupName));
private IEnumerable<ISearchItem> UnsetGroupItems => Items.Where(i => string.IsNullOrEmpty(i.GroupName)).OrderBy(i => i.Order);

private IEnumerable<KeyValuePair<string, IOrderedEnumerable<ISearchItem>>> GroupItems => Items
.Where(i => !string.IsNullOrEmpty(i.GroupName))
.GroupBy(i => i.GroupName).OrderBy(i => i.Key)
.Select(i => new KeyValuePair<string, IOrderedEnumerable<ISearchItem>>(i.First().GroupName!, i.OrderBy(x => x.Order)));
.Select(i => new KeyValuePair<string, IOrderedEnumerable<ISearchItem>>(i.First().GroupName!, i.OrderBy(x => x.GroupOrder)));

/// <summary>
/// <inheritdoc/>
Expand Down
19 changes: 18 additions & 1 deletion src/BootstrapBlazor/Components/Table/Table.razor.Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,11 +182,28 @@ private IEnumerable<ISearchItem> SearchFormItems
NumberEndValueLabelText = SearchFormLocalizer[nameof(Components.SearchFormLocalizerOptions.NumberEndValueLabelText)]
};
}
_searchItems ??= SearchItems ?? GetSearchColumns().Select(i => i.ParseSearchItem(SearchFormLocalizerOptions.Value)).ToList();
_searchItems ??= GetSearchItems(SearchFormLocalizerOptions.Value);
return _searchItems;
}
}

private IEnumerable<ISearchItem> GetSearchItems(SearchFormLocalizerOptions options)
{
if (SearchItems != null)
{
// TODO: 增加内部创建默认 ISearchItemMetaData 逻辑,减少用户使用成本
//foreach (var item in SearchItems)
//{
// // 创建默认 ISearchItemMetaData
// item.MetaData ??= column.BuildSearchMetaData(options);
//}

return SearchItems;
}

return GetSearchColumns().Select(i => i.ParseSearchItem(options)).ToList();
}

private Task OnSearchFormFilterChanged(FilterKeyValueAction action)
{
_searchFilter = action;
Expand Down
1 change: 1 addition & 0 deletions src/BootstrapBlazor/Extensions/ITableColumnExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ private static void CopyValue(this ITableColumn col, ITableColumn dest)
if (col.IsRequiredWhenAdd.HasValue) dest.IsRequiredWhenAdd = col.IsRequiredWhenAdd;
if (col.IsRequiredWhenEdit.HasValue) dest.IsRequiredWhenEdit = col.IsRequiredWhenEdit;
if (col.IgnoreWhenExport.HasValue) dest.IgnoreWhenExport = col.IgnoreWhenExport;
if (col.SearchFormItemMetaData != null) dest.SearchFormItemMetaData = col.SearchFormItemMetaData;
}

/// <summary>
Expand Down
3 changes: 3 additions & 0 deletions test/UnitTest/Attributes/AutoGenerateClassTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ public void AutoGenerateColumn_Ok()
attrInterface.IsRequiredWhenEdit = true;
Assert.True(attrInterface.IsRequiredWhenEdit);

attrInterface.SearchFormItemMetaData = new StringSearchMetaData();
Assert.NotNull(attrInterface.SearchFormItemMetaData);

var attrEditor = (IEditorItem)attr;
attrEditor.Items = null;
Assert.Null(attrEditor.Items);
Expand Down
1 change: 1 addition & 0 deletions test/UnitTest/Components/InternalTableColumnTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void InternalTableColumn_Ok()
SetValue("IsRequiredWhenEdit", true);
SetValue("LookupService", null);
SetValue("IgnoreWhenExport", true);
SetValue("SearchFormItemMetaData", new StringSearchMetaData());

void SetValue(string propertyName, object? val) => type!.GetProperty(propertyName)!.SetValue(instance, val);
}
Expand Down
14 changes: 13 additions & 1 deletion test/UnitTest/Components/SearchFormTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public async Task Filter_Ok()
await stringSearchMetaData.ValueChangedHandler("test1");
Assert.Single(filterKeyValueAction.Filters);
Assert.Equal("test1", filterKeyValueAction.Filters[0].FieldValue);

var searchForm = cut.Instance;
Assert.NotNull(searchForm.Filter);
}

[Fact]
Expand All @@ -73,7 +76,9 @@ public void LabelAlign_Ok()
{
new SearchItem(nameof(Foo.Name), typeof(string), "Name")
{
Text = "Name-Updated",
GroupName = "Group1",
GroupOrder = 1,
MetaData = stringSearchMetaData
},
new SearchItem(nameof(Foo.Address), typeof(string), "Address")
Expand All @@ -93,7 +98,7 @@ public void LabelAlign_Ok()
}

[Fact]
public void ShowUnsetGroupItemsOnTop_Ok()
public void Group_Ok()
{
var cut = Context.Render<SearchForm>(pb =>
{
Expand All @@ -103,6 +108,7 @@ public void ShowUnsetGroupItemsOnTop_Ok()
new SearchItem(nameof(Foo.Name), typeof(string), "Name")
{
GroupName = "Group1",
GroupOrder= 1,
MetaData = new StringSearchMetaData()
},
new SearchItem(nameof(Foo.Address), typeof(string), "Address")
Expand All @@ -111,6 +117,12 @@ public void ShowUnsetGroupItemsOnTop_Ok()
}
});
});

// 查找标签一共有两个第一个应该是 Address 第二个应该是 Name
var labels = cut.FindAll(".bb-search-form label");
Assert.Equal(2, labels.Count);
Assert.Equal("Address", labels[0].TextContent);
Assert.Equal("Name", labels[1].TextContent);
}

[Fact]
Expand Down
46 changes: 46 additions & 0 deletions test/UnitTest/Components/TableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,52 @@ public async Task UseSearchForm_Ok()
await cut.InvokeAsync(() => table.Instance.QueryAsync());
}

[Fact]
public async Task SearchItems_Ok()
{
var localizer = Context.Services.GetRequiredService<IStringLocalizer<Foo>>();
var cut = Context.Render<BootstrapBlazorRoot>(pb =>
{
pb.AddChildContent<Table<Foo>>(pb =>
{
pb.Add(a => a.RenderMode, TableRenderMode.Table);
pb.Add(a => a.ShowToolbar, true);
pb.Add(a => a.ShowSearch, true);
pb.Add(a => a.UseSearchForm, true);
pb.Add(a => a.SearchItems, new List<SearchItem>()
{
new SearchItem("Name", typeof(string), "名称") { MetaData = new StringSearchMetaData() }
});
pb.Add(a => a.SearchMode, SearchMode.Top);
pb.Add(a => a.OnQueryAsync, OnQueryAsync(localizer));
pb.Add(a => a.TableColumns, foo => builder =>
{
builder.OpenComponent<TableColumn<Foo, string>>(0);
builder.AddAttribute(1, "Field", "");
builder.AddAttribute(2, "FieldExpression", Utility.GenerateValueExpression(foo, "Name", typeof(string)));
builder.CloseComponent();
});
});
});

cut.Contains("bb-editor bb-search-form");

// 触发 Filter
var searchForm = cut.FindComponent<SearchForm>();
Assert.NotNull(searchForm);

var input = searchForm.FindComponent<BootstrapInput<string>>();
Assert.NotNull(input);

var cb = input.Instance.OnValueChanged;
Assert.NotNull(cb);
await cb("test");

var table = cut.FindComponent<Table<Foo>>();
Assert.NotNull(table);
await cut.InvokeAsync(() => table.Instance.QueryAsync());
}

[Fact]
public void ShowToolbar_Ok()
{
Expand Down
8 changes: 6 additions & 2 deletions test/UnitTest/Extensions/ITableColumnExtensionsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public void InheritValue_Ok()
Sortable = true,
TextEllipsis = true,
ShowCopyColumn = true,
Visible = false,
Visible = false
};
col.InheritValue(attr);
Assert.Equal(Alignment.Center, col.Align);
Expand Down Expand Up @@ -113,7 +113,9 @@ public void CopyValue_Ok()
Required = true,
RequiredErrorMessage = "test",
IsRequiredWhenAdd = true,
IsRequiredWhenEdit = true
IsRequiredWhenEdit = true,

SearchFormItemMetaData = new StringSearchMetaData()
};
col.CopyValue(attr);
Assert.NotNull(col.ComponentType);
Expand Down Expand Up @@ -183,6 +185,8 @@ public void CopyValue_Ok()
Assert.NotNull(col.LookupService);
Assert.Equal("test-key", col.LookupServiceKey);
Assert.Equal(true, col.LookupServiceData);

Assert.NotNull(col.SearchFormItemMetaData);
}

[Fact]
Expand Down
Loading