Showing Loading State When Expanding Nodes in Kendo UI for Vue TreeView
Environment
| Product | Kendo UI for Vue TreeView |
| Version | Current |
Description
I need to show a loading spinner when a node in the Kendo UI for Vue TreeView is expanding and data is being loaded from the server.
This knowledge base article also answers the following questions:
- How to add a loading spinner in Vue TreeView when expanding nodes?
- How to show a loading indicator for server requests in TreeView?
- How to implement a custom loading state in Kendo UI for Vue TreeView?
Solution
Track a loading state for each node and use a custom item slot to display a Loader next to the node text while its children are loading. The slot customizes the node content; it does not replace the built-in expand/collapse icon.
Follow these steps:
- Set
hasChildrentotruefor nodes whose children are not loaded yet, and leave theiritemsfield unset. This lets you expand a node before its children are available. - Handle the
expandchangeevent to toggle the node'sexpandedstate. Request children only when the node is expanded, has no loaded items, and is not already loading. - Set the node's
loadingflag before starting the request. Assign the returned children toitems, updatehasChildrento reflect the result, and clear the flag in afinallyblock. - Set the TreeView's
itemprop to a named slot. In the slot, usev-ifto render the Loader only while that node's loading flag is active.
Runnable Example
The following example simulates a server request with a 1.5-second delay. Expand Furniture or Decor to see the loader, then expand Bed Linen to load another level. Loaded children are reused when you collapse and expand a node again.
Replace the loadChildren function with your server request when using this approach in your application.