chiapa
chiapa

Reputation: 4412

Getting id of selected node in a Kendo TreeView

I've got this controller method that sends the tree items to the tree in the view:

private IEnumerable<TreeViewItemModel> GetTrees()
{
    InstallationPlaceModel ipm = new InstallationPlaceModel();
    var gipo = ipm.getRootInstallationPlaces();
    List<TreeViewItemModel> fullTree = new List<TreeViewItemModel>();

    foreach (wsInstallationPlace.installationPlaceOutput father in gipo.installationPlaces)
    {
        var gipo2 = ipm.getChildInstallationPlaces(father.installationPlace.id);
        List<TreeViewItemModel> childTree = new List<TreeViewItemModel>();

        foreach (wsInstallationPlace.installationPlaceOutput child in gipo2.installationPlaces)
        {
            TreeViewItemModel childTreeItem = new TreeViewItemModel
            {
                Text = child.installationPlace.mediumDescription,
                Id = child.installationPlace.id
            };          
            childTree.Add(childTreeItem);
        }
        TreeViewItemModel fatherTreeItem = new TreeViewItemModel
        {
            Text = father.installationPlace.mediumDescription,
            Id = father.installationPlace.id,
            Items = childTree
        };
        fullTree.Add(fatherTreeItem);
    }
    ViewBag.mytree = fullTree;
    return fullTree;
}

This is the Kendo TreeView:

@(Html.Kendo().TreeView()
    .Name("treeview")
    .DragAndDrop(true)
    .Events(e => e.Select("onSelect"))
    .BindTo((IEnumerable<TreeViewItemModel>)ViewBag.mytree)
)

And this function to handle selection of a tree node:

function onSelect(e) {
    alert(this.text(e.node));
}

When a node is selected, an alert with the node's text is displayed. I wanted to show the id of the node. I tried:

function onSelect(e) {
    alert(this.id(e.node));
}

but no luck. As you can see in the controller method, I am filling the text and id properties but I can only access the text. Any help?

LIVE DEMO

Upvotes: 0

Views: 2065

Answers (1)

OnaBai
OnaBai

Reputation: 40887

For getting the id of the selected node, you should use $(e.node).data("id").

function onSelect(e) {
    alert($(e.node).data("id"));
}

Upvotes: 1

Related Questions