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

'HasChildren = false' ignored

1 Answer 187 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Paul
Top achievements
Rank 1
Paul asked on 16 Mar 2014, 01:23 AM
I have the following declaration:

@(Html.Kendo().TreeView()
    .Name("FolderTree")
    .TemplateId("treeviewtemplate")
    .Events(e => e
        .Select("tvOnSelect")
        .DataBound("tvOnDataBound")
        .DragEnd("tvOnDragEnd")
    )
    .BindTo(Model.Folders, (Kendo.Mvc.UI.Fluent.NavigationBindingFactory<TreeViewItem> mappings) =>
    {
        mappings.For<PriMate.Web.Models.FolderModel>(bound => bound.ItemDataBound((node, item) =>
        {
            node.Text = item.name;
            node.Id = item.id.ToString();
            node.HasChildren = false;
        })
        .Children(item => item.folders));
    })
)


I want to override the 'HasChildren' property with a property from my own model/class and display an image next to the tree item if HasChildren is false. This is my (part of) my template:

# if (item.hasChildren) { #
    <input type="image" src="Content/images/trash.png" class="delete-node" onclick="return confirmDelete('folder', '${item.text}', '${item.id}')" title="delete ${item.text}">
# } #


However, I've noticed that:

1. When the property is hard-coded true, it behaves as expected - the image is hidden for all nodes
2. When the property is hard-coded false, it is ignored, and the image is shown only if the the node does not have children (default)
3. When I set the property using my own class property, (node.HasChildren = item.hasChildren;), all images are hidden, despite the fact that this bool value varies.

Is it possible to override this property successfully? If not, can I add my own custom fields to the mappings?

1 Answer, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 19 Mar 2014, 08:26 AM
Hello,

I am afraid that this is not supported. When the Children mapping is specified the HasChildren property is ignored. If you wish to use a property of the model to determine if the children should be rendered, then you should use the Items method instead to bind the data e.g.
@functions{
    public void Bind(Kendo.Mvc.UI.Fluent.TreeViewItemFactory factory, IEnumerable<PriMate.Web.Models.FolderModel> data)
    {       
        foreach (var folder in data)
        {
            var node = factory.Add().Text(folder.name).Id(folder.id.ToString());
            if (folder.HasSubFolders && folder.folders != null && folder.folders.Any())
            {
                node.Items(children =>
                    {
                        Bind(children, folder.Items);
                    });
            }
        }
         
    }
}
 
@(Html.Kendo().TreeView()
    .Name("FolderTree")
    .TemplateId("treeviewtemplate")
    .Items(items =>
        {
            Bind(items, Model);
        })
)


Regards,
Daniel
Telerik
 

DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

 
Tags
TreeView
Asked by
Paul
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or