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

[Solved] Recursively populate TreeView children

4 Answers 226 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.
Tx3
Top achievements
Rank 1
Tx3 asked on 10 May 2010, 02:04 PM
Hi!

Is it possible to generate TreeView recursively? I have items n amount both horizontally and vertically. All the examples I have seen have only 1 or 2 levels.

I have following structure:

{Main level item #1}
          {Depth 1.. Child item #1}
                   {Depth n.. Child item #1}
                   {Depth n.. Child item #n}
                ....
          {Depth 1.. Child item #n}
          ....
{Main level item #n}

-Tatu

4 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 10 May 2010, 02:32 PM
Hi Tx3,

What is the class which you are trying to bind the TreeView to? Pasting it here would help us provide specific directions.

Regards,
Atanas Korchev
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
Tx3
Top achievements
Rank 1
answered on 11 May 2010, 07:22 AM

Sure, here are my classes.

Small explanation: The first level is called ProductBranch and under those there are x amount of ProductVersionLeaf instances in the List. Each of ProductVersionLeaf instances can have x amount of ProductVersionLeaf instances and so on..

 


 

    public class ProductViewModel  
    {  
        public class ProductVersionLeaf : BaseTreeViewItem  
        {  
            public string Version { getset; }  
            public string StatusText { getset; }  
            public Common.Enums.Status Status { getset; }  
            public string Label { getset; }  
            public Guid ProductId { getset; }  
            public bool IsLast { getset; }  
            public List<ProductVersionLeaf> SubProductVersions = new List<ProductVersionLeaf>();  
 
            public ProductVersionLeaf()  
            {  
                this.SubProductVersions = new List<ProductVersionLeaf>();  
            }  
        }  
 
        public class ProductBranch : BaseTreeViewItem  
        {  
            public List<ProductVersionLeaf> ProductVersions = new List<ProductVersionLeaf>();  
 
            public ProductBranch()  
            {  
                this.ProductVersions = new List<ProductVersionLeaf>();  
            }  
        }  
 
        public List<ProductBranch> Products = new List<ProductBranch>();               
 
        public ProductViewModel()  
        {  
            this.Products = new List<ProductBranch>();  
        }  
    } 

If my data structure is not suitable then I can change it if something else has better support in the TreeView.

0
Atanas Korchev
Telerik team
answered on 11 May 2010, 08:06 AM
Hi Tx3,

I think the binding to model example will help you. You need to modify the sample code to create mappings for your types (ProductVersionLeaf and ProductBranch).

Best wishes,
Atanas Korchev
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
Tx3
Top achievements
Rank 1
answered on 11 May 2010, 08:37 AM

I think was missing that last .Children call. Here is the working code for my application. In the example TreeView has only main level (for example Beverages) and children (like Chai), so there is no need for the Children.

Atanas, thank you for guiding me to the correct direction!

    <%= Html.Telerik().TreeView()  
        .Name("TreeView")  
        .BindTo(Model.Products, mappings =>   
        {  
            // product level  
            mappings.For<ProductViewModel.ProductBranch>(binding => binding                                  
                    .ItemDataBound((item, product) => 
                    {  
                        item.Selected = product.IsSelected;  
                        item.Expanded = product.IsExpanded;                      
                        item.Encoded = false;  
                        item.Text = ProductHelper.GetProductContent(product);  
                    })  
                    .Children(product => product.ProductVersions));  
            // product version  
            mappings.For<ProductViewModel.ProductVersionLeaf>(binding => binding  
                    .ItemDataBound((item, productVersion) => 
                    {  
                        item.Selected = productVersion.IsSelected;  
                        item.Encoded = false;  
                        item.Text = ProductHelper.GetProductVersionContent(productVersion, Request.Url.PathAndQuery);  
                    })  
                    .Children(productVersion => productVersion.SubProductVersions));  
        }).HtmlAttributes(new { id = "myGridControl" })%> 

-Tatu
Tags
TreeView
Asked by
Tx3
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Tx3
Top achievements
Rank 1
Share this question
or