'HasChildren = false' ignored

2 posts, 0 answers
  1. Paul
    Paul avatar
    9 posts
    Member since:
    Feb 2014

    Posted 15 Mar 2014 Link to this post

    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?

  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 19 Mar 2014 Link to this post

    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.

     
  3. UI for ASP.NET MVC is VS 2017 Ready
Back to Top