5

I have a Kendo tree that I am binding using local data

Everything works fine with the code I have.

However, I am trying to add custom attributes to the items generated, like data-name.

How can I do this using kendo.data.HierarchicalDataSource?

// bind kendo tree
var treeDataSource = new kendo.data.HierarchicalDataSource({
    data: [{ Id: "id", Text: "Node 1", HasChildren: false, ChildrenFolders: [], HtmlAttributes: { "data-name": "Custom Name" } }],
    schema: {
        model: {
            children: "ChildrenFolders",
            hasChildren: "HasChildren",
            id: "Id",
            htmlAttributes: "HtmlAttributes"
        }
    }
});

this.kendoTreeView = $("#tree").kendoTreeView({
    dataSource: treeDataSource,
    dataTextField: "Text",
    loadOnDemand: false
}).data("kendoTreeView");
numaroth
  • 1,295
  • 4
  • 25
  • 36
Catalin
  • 11,503
  • 19
  • 74
  • 147

2 Answers2

3

You'd have to replace the item template in the treeview widget, which unfortunately would mean replacing the whole _template method. I'd suggest creating the DOM structure manually (as done here), that way you can set the attributes before initializing the treeview. Another option would be to use the treeview's template option (in that case, you can only add the attributes on the child elements though).

Lars Höppner
  • 18,252
  • 2
  • 45
  • 73
0

You can get dataSource item properties using dataItem(), method takes jQuery selector as parameter:

var treeview = $("#treeview").kendoTreeView({
                        animation: false,
                        dataSource: dataSource,
                        select: function (e) {
                            var dataitem = treeview.dataItem("#treeview_tv_active");
                            //dataitem.HtmlAttributes
                        }
                    }).data("kendoTreeView");
enumm
  • 13
  • 3