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

[Solved] Tree population with different app architecture

1 Answer 56 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.
Ales Tepina
Top achievements
Rank 1
Ales Tepina asked on 16 Mar 2010, 11:29 AM
Hello!

I'm trying to use the TreeView MVC component but am unable to populate children as i'm using different application architecture in my case.

public class Node 
{ 
    public virtual Node ParentNode { get; set; } 
    public virtual int NodeId { get; set; } 
     
} 
 
public class NodeRepository 
{ 
    private static readonly List<Node> InternalStorage = new List<Node>(); 
     
    //where i have methods 
     
    public void Add(Node node) { // logic here } 
    public Node GetRoot() { // logic here } 
    public IEnumerable<Node> GetChildren(Node node) { // logic here } 
    public IEnumerable<Node> GetSiblings(Node node) { // logic here } 
    public Node GetById(int nodeId) { // logic here } 
    public IEnumerable<Node> GetAncestors(Node node) { // logic here } 
    public void Remove(Node node) { // logic here } 
} 
     

I'm wondering...is it possible to populate TreeView with this kind of application architecture? 
I can send the data from controller to view via:
ViewData.Model = _nodeRepository.GetChildren(rootNode); 
return View(); 

And then in the view i can populate the first level of nodes, but am unable to do that for all the children since my nodes don't have direct reference to descendants.

 <%= Html.Telerik().TreeView() 
        .Name("TreeView") 
        .BindTo(Model, mappings =>  
        { 
            mappings.For<Node>(binding => binding 
                    .ItemDataBound((item, node) => 
                    { 
                        item.Text = node.Title; 
                    }) 
 
                    // Below this, does not work 
                    //.Children(node => node.Children)); 
 
        }) 
%> 

Is it possible to do that with my architecture of the application? If not, do you have a specification of TreeNode so i could see a different implementation and the requirements to make it work with your TreeView.

Thank you for your answer.

1 Answer, 1 is accepted

Sort by
0
Georgi Krustev
Telerik team
answered on 17 Mar 2010, 10:29 AM
Hello Ales Tepina,

After quick review the code snippets which you send to us, I noticed that GetChildren(...) method returns flaat collection of Node. This means that you can populate only one level of TreeViewItem objects using one of our BindTo methods.

Use this BindTo method, which will make treeview "flat". In the action, which is second parameter, you should perform recursive adding of items in the Items collection. Here is a code snippet showing what I mean:
<%     
    Html.Telerik().PanelBar()
                  .Name("PanelBar2")
                  .BindTo(Model, (treeViewItem, modelItem)=>
                   {
                       //In this action the root items is already added.
                       //you need to add child items, because only you knows
                       //which item of the flat collection is child of other
                       //item part of the same flat collection.
                       //treeViewItem.Items.Add(new TreeViewItem{
                       //set the properties
                       //});
                   })
                  .Render();
     %>

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.
Tags
TreeView
Asked by
Ales Tepina
Top achievements
Rank 1
Answers by
Georgi Krustev
Telerik team
Share this question
or