将父母包括在过滤后的递归清单中

发布于 01-18 22:47 字数 2010 浏览 0 评论 0原文

目标是过滤递归列表。当用户搜索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.

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

泅渡2025-01-25 22:47:56

您可以添加函数 Filter 或在 @code 中使用适当的名称,如下所示。并更新 说明在注释中。

<CascadingValue Value="Filter(SearchTerm)">
    <ItemList Parent="0"/>
</CascadingValue>

public Item[] Filter(string SearchTerm)
{
    // Get filtered list. Return all values when SearchTerm is null or empty else return matching values only
    var a = Items.Where(i => string.IsNullOrEmpty(SearchTerm) || i.Name.Contains(SearchTerm)).ToList();

    // NOTE : Add children first then only search for parent
    // Use condition to find all children hierarchy of any object which is not exist in the list
    while (Items.Any(i => a.Any(x => x.Id == i.Parent) && !a.Any(x => x.Id == i.Id)))
    {
        // Add complete children hierarchy for selected iterms
        a.AddRange(Items.Where(i => a.Any(x => x.Id == i.Parent) && !a.Any(x => x.Id == i.Id)));
    }
    
    // Use condition to find parent of any object which is not exist in the list
    while (Items.Any(i => a.Any(x => x.Parent == i.Id) && !a.Any(x => x.Id == i.Id)))
    {
        // Add all parents who are not already added in list
        a.AddRange(Items.Where(i => a.Any(x => x.Parent == i.Id) && !a.Any(x => x.Id == i.Id)));
    }

    // return final object
    return a.ToArray();
}

You can add function Filter or use appropriate name inside @code like below. And update <CascadingValue Value="Filter(SearchTerm)"> Explanation is in comment.

<CascadingValue Value="Filter(SearchTerm)">
    <ItemList Parent="0"/>
</CascadingValue>

public Item[] Filter(string SearchTerm)
{
    // Get filtered list. Return all values when SearchTerm is null or empty else return matching values only
    var a = Items.Where(i => string.IsNullOrEmpty(SearchTerm) || i.Name.Contains(SearchTerm)).ToList();

    // NOTE : Add children first then only search for parent
    // Use condition to find all children hierarchy of any object which is not exist in the list
    while (Items.Any(i => a.Any(x => x.Id == i.Parent) && !a.Any(x => x.Id == i.Id)))
    {
        // Add complete children hierarchy for selected iterms
        a.AddRange(Items.Where(i => a.Any(x => x.Id == i.Parent) && !a.Any(x => x.Id == i.Id)));
    }
    
    // Use condition to find parent of any object which is not exist in the list
    while (Items.Any(i => a.Any(x => x.Parent == i.Id) && !a.Any(x => x.Id == i.Id)))
    {
        // Add all parents who are not already added in list
        a.AddRange(Items.Where(i => a.Any(x => x.Parent == i.Id) && !a.Any(x => x.Id == i.Id)));
    }

    // return final object
    return a.ToArray();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文