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

databinding multiple kind of children

9 Answers 161 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.
Paul Evers
Top achievements
Rank 1
Paul Evers asked on 11 Feb 2011, 04:15 PM
I want to bind a model to a treeview where you got groups and items. Groups can have subgroups. Each Group/Subgroup can have multiple items.
A group can have groups and items as children. With the following code the first Children-attribute is ignored.
Is it possible to have more than one kind of children in a treeview?

<%= Html.Telerik().TreeView()
           .Name("TreeView")
           .BindTo(Model.BaseGroups, mappings =>
               
                   mappings.For<Group>(binding => binding
                       .ItemDataBound((item, tlgroup) =>
                       {
                           item.Text = tlgroup.DisplayName;
                       })
                       .Children(tlgroup => tlgroup.Groups)
                       .Children(tlgroup => tlgroup.Lights));
                   mappings.For<Light>(binding => binding
                       .ItemDataBound((item, tl) =>
                        {
                            item.Text = tl.DisplayName;
                        }));
               })
    %>

Paul

9 Answers, 1 is accepted

Sort by
0
Boris
Top achievements
Rank 1
answered on 17 Feb 2011, 10:57 PM
Did you figure out how to do this?
I have exactly the same problem with in my application design.

I have figured out how to do a recursive folder structure with mappings for Folder object, but my folders can contain collections of different types of objects. I guess it would be possible with different model, but the View syntax would be a better way.

Thanks,
Boris
0
Paul Evers
Top achievements
Rank 1
answered on 18 Feb 2011, 08:43 AM
No. As nobody responded to my question I don't think this is possible.
0
Boris
Top achievements
Rank 1
answered on 19 Feb 2011, 04:20 PM
I have created a custom model class. Maybe this approach will work for you too.
I create it with the call
new TreeItem(rootFolder)

public
partial class TreeItem
{
    public String Name { get; set; }
    public String Type { get; set; }
    public IEnumerable<TreeItem> Children { get; set; }
    public String ImageUrl { get; set; }
 
    public TreeItem(Folder f)
    {
        this.Name = f.Name;
        this.Type = "F";
        List<TreeItem> children = new List<TreeItem>();
        foreach (Folder childf in f.Folders)
        {
            TreeItem item = new TreeItem(childf);
            item.ImageUrl = "../../Content/Images/FolderOpen.png";
            children.Add(item);
        }
        foreach (Book childb in f.Books)
        {
            TreeItem item = new TreeItem(childb);
            item.ImageUrl = "../../Content/Images/Book.png";
            children.Add(item);
        }
        this.Children = children;
    }
 
    public TreeItem(Book b)
    {
        this.Name = b.Name;
        this.Type = "B";
    }
 
}
0
dani
Top achievements
Rank 1
answered on 07 Mar 2011, 09:35 AM
I'm also looking for a solution for this problem.... without rebuilding the entire tree.
It looks like have 2 children makes the tree-view ignore the first one.
I think it just ain't meant to be in this version, or we are doing something wrong....
Dani
0
Nick
Top achievements
Rank 1
answered on 02 May 2011, 08:10 PM
I'm encountering the same problem. I have multiple types of children that need to rendered differently, but not having much luck finding a solution with Telerik's MVC TreeView that supports it...
0
Atanas Korchev
Telerik team
answered on 03 May 2011, 07:57 AM
Hi,

 The TreeView (and the other navigational components) do not support specifying to more than one type of children. This is because calling Children() overwrites the previous setting. 

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
Linus
Top achievements
Rank 1
answered on 19 May 2011, 02:08 PM
That's a shame as that's what I'm doing (calling Children() twice) and wondered why it didn't work. You should document that in the example.
0
Andres
Top achievements
Rank 1
answered on 03 Aug 2011, 08:42 PM
I found a solution to create a tree with items of different  types. In this case a Group has a collection of children's of type Group and a set of articles of type Article. So, I use a union to compose both collections, but first I cast both collections into a collection of objects.

Hope this helps

@(Html.Telerik().TreeView()
                .Name("GroupsTree")
                .ExpandAll(true)
                .BindTo(Model, mappings =>
                {
                    mappings.For<Group>(binding => binding
                            .ItemDataBound((item, group) =>
                            {
                                item.Text = group.Name;
                                item.Value = group.GroupId.ToString();
                                item.Expanded = true;
                            })
                            .Children(group => ((IEnumerable<object>) group.Children).Union ((IEnumerable<object>)group.Articles)));
                    
                    mappings.For<Article>(binding => binding
                            .ItemDataBound((item, article) =>
                            {
                                item.Text = article.Name;
                                item.Value = article.ArticleId.ToString();
                                item.Expanded = true;
                            }).Children(article => null));
                }))
0
Nick
Top achievements
Rank 1
answered on 27 Sep 2011, 07:10 PM
That worked perfectly for me. I did exactly as you described and unioned the two collections after casting them to IEnumerable<Object>. The grid's mappings for the types that were being unioned together took care of the bindings and it displayed correctly. I don't know if this has implications later on for other aspects of the treeview but so far it does what I need. Thanks :-)
Tags
TreeView
Asked by
Paul Evers
Top achievements
Rank 1
Answers by
Boris
Top achievements
Rank 1
Paul Evers
Top achievements
Rank 1
dani
Top achievements
Rank 1
Nick
Top achievements
Rank 1
Atanas Korchev
Telerik team
Linus
Top achievements
Rank 1
Andres
Top achievements
Rank 1
Share this question
or