Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Basic Javascript Code to Hierarchical Sort Flat Parent Child Data

    I am using BIRT and I don't have access to the deployment server which limits my language to basic javascript.

    I am iterating through rows of data. Each row of data has a EmployeeID and ManagerID.

    I need some basic javascript code that will input either EmployeeID and ManagerID one row at a time or that will input a two dimensional array of EmployeeID and ManagerID pairs and then output an array of hierarchically sorted EmployeeIDs.

    For example, if the input data looks like this:

    Code:
    CompanyEmpId MgrCompanyEmpId            
    0            1          
    2            0          
    3            0          
    4            0          
    5            0          
    6            0          
    7            0          
    8            0          
    9            3          
    10           6          
    11           7          
    12           5
    The output would be.

    Code:
    EmployeeID  
    2   
    3   
    9   
    4   
    5   
    12  
    6   
    10  
    7   
    11  
    8
    My example only traverses 3 levels deep, but I need code that will traverse down to any number of levels.

    I have spent quite a bit of time trying to figure this out on my own. I have been reading on Google for about a year trying to figure this out. I actually need this code for a project I am working on, but if someone has the time to provide me with some code that works along with explanations as to how it works, it would be a wonderful learning experience.

    If there are any helpful links, that would be just as good as I would prefer to figure this out and code it myself. I am new to building tree type data, so I really just need a good example or a good starting point.

  • #2
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm sorry, my output got truncated. It should be:

    Code:
    EmployeeID  
    1
    0
    2   
    3   
    9   
    4   
    5   
    12  
    6   
    10  
    7   
    11  
    8

  • #3
    Senior Coder xelawho's Avatar
    Join Date
    Nov 2010
    Posts
    2,775
    Thanks
    55
    Thanked 519 Times in 516 Posts
    could you explain why the output would be like that? maybe I'm being thick but I can't see why, for example, 9 appears between 3 and 4

  • #4
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the reply!

    It's because of the parent child relationships. I need to sort by hierarchy. I will try to represent this below with generic indentation.

    Code:
    EmployeeID  
    1
      0
        2   
        3   
          9   
        4   
        5   
          12  
        6   
          10  
        7   
          11  
        8

  • #5
    Regular Coder
    Join Date
    Aug 2010
    Posts
    945
    Thanks
    19
    Thanked 205 Times in 203 Posts
    Quote Originally Posted by dbmathis View Post
    I am iterating through rows of data. Each row of data has a EmployeeID and ManagerID.
    You should show us how
    you are "iterating"
    Quote Originally Posted by dbmathis View Post
    My example only traverses 3 levels deep, but I need code that will traverse down to any number of levels.
    You could use recursion.
    Recursion with JSON

  • #6
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am in BIRT and the iterator is the onFetch event of the data set that I am pulling in.

    Here is my first attempt at this myself using recursion. I borrow the obj sort function from here

    Please let me know if this is the proper way to do this or if it can be better. It seems to work though.

    Code:
    var tree = {
        '12' : '5',
        '11' : '7',
        '10' : '6',
        '9' : '3',
        '8' : '0',
        '7' : '0',
        '6' : '0',
        '5' : '0',
        '4' : '0',
        '3' : '0',
        '2' : '0',
        '0' : null
    };		
    
    var sortedTree = sortObj(tree);
    
    output = parseTree(sortedTree);
    
    function parseTree (tree, root) {
    	if(typeof root === "undefined"){
    		root = null;
    	}
        var output = {};
        // Traverse the tree and search for direct children of the root
        for (var child in tree) {
           var parent = tree[child];
            // A direct child is found
            if(parent == root) {
            logToDebugWindow(child);
                // Remove item from tree (we don't need to traverse this again)
                delete(tree[child]);
                // Append the child into result array and parse its children
                output = {
                    'name' : child,
                    'children' : parseTree(tree, child)
                }
            }
        }
    }			
    
    function sortObj(obj, type, caseSensitive) {
      var temp_array = [];
      for (var key in obj) {
        if (obj.hasOwnProperty(key)) {
          if (!caseSensitive) {
            key = (key['toLowerCase'] ? key.toLowerCase() : key);
          }
          temp_array.push(key);
        }
      }
      if (typeof type === 'function') {
        temp_array.sort(type);
      } else if (type === 'value') {
        temp_array.sort(function(a,b) {
          var x = obj[a];
          var y = obj[b];
          if (!caseSensitive) {
            x = (x['toLowerCase'] ? x.toLowerCase() : x);
            y = (y['toLowerCase'] ? y.toLowerCase() : y);
          }
          return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        });
      } else {
        temp_array.sort();
      }
      var temp_obj = {};
      for (var i=0; i<temp_array.length; i++) {
        temp_obj[temp_array[i]] = obj[temp_array[i]];
      }
      return temp_obj;
    };

  • #7
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am waiting on the moderator. I had posted a reply with some code I started on that seems to work, but I had given credits to the author of the obj sort function I used and that has to have approval.

  • #8
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyway, the code is not working exactly right. It's missing people in my data. I am iterating with the onFetch event in BIRT. It iterates through rows of data while fetching a data set. For the purposes of this exercise the following sample data structure can be used.

    /**
    Tree data example:

    Code:
    var tree = {
        '12' : '5',
        '11' : '7',
        '10' : '6',
        '9' : '3',
        '8' : '0',
        '7' : '0',
        '6' : '0',
        '5' : '0',
        '4' : '0',
        '3' : '0',
        '2' : '0',
        '0' : null
    };

  • #9
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I have this working. The missing children was being caused by case in the obj sort function. Let me know if you see flaws in the way I am doing this. I don't know much about recursion or javascript, but wanted to at least give this my best effort if asking for help. Would love to hear from the experts, if I am on the right path.

    Code:
    var tree = {
        '12' : '5',
        '11' : '7',
        '10' : '6',
        '9' : '3',
        '8' : '0',
        '7' : '0',
        '6' : '0',
        '5' : '0',
        '4' : '0',
        '3' : '0',
        '2' : '0',
        '0' : null
    };		
    
    var count = 1;
    
    var sortedTree = sortObj(tree,'key',1);
    
    output = parseTree(sortedTree);
    
    function parseTree (tree, root) {
    	if(typeof root === "undefined"){
    		root = null;
    	}
        var output = {};
        // Traverse the tree and search for direct children of the root
        for (var child in tree) {
           var parent = tree[child];
            // A direct child is found
            if(parent == root) {
            	reportContext.setPersistentGlobalVariable(child + 'sort', count.toString());
            	count++;
            	logToDebugWindow(child);
                // Remove item from tree (we don't need to traverse this again)
                delete(tree[child]);
                // Append the child into result array and parse its children
                output = {
                    'name' : child,
                    'children' : parseTree(tree, child)
                }
            }
        }
    }			
    
    function sortObj(obj, type, caseSensitive) {
      	var temp_array = [];
      	for (var key in obj) {
        	if (obj.hasOwnProperty(key)) {
          		if (!caseSensitive) {
            		key = (key['toLowerCase'] ? key.toLowerCase() : key);
          		}
          		temp_array.push(key);
        	}
      	}
      	if (typeof type === 'function') {
        	temp_array.sort(type);
      	} else if (type === 'value') {
        	temp_array.sort(function(a,b) {
          		var x = obj[a];
          		var y = obj[b];
          		if (!caseSensitive) {
            		x = (x['toLowerCase'] ? x.toLowerCase() : x);
            		y = (y['toLowerCase'] ? y.toLowerCase() : y);
          		}
          		return ((x < y) ? -1 : ((x > y) ? 1 : 0));
        	});
      	} else {
        	temp_array.sort();
      	}
      	var temp_obj = {};
      	for (var i=0; i<temp_array.length; i++) {
        	temp_obj[temp_array[i]] = obj[temp_array[i]];
      	}
      	return temp_obj;
    };
    The global variable is keeping track of the sort which I will use in my table in BIRT later on.
    Last edited by dbmathis; 09-23-2013 at 11:05 PM. Reason: To make code and explanation clearer.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •