How to Append Nodes on Expanding Parent Node in JavaScript TreeView?
[index.js]
// Render the TreeView by mapping its fields property with data source properties var treeObj = new ej.navigations.TreeView({ fields: { dataSource: data, value: 'id', text: 'text', parentValue: 'pid', hasChildren: 'hasChild', }, nodeExpanding: function (args) { if ( args.node.querySelectorAll('.e-icons.e-icon-expandable').length > 0 && args.node.querySelectorAll('ul li').length == 0 ) { this.currentTarget = args.node; this.parentID = args.node.getAttribute('data-uid'); this.fetchChildNodes(args.node, args.node.getAttribute('data-uid')); } }, fetchChildNodes(currentTarget, parentID) { var request = new XMLHttpRequest(); request.open( 'GET', 'http://localhost:52799/Home/Get?%24filter=' + parentID, true ); var proxy = this; var parent = currentTarget; request.onload = function () { if (request.status >= 200 && request.status < 400) { // Success! var resp = request.responseText; proxy.addNodes(JSON.parse(resp), parent); } }; .... }, });
|
[HttpGet] public IEnumerable<OrdersDetails> Get() { var data = OrdersDetails.GetAllRecords().ToList(); var queryString = Request.Query; // return TreeView data; if (queryString.Keys.Contains("$filter")) { string filter = string.Join("", queryString["$filter"].ToString().Split(' ').Skip(2));// get filter from querystring filter = filter.Trim('\''); // filter the data based on the expand node id. data = data.Where(d => d.pid.ToString() == filter).ToList(); return data; } else { // if the parent id is null. data = data.Where(d => d.pid == null).ToList(); return data; } } |
See Also :
Conclusion
I hope you enjoyed learning how to append nodes on expanding parent node in JavaScript TreeView.
You can refer to our JavaScript TreeView feature tour page to know about its other groundbreaking feature representations and documentation, and how to quickly get started for configuration specifications. You can also explore our JavaScript TreeView example to understand how to create and manipulate data.For current customers, you can check out our components from the License and Downloads page. If you are new to Syncfusion, you can try our 30-day free trial to check out our other controls.
If you have any queries or require clarifications, please let us know in the comments section below. You can also contact us through our support forums, Direct-Trac, or feedback portal. We are always happy to assist you!