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

show k-icon k-i-collapse and Image in Kendo Tree View

2 Answers 673 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Robin
Top achievements
Rank 1
Veteran
Robin asked on 06 Dec 2020, 08:26 PM

Hello Everyone,

I am trying to build a kendo tree view with remote data  getting from the controller. I want to display a Image (attachment) at a certain node . How do I do that.?

Following is my data from the controller.

In the attached files, I have uploaded, I want to show the image at the arrow highlighted

Also. k-icon k-i-collapse is not showing at "Project Metrics" node even when the node is expanded.

 

 var result = new List<HierarchicalViewModel>()
            {


                new HierarchicalViewModel() { ID = 1, ParendID = null, HasChildren = true, Name = "Program Structure" },
               new HierarchicalViewModel() { ID = 3, ParendID = null, HasChildren = true, Name = "Project Metrics" },
                new HierarchicalViewModel() { ID = 2, ParendID = 1, HasChildren = true, Name = "Program Structure" },
            
                new HierarchicalViewModel() { ID = 4, ParendID = 3, HasChildren = true, Name = "Project Metrics" },
                new HierarchicalViewModel() { ID = 5, ParendID = 2, HasChildren = false, Name = "State" },
                new HierarchicalViewModel() { ID = 6, ParendID = 2, HasChildren = false, Name = "Executive Summary" },
                 new HierarchicalViewModel() { ID = 7, ParendID = 2, HasChildren = false, Name = "Reporting period for RHGCP funding" },
                new HierarchicalViewModel() { ID = 8, ParendID = 2, HasChildren = false, Name = "Describe how funds are distributed and administrated in the State" },
                new HierarchicalViewModel() { ID = 9, ParendID = 2, HasChildren = false, Name = "Describe the method(s) used for project selection" },
                new HierarchicalViewModel() { ID = 10, ParendID = 2, HasChildren = false, Name = "Describe the method(s) used to measure effectivenss of the projects and programs" },
                new HierarchicalViewModel() { ID = 11, ParendID = 2, HasChildren = false, Name = "Describe any noteworthy efforts the State has used to effectively deliver a successful program" },
               new HierarchicalViewModel() { ID = 12, ParendID = 2, HasChildren = false, Name = "Describe the status of data acquistion and analysis efforts" },
                new HierarchicalViewModel() { ID = 13, ParendID = 2, HasChildren = false, Name = "Add number of crossings" },
                new HierarchicalViewModel() { ID = 14, ParendID = 2, HasChildren = false, Name = "Provide program emphasis areas" },
                new HierarchicalViewModel() { ID = 15, ParendID = 2, HasChildren = false, Name = "Describe Section 130 program effectiveness" },
                new HierarchicalViewModel() { ID = 16, ParendID = 2, HasChildren = false, Name = "Input performance measures" },
                new HierarchicalViewModel() { ID = 17, ParendID = 3, HasChildren = false, Name = "Project Listing" },
                new HierarchicalViewModel() { ID = 18, ParendID = 3, HasChildren = false, Name = "Crash Data Statistics" }
            };

            return result;

 

//Kendo Tree View

 @(Html.Kendo().TreeView()
                                                .Name("treeview")
                                                .DataTextField("Name")
                                                 .Checkboxes(true)
                                                .DataSource(dataSource => dataSource
                                                .Read(read => read
                                                    .Action("Read_TreeViewData", "Questions")
                                                    )
                                                )
                                                .Events(ev => ev.Select("onTreeViewselectNode")
                                                .Check("onCheck")
                                                )
                                            )

<script>
                                    function onTreeViewselectNode(e) {
                                        var dataItem = e.sender.dataItem(e.node);
                                        console.log(dataItem);
                                    }

                                    function onCheck(e) {
                                        console.log("Checkbox changed :: " + this.text(e.node));
                                        var checkedNodes = [],
                                            treeView = $("#treeview").data("kendoTreeView"),
                                            message;

                                        checkedNodeIds(treeView.dataSource.view(), checkedNodes);

                                        if (checkedNodes.length > 0) {
                                            message = "IDs of checked nodes: " + checkedNodes.join(",");
                                        } else {
                                            message = "No nodes checked.";
                                        }

                                        $("#result").html(message);

                                    }

                                    function checkedNodeIds(nodes, checkedNodes) {
                                        for (var i = 0; i < nodes.length; i++) {
                                            if (nodes[i].checked) {
                                                checkedNodes.push(nodes[i].id);
                                            }

                                            if (nodes[i].hasChildren) {
                                                checkedNodeIds(nodes[i].children.view(), checkedNodes);
                                            }
                                        }
                                    }

                                </script>

2 Answers, 1 is accepted

Sort by
0
Accepted
Aleksandar
Telerik team
answered on 08 Dec 2020, 11:42 AM

Hi Robin,

Thank you for reaching out. I see you have posted the same question in several other forum threads: here and here. I will reply to this thread, as it contains the most information on the scenario, and will ask you to continue the discussion on loading the TreeView data and setting the expanded state here. I assure you we monitor all communication channels and once a question is posed we will get back to you.

The reason for the observed behavior for the expand icon to not show on the second "Program Structure" node is an incorrect reference to the parent id:

public static IList<HierarchicalViewModel> GetHierarchicalData()
        {
            var result = new List<HierarchicalViewModel>()
            {
                new HierarchicalViewModel() { ID = 1, ParendID = null, HasChildren = true, Name = "Program Structure" },
                new HierarchicalViewModel() { ID = 3, ParendID = null, HasChildren = true, Name = "Project Metrics" },
                new HierarchicalViewModel() { ID = 2, ParendID = 1, HasChildren = true, Name = "Program Structure" },

                new HierarchicalViewModel() { ID = 4, ParendID = 3, HasChildren = true, Name = "Project Metrics" },
                new HierarchicalViewModel() { ID = 5, ParendID = 2, HasChildren = false, Name = "State" },
                new HierarchicalViewModel() { ID = 6, ParendID = 2, HasChildren = false, Name = "Executive Summary" },
                new HierarchicalViewModel() { ID = 7, ParendID = 2, HasChildren = false, Name = "Reporting period for RHGCP funding" },
                new HierarchicalViewModel() { ID = 8, ParendID = 2, HasChildren = false, Name = "Describe how funds are distributed and administrated in the State" },
                new HierarchicalViewModel() { ID = 9, ParendID = 2, HasChildren = false, Name = "Describe the method(s) used for project selection" },
                new HierarchicalViewModel() { ID = 10, ParendID = 2, HasChildren = false, Name = "Describe the method(s) used to measure effectivenss of the projects and programs" },
                new HierarchicalViewModel() { ID = 11, ParendID = 2, HasChildren = false, Name = "Describe any noteworthy efforts the State has used to effectively deliver a successful program" },
                new HierarchicalViewModel() { ID = 12, ParendID = 2, HasChildren = false, Name = "Describe the status of data acquistion and analysis efforts" },
                new HierarchicalViewModel() { ID = 13, ParendID = 2, HasChildren = false, Name = "Add number of crossings" },
                new HierarchicalViewModel() { ID = 14, ParendID = 2, HasChildren = false, Name = "Provide program emphasis areas" },
                new HierarchicalViewModel() { ID = 15, ParendID = 2, HasChildren = false, Name = "Describe Section 130 program effectiveness" },
                new HierarchicalViewModel() { ID = 16, ParendID = 2, HasChildren = false, Name = "Input performance measures" },
                new HierarchicalViewModel() { ID = 17, ParendID = 4, HasChildren = false, Name = "Project Listing" },
                new HierarchicalViewModel() { ID = 18, ParendID = 4, HasChildren = false, Name = "Crash Data Statistics" }
            };

            return result;
        }

You have two nodes with the same NAme, but different ID, so the child nodes should point to the second parent - ParendID = 4. With this update the nodes will be displayed as desired:

As for the other question on expanding all nodes once the TreeView is loaded I would suggest handling the dataBound event and expanding all items, once data for a node is loaded:

@(Html.Kendo().TreeView()
    .Name("treeview")
//aditional configuration options
    .Events(ev => ev.Select("onTreeViewselectNode").Check("onCheck").DataBound("onDataBound"))
)

function onDataBound(e) {
               e.sender.expand(".k-item")
        }

I hope the above helps. Let me know if you have additional questions.

Regards,
Aleksandar
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Robin
Top achievements
Rank 1
Veteran
answered on 30 Jan 2021, 02:48 PM
Thank you the response , I made the change and it worked
Tags
TreeView
Asked by
Robin
Top achievements
Rank 1
Veteran
Answers by
Aleksandar
Telerik team
Robin
Top achievements
Rank 1
Veteran
Share this question
or