Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

jQuery EasyUI Tree Menu - Tree Menu loads parent/child nodes


May 09, 2021 jQuery EasyUI


Table of contents


jQuery EasyUI Tree Menu - Tree Menu loads parent/child nodes

Typically, a tree node is represented by storing a parentid at each node, also known as the adjacent list model. L oading this data directly into the Tree menu is not allowed, but we can convert it to the standard Tree data format before loading the tree menu. T he Tree plug-in provides a 'loadFilter' option function, which it can do. I t provides an opportunity to change any entry data.

This tutorial shows you how to load a parent/child node into a tree menu using the 'loadFilter' function.

jQuery EasyUI Tree Menu - Tree Menu loads parent/child nodes

Parent/child node data

	[
	{"id":1,"parendId":0,"name":"Foods"},
	{"id":2,"parentId":1,"name":"Fruits"},
	{"id":3,"parentId":1,"name":"Vegetables"},
	{"id":4,"parentId":2,"name":"apple"},
	{"id":5,"parentId":2,"name":"orange"},
	{"id":6,"parentId":3,"name":"tomato"},
	{"id":7,"parentId":3,"name":"carrot"},
	{"id":8,"parentId":3,"name":"cabbage"},
	{"id":9,"parentId":3,"name":"potato"},
	{"id":10,"parentId":3,"name":"lettuce"}
	]

Create a tree menu with 'loadFilter'

	$('#tt').tree({
		url: 'data/tree6_data.json',
		loadFilter: function(rows){
			return convert(rows);
		}
	});

The implementation of the transformation

	function convert(rows){
		function exists(rows, parentId){
			for(var i=0; i<rows.length; i++){ 				
                            if (rows[i].id == parentId) return true; 			
                        } 			
                        return false; 		
                 } 		 		
                 var nodes = []; 		
                 // get the top level nodes 		
                 for(var i=0; i<rows.length; i++){ 			
                     var row = rows[i]; 			
                     if (!exists(rows, row.parentId)){ 				
                         nodes.push({ 					
                             id:row.id, 					
                             text:row.name 				
                         }); 			
                     } 		
                 } 		 		
                 var toDo = []; 		
                 for(var i=0; i<nodes.length; i++){ 			
                     toDo.push(nodes[i]); 		
                 } 		
                 while(toDo.length){ 			
                    var node = toDo.shift();	// the parent node 			
                    // get the children nodes 			
                    for(var i=0; i<rows.length; i++){ 				
                        var row = rows[i]; 				
                        if (row.parentId == node.id){ 					
                            var child = {id:row.id,text:row.name}; 					
                            if (node.children){ 						
                                node.children.push(child); 					
                            } else { 						
                                node.children = [child]; 					
                            } 					
                            toDo.push(child); 				
                         } 			
                     } 		
                 } 		
                 return nodes; 	
        } 

Download the jQuery EasyUI instance

jeasyui-tree-tree6.zip