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

Expand and Collapse All Nodes in TreeList

2 Answers 2681 Views
TreeList
This is a migrated thread and some comments may be shown as answers.
Kevin
Top achievements
Rank 1
Kevin asked on 19 Oct 2018, 08:33 PM
What is the best and most efficient way to expand and collapse all nodes for a given TreeList?  I'm currently using the following code where fp-grid is the root div element of my kendoTreeList.  It works.  But it's very slow!  Is there a better and more efficient way to do this?
var treeList = jQuery('#fp-grid').data("kendoTreeList");
if (treeList) {
  jQuery('#fp-grid tbody>tr').each(function (index) {
    if (treeExpand) {
      treeList.expand(jQuery(this));
    } else {
      treeList.collapse(jQuery(this));
    }
  });
}

2 Answers, 1 is accepted

Sort by
0
Accepted
Georgi
Telerik team
answered on 23 Oct 2018, 01:23 PM
Hi Kevin,

Another possible solution is to toggle the dataSource.schema.model.expanded property and update the dataSource via the setDataSource method.

e.g.

var treelist =   $("#treelist").data('kendoTreeList');
treelist.options.dataSource.schema.model.expanded = !treelist.options.dataSource.schema.model.expanded
 
treelist.setDataSource( treelist.options.dataSource)

Below you will find a small sample which demonstrates the above approach:



Regards,
Georgi
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
0
Kevin
Top achievements
Rank 1
answered on 23 Oct 2018, 01:41 PM
This is much faster!  Thank you!
Chris
Top achievements
Rank 1
Iron
commented on 08 Dec 2022, 03:15 PM

Testing this with v2022.3.1109 (ASP.NET/MVC and jQuery), the TreeList DataSource Read action gets executed when treelist.setDataSource( treelist.options.dataSource) executes. 

While this behaviour is fine for static content TreeLists, it is not suitable for TreeLists where users are editing existing records or adding new records to the TreeList  locally (e.g., not submitting them to the server/database).

Is there a mechanism for applying the expanded property without the DataSource Read action being executed?

Chris
Top achievements
Rank 1
Iron
commented on 08 Dec 2022, 04:47 PM

The following CSJS code will do the trick (inspired by https://docs.telerik.com/kendo-ui/knowledge-base/treelist-load-and-expand-one-level-of-data)

let items = treeList.items();//get the items from the current view
if ( doCollapse ) {
	treeList.collapse(items);
} else {
	treeList.expand(items);
}

For large data sets (I am testing this with 150+ multi-level parent nodes in a TreeList widget) this is faster than treeList.setDatasource() since it does not execute the DataSource Read action. More importantly, it is not destructive which means edited or inserted data items are preserved.

Nikolay
Telerik team
commented on 12 Dec 2022, 08:21 AM

Hi Chris,

Indeed, this is a valid solution as well. Than you for sharing it with the community. I believe it will be helpful to others facing the same scenario.

Regards,

Nikolay

Tags
TreeList
Asked by
Kevin
Top achievements
Rank 1
Answers by
Georgi
Telerik team
Kevin
Top achievements
Rank 1
Share this question
or