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

Loading level during expanding node

7 Answers 154 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Bartosz
Top achievements
Rank 1
Bartosz asked on 06 Oct 2015, 10:17 PM

Hi,

my treeview configuration at view:

@Html.Raw((Html.Kendo().TreeView().Name("AppellationTree").LoadOnDemand(true)
                .Events(events => events
                    .Select("TreeView_onSelect"))
                    .Template("# return escapeHtml(item.Name); #")
                .DataSource(d => d
                    .Model(m => m
                        .Id("Id")
                        .HasChildren("HasChildren")
                        .Children("Children")
                        .Field("IsAppellation", typeof(bool)))
                    .Read(r => r.Action("_AppellationTree", "Appellation", new { id = Model.Id })))
                .DataTextField("Name")))

My _AppellationTree method:

[ActionName("_AppellationTree")]
        public JsonResult GetAppellationData(int id)
        {
            var model = new List<TreeItemModel>();
            var appellations = _appellationRepo.GetList(x => x.ParentAppellation.Id == id).OrderBy(x => x.Name);
            foreach (var appellation in appellations)
            {
                var r = new TreeItemModel();
                r.Id = appellation.Id;
                r.Name = appellation.Name;
                r.HasChildren = appellation.HasChildren;
                model.Add(r);
            }

            return Json(model, JsonRequestBehavior.AllowGet);
        }

I'd like to add Expand Action with javascript function and launch loading data during expanding node. How to do it? I think that should possible out of the box with three js lines like:

function onExpand(e)

{

     get data (next level) from server

     attache data to existing data source

     fire expanding

}

Best regards

Bart

7 Answers, 1 is accepted

Sort by
0
Boyan Dimitrov
Telerik team
answered on 08 Oct 2015, 04:13 PM

Hello Bartosz,

 

The Kendo UI TreeView has a expand event that is fired before a subgroup gets expanded. 

 

Regards,
Boyan Dimitrov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Bartosz
Top achievements
Rank 1
answered on 16 Nov 2015, 02:34 PM

Hi Boyan,

thank you for your response, but my main question is how to attache data received during ajax call to existing treeview data structure. I found:

var selectedId = $("#AppellationTree").getKendoTreeView().dataItem(e.node).id;
           
            $.ajax({
                dataType: "json",
                type: 'GET',
                cache: false,
                url: '/Appellation/_GetLevel/' + selectedId,
                success: function (model) {
                    var parentRow = [{ text: "", items: [] }];

                    parentRow.items = model;

                    var ds = new kendo.data.HierarchicalDataSource({
                        data: model,
                        schema: {
                            model: {
                                id: "Id",
                                Name: "Name",
                                hasChildren: "HasChildren",
                                parentTreeId: "ParentId"
                            }
                        }
                    });
                    debugger;

                    var treeview = $("#AppellationTree").kendoTreeView({
                        dataSource: ds,
                        loadOnDemand: true,
                        dataTextField: "Name"
                    }).data("kendoTreeView");
                }
            });

 but it does not work. Could you help me?

Best regards

Bartosz

0
Bartosz
Top achievements
Rank 1
answered on 16 Nov 2015, 03:13 PM

... and new level is passed as a collection of:

    public class TreeItemModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public bool HasChildren { get; set; }
        public int ParentId { get; set; }
    }

Best regards

Bartosz

0
Boyan Dimitrov
Telerik team
answered on 18 Nov 2015, 05:31 PM

Hello Bartosz,

 

Please review http://dojo.telerik.com/amAgE example that shows how to populate the Kendo UI TreeView using the server response. 

 

Regards,
Boyan Dimitrov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Bartosz
Top achievements
Rank 1
answered on 23 Nov 2015, 09:29 AM
Thank you Boyan, my scenario is:
1. Opening page with tree
2. Expand first level (A).
3. If user clicked on node on level A and node on level A has children, send request to server, get data about children node from level A and expand next level (B)
4. If user clicked on node on level B and node on level B has children, send request to server, get data about children node from level B and expand next level (C)
5. etc.
I have big impression that example which was given in your last post is loading all tree at one. 
I don't want it. 
My tree has 2000 nodes and downloading of them taking too much time. I need download every sublevel separately.

Best regards
Bartosz
0
Bartosz
Top achievements
Rank 1
answered on 23 Nov 2015, 09:46 AM

Thank you Boyan,

my scenario is:
1. Opening page with tree
2. Expand first level (A).
3. If user clicked on node on level A and node on level A has children, send request to server, get data about children of level A and expand this node (A).
4. If user clicked on node on level B and node on level B has children, send request to server, get data about children of level B and expand this node (B).
5. etc.
I have big impression that example which was given in your last post is loading entire tree at once. 
I don't want it. 
My tree has 2000 nodes and downloading of them taking too much time. I need download every sublevel separately.

Best regards
Bartosz

0
Boyan Dimitrov
Telerik team
answered on 24 Nov 2015, 01:30 PM

Hello Bartosz,

 

By default when the Kendo UI TreeView is bound to remote data it automatically sets the loadOnDemand option to true. When enabled the Kendo UI TreeView loads its child items when the parent group is expanded. 

 

Please refer to the demos of the ASP.NET MVC wrappers which are shipped in the distribution package. They can be found in the wrappers\aspnetmvc\Examples\ folder. The TreeView / Remote Data example shows how to use the datasource binding. Also you can check the controller code to see the implementation. 

 

Regards,
Boyan Dimitrov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
TreeView
Asked by
Bartosz
Top achievements
Rank 1
Answers by
Boyan Dimitrov
Telerik team
Bartosz
Top achievements
Rank 1
Share this question
or