Unplanned
Last Updated: 23 May 2024 13:46 by ADMIN
Maria
Created on: 05 Mar 2020 09:37
Category: UI for Blazor
Type: Feature Request
59
DropDownTree

I would like a comopnent similar to this one https://demos.telerik.com/kendo-ui/dropdowntree/index

The goal is to be able to show and select hierarchical data, because the multiselect is flat https://demos.telerik.com/blazor-ui/multiselect/overview

4 comments
ADMIN
Hristian Stefanov
Posted on: 23 May 2024 13:46

Hi Matthijs,

I'm sorry to hear that the DropDownList alternative you are currently utilizing is not entirely suitable.

I contacted our product manager and the information we can share at the moment for orientation is that the chance of including the DropDownTree component in Q4 this year is very high. The best way to stay updated with what is coming is to keep tracking our Roadmap.

Your interest in this request is highly appreciated.

Regards,
Hristian Stefanov
Progress Telerik

Stay tuned by visiting our public roadmap and feedback portal pages! Or perhaps, if you are new to our Telerik family, check out our getting started resources!
Matthijs
Posted on: 16 May 2024 14:23

This example helps, but it takes a lot of efforts to get it to work properly. The CSS classes have changed a lot, and there is a bug due to the hard-coded "id" and "for" attributes in the ItemTemplate.

But more importantly the resulting component is far from being as user-friendly as what a real Telerik DropDownTree component would be: the dropdown is not closed automatically when it loses the focus, the focus is not automatically shifted to the treeview when the dropdown opens, using the enter key does not automatically select the focused item and close the dropdown, there is no placeholder value, no clear button, etc. I can probably implement some of that, but it is a lot of work, that needs to be repeated by 57 developers so far.

On top of that, this example is not even what I need, since I am interested in the DropDownTree without the checkboxes, to select only one item. I also need the filtering feature, also a lot of work when it's not a built-in feature.

My alternative for now is to use the DropDownList with the grouping feature. However, the bug I reported makes our users extremely unhappy.

Please consider adding this component soon.

Peter
Posted on: 06 Dec 2023 15:36
Can someone update this to be more bootstrap friendly, since that's what the demos are based on in '23
ADMIN
Marin Bratanov
Posted on: 05 Mar 2020 09:38

Here is a sample workaround that uses a treeview for the data, adds checkboxes to create item selection (you can also consider adding onclick handlers too - see here), and an animation container to create a dropdown:

<div style="position:relative;">
    <span class="k-widget k-dropdown k-header" @onclick="@ToggleDropdown" style="width: 300px;">
        <span class="k-dropdown-wrap k-state-default @( DropDownShown ? "k-state-focused" : "" )">
            <span class="k-input">@GetSelectedItemsText()</span>
            <span class="k-select">
                <span class="k-icon k-i-arrow-60-down"></span>
            </span>
        </span>
    </span>
    <TelerikAnimationContainer @ref="dropdown" Width="300px" Height="400px" Class="k-popup">
        <TelerikTreeView Data="@TreeData">
            <TreeViewBindings>
                <TreeViewBinding>
                    <ItemTemplate>
                        @{
                            TreeItem itm = context as TreeItem;
                            <input type="checkbox" id="cb1" class="k-checkbox" @bind="@itm.Selected">
                            <label class="k-checkbox-label" for="cb1">@itm.Text</label>
                        }
                    </ItemTemplate>
                </TreeViewBinding>
            </TreeViewBindings>
        </TelerikTreeView>
    </TelerikAnimationContainer>
</div>

@foreach (var item in SelectedItems)
{
    <div>@item.Id</div>
}

@code {
    // consider exposing these two as parameters and maybe giving them events if you encapsulate this in a standalone component
    public IEnumerable<TreeItem> TreeData { get; set; }
    public List<TreeItem> SelectedItems { get; set; } = new List<TreeItem>();
    
    private bool DropDownShown { get; set; }
    private TelerikAnimationContainer dropdown;

    async Task ToggleDropdown()
    {
        DropDownShown = !DropDownShown;
        await dropdown.ToggleAsync();
    }

    string GetSelectedItemsText()
    {
        //clean the current selection
        SelectedItems = new List<TreeItem>();

        //every time the DOM re-renders, we re-calculate the selection
        //consider doing this only on an event such as the click handler above
        foreach (var item in TreeData)
        {
            AppendSelectedItems(item);
        }

        // collect text for the dropdown element. Consider using
        // only the first N items for better appearance
        string result = string.Join(", ", SelectedItems);

        return result;
    }

    void AppendSelectedItems(TreeItem item)
    {
        if (item.Selected)
        {
            SelectedItems.Add(item);
        }
        foreach (var childItem in item.Items)
        {
            AppendSelectedItems(childItem);
        }
    }

    
    // data generation for the treeview
    // see more at https://docs.telerik.com/blazor-ui/components/treeview/data-binding/overview
    public class TreeItem
    {
        public string Text { get; set; }
        public int Id { get; set; }
        public List<TreeItem> Items { get; set; } = new List<TreeItem>();
        public bool Expanded { get; set; }
        public bool HasChildren { get; set; }
        public bool Selected { get; set; }

        // in this sample we override ToString to easily show text in the dropdown element
        public override string ToString()
        {
            return this.Text;
        }
    }

    protected override void OnInitialized()
    {
        LoadHierarchical();
    }

    private void LoadHierarchical()
    {
        List<TreeItem> roots = new List<TreeItem>() {
            new TreeItem { Text = "Item 1", Id = 1, Expanded = true, HasChildren = true },
            new TreeItem { Text = "Item 2", Id = 2, HasChildren = true }
        };

        roots[0].Items.Add(new TreeItem
        {
            Text = "Item 1 first child",
            Id = 3

        });

        roots[0].Items.Add(new TreeItem
        {
            Text = "Item 1 second child",
            Id = 4

        });

        roots[1].Items.Add(new TreeItem
        {
            Text = "Item 2 first child",
            Id = 5

        });

        roots[1].Items.Add(new TreeItem
        {
            Text = "Item 2 second child",
            Id = 6

        });

        TreeData = roots;
    }
}

 

Regards,
Marin Bratanov
Progress Telerik

 UI for Blazor