将父母包括在过滤后的递归清单中
目标是过滤递归列表。当用户搜索bar时,foo->栏应显示。同样,当搜索词为baz时,foo->酒吧 - >结果应包括在结果中。
我的想法是通过结果并将父(如果有)添加到结果中。但这必须以递归方式完成。使用LINQ有更简单的解决方案吗?你的想法是什么?
我尝试了什么:
游乐场: https://try.mudblazor.com/ >
main.Razor
<MudTextField
T="string"
DebounceInterval="500"
Clearable="true"
Placeholder="Search"
Margin="Margin.Dense"
Variant="Variant.Outlined"
Adornment="Adornment.Start"
AdornmentIcon="@Icons.Filled.Search"
ValueChanged="@(s => SearchTerm = s)"/>
<CascadingValue Value="Items.Where(i => (!string.IsNullOrEmpty(SearchTerm)) ? i.Name.Contains(SearchTerm) : true).ToArray()">
<ItemList Parent="0"/>
</CascadingValue>
@code {
public string? SearchTerm { get; set; } = string.Empty;
public Item[]? Items =
{
new Item { Id = 1, Name = "Foo", Parent = 0 },
new Item { Id = 2, Name = "Bar", Parent = 1 },
new Item { Id = 3, Name = "Baz", Parent = 2 },
};
}
itemlist.Razor
@foreach(var item in Items.Where(i => i.Parent == Parent))
{
@if (Items.Where(i => i.Parent == item.Id).Any())
{
<MudNavGroup Title="@item.Name">
<ItemList Parent="@item.Id"/>
</MudNavGroup>
}
else
{
<MudNavLink Href="@("/items/" + item.Id)">@item.Name</MudNavLink>
}
}
@code {
[Parameter] public int? Parent { get; set; }
[CascadingParameter] public Item[]? Items { get; set; }
}
item.cs
public class Item
{
public int Id { get; set; }
public string? Name { get; set; }
public int? Parent { get; set; }
}
The goal is to filter a recursive list. When the user searches for Bar, Foo -> Bar should be displayed. Likewise when the search term is Baz, Foo -> Bar -> Baz should be included in the results.
My idea is to go through the results and add the parent (if any) to the results. But this must be done in a recursive fashion. Is there a simpler solution using linq? What are your thoughts?
What I've tried:
Playground: https://try.mudblazor.com/snippet/GaGQYokTnQPmRIHO
Main.razor
<MudTextField
T="string"
DebounceInterval="500"
Clearable="true"
Placeholder="Search"
Margin="Margin.Dense"
Variant="Variant.Outlined"
Adornment="Adornment.Start"
AdornmentIcon="@Icons.Filled.Search"
ValueChanged="@(s => SearchTerm = s)"/>
<CascadingValue Value="Items.Where(i => (!string.IsNullOrEmpty(SearchTerm)) ? i.Name.Contains(SearchTerm) : true).ToArray()">
<ItemList Parent="0"/>
</CascadingValue>
@code {
public string? SearchTerm { get; set; } = string.Empty;
public Item[]? Items =
{
new Item { Id = 1, Name = "Foo", Parent = 0 },
new Item { Id = 2, Name = "Bar", Parent = 1 },
new Item { Id = 3, Name = "Baz", Parent = 2 },
};
}
ItemList.razor
@foreach(var item in Items.Where(i => i.Parent == Parent))
{
@if (Items.Where(i => i.Parent == item.Id).Any())
{
<MudNavGroup Title="@item.Name">
<ItemList Parent="@item.Id"/>
</MudNavGroup>
}
else
{
<MudNavLink Href="@("/items/" + item.Id)">@item.Name</MudNavLink>
}
}
@code {
[Parameter] public int? Parent { get; set; }
[CascadingParameter] public Item[]? Items { get; set; }
}
Item.cs
public class Item
{
public int Id { get; set; }
public string? Name { get; set; }
public int? Parent { get; set; }
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您可以添加函数
Filter
或在@code
中使用适当的名称,如下所示。并更新
说明在注释中。You can add function
Filter
or use appropriate name inside@code
like below. And update<CascadingValue Value="Filter(SearchTerm)">
Explanation is in comment.