This is a migrated thread and some comments may be shown as answers.

[Solved] How do I expand all parent nodes of the selected node

2 Answers 87 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Alastair Eddie
Top achievements
Rank 1
Alastair Eddie asked on 29 Sep 2010, 07:55 AM
Hi,

I'm persiting treeview state in a cookie.

Rather than store the expanded/collapsed state for every node, I'd rather just store the currently selected node. My controller then passes the SelectedNode to the view containing the tree and the item is set as follows...

item.Selected = ((string)ViewData["SelectedNode"] == item.Value);
My question is, how can I get the treeview to expand all the parent nodes of an item that I set as Selected during the bind?

Thanks,
Alastair Eddie

2 Answers, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 29 Sep 2010, 09:35 AM
Hello Alastair,

You can try this approach:
<asp:content contentplaceholderid="MainContent" runat="server">
 
<script runat="server">
    public void Expander(TreeViewItem item)
    {
        if(item.Parent != null)
        {
            item.Expanded = true;
            Expander(item.Parent);
        }
    }
</script>
 
<%= Html.Telerik().TreeView()
        .Name("TreeView")
        .BindTo(Model, mappings =>
        {
            mappings.For<Category>(binding => binding
                    .ItemDataBound((item, category) =>
                    {
                        item.Text = category.CategoryName;
                    })
                    .Children(category => category.Products));
             
            mappings.For<Product>(binding => binding
                    .ItemDataBound((item, product) =>
                    {
                        item.Text = product.ProductName;
                        Expander(item);
                    }));
        })
%>
 
</asp:content>

Look at the Expander method. You can use it in order to achieve your goal.

Greetings,
Georgi Krustev
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Alastair Eddie
Top achievements
Rank 1
answered on 29 Sep 2010, 02:05 PM
Bravo - works a treat.

Thanks,
Alastair
Tags
TreeView
Asked by
Alastair Eddie
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Alastair Eddie
Top achievements
Rank 1
Share this question
or