This is a migrated thread and some comments may be shown as answers.
Cannot get value of selected item in treeview
6 Answers 667 Views
This is a migrated thread and some comments may be shown as answers.
Phil
Top achievements
Rank 1
Phil asked on 05 Jul 2012, 12:47 AM
with the old telerik MVC extensions, i could do the following:
@(Html.Telerik().TreeView().Name("TreeView").HtmlAttributes(new { style = "white-space:normal;width:250px;" }).ClientEvents(events => events
                        .OnLoad("onLoad")
                        .OnSelect("onSelect")
                )
        .BindTo(Model.RootNode.ChildNodes, mappings =>
        {
            mappings.For<DocReaderNode>(binding => binding
                    .ItemDataBound((item, nd) =>
                    {
                        item.Text = nd.DocTitle;
                        item.Value = nd.DocUrl;
                    })
                    .Children(nd => nd.ChildNodes));
        })
)
then i could use the following javascript to get the node value when a node is selected:
function onSelect(e) {
var t = $('#TreeView').data('kendoTreeView');
var selVal = t.getItemValue(e.item) ;
}
Now with Kendo, i don't see how to get the value of the selected node any longer.  Any tips? 

6 Answers, 1 is accepted

Sort by
0
Trent
Top achievements
Rank 1
answered on 10 Jul 2012, 06:44 AM
Hey Phil. Did you get an answer for this? I'm also porting from Telerik to Kendo and ran into the same issue.

Thanks!
0
Alex Gyoshev
Telerik team
answered on 10 Jul 2012, 08:58 AM
Hello Phil, Trent,

Depending on your use case, you have two options:
  • Use HtmlAttributes to serialize arbitrary data, item.HtmlAttributes(new { data_foo = "bar" }), and access it through e.item.data("foo")
  • Use the TreeViewItem.Id field, if the serialized data is an Id and will be used for fetching nodes from a remote datasource. This field will be introduced in the upcoming Q2 release this week.
Kind regards,
Alex Gyoshev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Phil
Top achievements
Rank 1
answered on 12 Jul 2012, 06:28 PM
when you say 'release this week', do you mean the 2012.2.710 release that i just downloaded? 

That release actually broke the way the treeview used to work entirely.  The bindto(model, mapping => mapping.For .....  no longer exists.  Your documentation still references it at
http://docs.kendoui.com/api/wrappers/aspnet-mvc/Kendo.Mvc.UI.Fluent/TreeViewBuilder

however.  Not sure how to proceed with your currennt treeview.

Additionally, do you have an example with the treeviewitem.id field that you mention below?
0
Alex Gyoshev
Telerik team
answered on 16 Jul 2012, 06:18 AM
> The bindto(model, mapping => mapping.For .....  no longer exists. 

The method exists (as seen in the Intellisense), but when you have a syntax error in the lambda expression, the c# compiler assumes it has a different type -- and thus complains about the method.

There is no dedicated example that shows this, so here it goes:

.BindTo(new List<Employee> () {new Employee { FirstName = "Alex" }}, mappings => {
            mappings.For<Employee>(binding => binding
                .ItemDataBound((item, employee) =>
                {
                    item.Text = employee.FirstName;
                    item.Id = "2";
                }));
        })

Greetings,
Alex Gyoshev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Phil
Top achievements
Rank 1
answered on 16 Jul 2012, 03:10 PM
Thanks for that, the problem that i was having was assuming there was a TreeViewItem 'value' property in kendo like there was in the Telerik extensions.  Looks like that was taken out.

For some reason i can't get this ID value in the Select event.  basically, my code is:
function onSelect(e) {
                    //var treeView = $("#kTreeView").data("kendoTreeView");
                    var nd = e.node;
                    var t = nd.textContent;
                    var v = nd.id;
                    var c = nd.url;
}
so nd and the t variables work fine, i see values in them via firebug no problem.  The id and url values give me nothing at all though (I also changed your code above so that the item.Url value is populated.  that value might work better for me more than Id anyways). 
0
Accepted
Alex Gyoshev
Telerik team
answered on 18 Jul 2012, 02:15 PM
See the dataItem method.

Greetings,
Alex Gyoshev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
Asked by
Phil
Top achievements
Rank 1
Answers by
Trent
Top achievements
Rank 1
Alex Gyoshev
Telerik team
Phil
Top achievements
Rank 1
Share this question
or